[英]Get all SharePoint folder permissions of a particular user programatically
I am trying to access permissions of all folders(not hidden) in a site for a particular user 我正在尝试为特定用户访问网站中所有文件夹(未隐藏)的权限
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
SPWeb web = site.OpenWeb();
SPFolderCollection folders = web.Folders;;
foreach (SPFolder folder in web.Folders)
{
lblFolder.Text += "<br/><STRONG>" + folder.Name + "</STRONG>
<br/>";
foreach (SPRoleAssignment folderRole in folder.Item.RoleAssignments)
{ }// throws exception object specifies does not belong to list
}
Not only name but i need the permissions on that folder, please help!! 不仅名称,而且我需要该文件夹的权限,请帮助!!
Thanks 谢谢
You're grabbing folders when maybe you should be grabbing the list items of the document library? 您正在抓取文件夹时,也许您应该抓取文档库的列表项? That is, assuming they inherit from the top.
也就是说,假设它们从顶部继承。
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["Documents"];
SPQuery qry = new SPQuery();
qry.Query = "<Where><Eq><FieldRef Name='Title'><Value Type='Text'>"+title+"</Value></Eq></Where>";
SPListItemCollection docColl = new SPListItemCollection(qry);
List<string> perms = new List<string>();
if (docColl.Count > 0)
{
SPListItem fldrItem = docColl[0];
if (fldrItem.RoleAssignments.Count > 0) {
SPRoleAssignmentCollection assignColl = fldrItem.RoleAssignments;
foreach (SPRoleAssignment assignment in assignColl)
{
perms.Add(assignment.Member.LoginName);
Console.WriteLine("Perms: " + assignment.Member.LoginName);
}
}
}
}
}
SPWeb.Folders gives you a collection of the "sub folders" of your web site (Like _catalogs, Lists, ...) none of these have an associated List Item, some of them like Shared Documents may have an associated list, but in that case they are the root folder which doesn't have a list item (the rights come directly from the list). SPWeb.Folders为您提供了网站的“子文件夹”的集合(如_catalogs,List等),它们都不具有关联的列表项,其中一些诸如“共享文档”可能具有关联的列表,但是在在这种情况下,它们是没有列表项的根文件夹(权限直接来自列表)。
So you should check if Item is null (Maybe in that case use ParentListId to get the List if it's not Guid.Empty) and then continue by parsing SubFolders recursively 因此,您应该检查Item是否为null(在这种情况下,如果不是Guid.Empty,则可以使用ParentListId获取List),然后继续递归解析SubFolders
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.