如何在Sharepoint中隐藏文件夹

Ros*_*lai 1 directory sharepoint hide

我需要在下面的代码中添加什么来确保文件夹是隐藏的还是只读的?

SPListItem createFolder = myDocLib.Folders.Add(myDocLib.RootFolder.ServerRelativeUrl,SPFileSystemObjectType.Folder, "Folder444");

folder.Update();

Gra*_*ote 5

与实际列表不同,文件夹不能在正常意义上"隐藏".也就是说,您不能使UI不可见,但仍然具有与之关联的所有正常访问级别(例如,传统的工作流历史列表以这种方式隐藏).在某种程度上,隐藏和使其成为只读的目标在文件夹中是一致的,因为两者都是由权限完成的.您可以通过复杂的视图过滤器从技术上降低特定项目的可见性,但这只会改变视图级别的内容.

文件夹所在的列表项的可见性取决于您是否对该项具有权限.真正"隐藏"列表项的唯一方法是从该列表项中删除某人的所有权限.为了使文件夹成为只读,您将从文件夹中删除所有添加,编辑和删除权限.如果您仍具有默认的SharePoint权限级别(完全控制,设计,贡献和读取),则只需将所有内容重新分配给读取即可实现此目标.

下面是一个代码示例,用于对所有用户进行讨论(这是一个文件夹),除了"工程师"组中的那些用户,它们是只读的.

// I excluded these two variable declarations because they were specific to 
//   my situation.
// Giving you my variable declarations would be misleading, since this is 
//   meant to be a generic code sample.
// SPListItem folder is the folder I am working on.
// SPWeb web is the SPWeb for the site I am on.
if (!folder.HasUniqueRoleAssignments)
{ 
    folder.BreakRoleInheritance(false); //Setting false erases all permissions, so now the folder is hidden from everyone.
    folder.SystemUpdate(false); 
}
SPRoleDefinition role = web.RoleDefinitions["Read"];
SPPrincipal smith = web.SiteGroups["Engineers"]; //You would replace "Engineers" with the name of whatever group you want to have read permissions. Or, use web.SiteUsers[] to get a specific user.
SPRoleAssignment roleAssignment = new SPRoleAssignment(smith);
roleAssignment.RoleDefinitionBindings.Add(role);
folder.RoleAssignments.Remove(smith); //If I have already assigned a different permission, this will clear it so that I ensure that the only permission is Read.
folder.RoleAssignments.Add(roleAssignment); //This sets it to Read-Only for these people.
folder.SystemUpdate(false);
Run Code Online (Sandbox Code Playgroud)

编辑

分配多个实例时,它取决于您是在执行所有组还是只是一个子集.我会列出所有组的代码,因为这是你直接问的.

if (!folder.HasUniqueRoleAssignments)
{ 
    folder.BreakRoleInheritance(false); //Setting false erases all permissions, so now the folder is hidden from everyone.
    folder.SystemUpdate(false); 
}
SPRoleDefinition role = web.RoleDefinitions["Read"];
SPRoleAssignment roleAssignment;
foreach (SPPrincipal smith = web.SiteGroups)
{
    roleAssignment = new SPRoleAssignment(smith);
    roleAssignment.RoleDefinitionBindings.Add(role);
    folder.RoleAssignments.Remove(smith);
    folder.RoleAssignments.Add(roleAssignment);
}
folder.SystemUpdate(false);
Run Code Online (Sandbox Code Playgroud)

如果您只想为组的子集执行此操作,则应定义所有这些组的名称列表,执行a foreach (string groupName in groupList),并作为循环的第一行指定SPPrincipal smith = web.SiteGroups[groupName];.