To get the list of folders of a list you can use the Folders
property of the SPList
object:
private SPFolderCollection GetListFolders(SPList list) {
return list.Folders;
// you can also do:
// return list.Folders.Cast<SPFolder>().ToList();
// to return a List<SPFolder> instead of a SPFolderCollection
}
To check if a given user has Contribute permission on a folder you need to get the SPListItem
associated with the SPFolder,
check for a RoleAssignment
of the given user and check its RoleDefinitionBindings
for the Contribute Role Definition:
private bool HasContributePermissionOnFolder(SPFolder folder, SPPrincipal user) {
var contributePermission = folder.ParentWeb.RoleDefinitions["Contribute"];
var roleAssignementsOfUser = folder.Item.RoleAssignments.Cast<SPRoleAssignment>()
.Where(ra => ra.Member == user);
var hasContributePermission = roleAssignementsOfUser
.Where(ra => ra.RoleDefinitionBindings.Contains(contributePermission)).Count() > 0;
return hasContributePermission;
}
Usage example
//remember to add using System.Linq; for the above code to work
//SPList list = <your list>;
//SPWeb web = <your web>;
var folders = GetAllFoldersOfList(list);
foreach (SPFolder folder in folders) {
if (HasContributePermissionOnFolder(folder, spWeb.CurrentUser)) {
// do stuff
}
SPListItemCollection
toSPFolderCollection
, orSPListItem
toSPFolder
, the compiler won't have it. – Inocenciainoculable