如何在 Sharepoint 中隐藏文件夹

发布于 2024-08-30 17:18:15 字数 199 浏览 2 评论 0原文

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

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

文件夹.Update();

What else I need to add to following code to make sure the folder is hidden or read only?

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

folder.Update();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蛮可爱 2024-09-06 17:18:15

与实际列表不同,文件夹不能按正常意义上“隐藏”。也就是说,您无法使其对 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);

编辑

分配多个实例时,这取决于您是执行所有组还是仅执行子集。我将列出所有组的代码,因为这是您直接询问的。

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);

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

Unlike actual lists, a Folder cannot be made "hidden" in the normal sense. That is, you cannot make it invisible to the UI, but still have all the normal access levels associated with it (for example, the traditional Workflow History list is hidden in this fashion). In a way, your goals of hiding and making it read-only coincide when it comes to folders, as both are accomplished by Permissions. You can technically reduce the visibility of a particular item through a complicated view filter, but that would only change things on the view level.

The visibility of a list item, which a Folder is, is based on whether you have permissions on that item. The only way to truly "hide" a list item is to remove all permissions of a person from that list item. In order to make a folder read-only, you would remove all Add, Edit, and Delete permissions from the folder. If you still have the default SharePoint permission levels (Full Control, Design, Contribute, and Read), then simply reassigning everything to Read will accomplish this goal.

Here is an example of code that I use to make a discussion (which is a folder) hidden from all users except those in the group "Engineers", to whom it is read-only.

// 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);

EDIT

When assigning multiple instances, it depends on whether you are doing all of the groups or just a subset. I'll list the code for all groups since that is what you are directly asking.

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);

If you wanted to do it only for a subset of groups, you would define a list of the names of all of those groups, do a foreach (string groupName in groupList), and as the first line of the loop, specify SPPrincipal smith = web.SiteGroups[groupName];.

杀手六號 2024-09-06 17:18:15

谢谢短号。这是使文件夹只读的方法。再次感谢您的帮助...

    private void MakeFolderReadOnly(SPWeb web,SPListItem folder)
    {
        if (!folder.HasUniqueRoleAssignments)
        {
            //Setting false erases all permissions, so now the folder is hidden from everyone.
            folder.BreakRoleInheritance(false);
            folder.SystemUpdate(false);
        }

        SPRoleDefinition role = web.RoleDefinitions["Read"];
        SPGroupCollection collGroups = web.SiteGroups;
        foreach(SPGroup group in collGroups)
        {
            SPPrincipal principal = group;
            SPRoleAssignment roleAssign = new SPRoleAssignment(principal);
            roleAssign.RoleDefinitionBindings.Add(role);
            folder.RoleAssignments.Remove(principal);
            folder.RoleAssignments.Add(roleAssign);
            folder.SystemUpdate(false);           
        }

    }

Thank you Cornet. Here is the method that makes a folder read only. THANKS AGAIN FOR YOUR HELP...

    private void MakeFolderReadOnly(SPWeb web,SPListItem folder)
    {
        if (!folder.HasUniqueRoleAssignments)
        {
            //Setting false erases all permissions, so now the folder is hidden from everyone.
            folder.BreakRoleInheritance(false);
            folder.SystemUpdate(false);
        }

        SPRoleDefinition role = web.RoleDefinitions["Read"];
        SPGroupCollection collGroups = web.SiteGroups;
        foreach(SPGroup group in collGroups)
        {
            SPPrincipal principal = group;
            SPRoleAssignment roleAssign = new SPRoleAssignment(principal);
            roleAssign.RoleDefinitionBindings.Add(role);
            folder.RoleAssignments.Remove(principal);
            folder.RoleAssignments.Add(roleAssign);
            folder.SystemUpdate(false);           
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文