要获取列表的文件夹列表,您可以使用对象的Folders
属性SPList
:
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
}
要检查给定用户是否对文件夹具有贡献权限,您需要获取与给定用户的检查SPListItem
相关联并检查其贡献角色定义:SPFolder,
RoleAssignment
RoleDefinitionBindings
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;
}
使用示例
//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
}