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
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).
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
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);
}
}
}
}
}
精彩评论