【问题标题】:Set Folder Permissions设置文件夹权限
【发布时间】:2017-05-16 14:58:16
【问题描述】:

我有以下代码在创建文件夹时可以正常工作:

public void CreateFolders()
{
    _SharePoint.ClientContext _ClientContext = new _SharePoint.ClientContext("https://sharepoint.oshiro.com/sites/oshirodev/");
    _ClientContext.Credentials = new NetworkCredential("user", "pass", "oshiro.com");

    var _web = _ClientContext._web;
    var _Root = _web.Lists.GetByTitle("Library1");
    var _folder1 = _Root.RootFolder.Folders.Add("Folder1");
    var _subfolder1 = _folder1.Folders.Add("SubFolder1");
    _folder1.Update();
    _subfolder1.Update();

    var _folder2 = _Root.RootFolder.Folders.Add("Folder2");
    var _subfolder2 = _folder2.Folders.Add("SubFolder2");
    _folder1.Update();
    _subfolder1.Update();

    _ClientContext.ExecuteQuery();
}

我了解如何更改库中所有内容的权限,例如:

public void ChangeFldPerms()
{
    _SharePoint.ClientContext _ClientContext = new _SharePoint.ClientContext("https://sharepoint.oshiro.com/sites/oshirodev/");
    _ClientContext.Credentials = new NetworkCredential("user", "pass", "oshiro.com");

    _SharePoint.Principal _user = _ClientContext.Web.EnsureUser(@"oshiro\tom");
    _SharePoint.List _item = _ClientContext.Web.Lists.GetByTitle("Library1");
    var roleDefinition = _ClientContext.Site.RootWeb.RoleDefinitions.GetByType(_SharePoint.RoleType.Reader);  //get Reader role
    var roleBindings = new _SharePoint.RoleDefinitionBindingCollection(_ClientContext) { roleDefinition };
    _item.BreakRoleInheritance(true, false);
    _item.RoleAssignments.Add(_user, roleBindings);
    _ClientContext.ExecuteQuery();
}

但我不想更改所有内容的权限,我只想为我正在创建的新文件夹设置权限。

所以我尝试了这个:

public void ChangeFldPerms2()
{
    SharePoint.ClientContext _ClientContext = new _SharePoint.ClientContext("https://sharepoint.oshiro.com/sites/oshirodev/");
    _ClientContext.Credentials = new NetworkCredential("user", "pass", "oshiro.com");

    _SharePoint.Principal _user = _ClientContext.Web.EnsureUser(@"oshiro\tom");
    _SharePoint.Folder _folder = _ClientContext.Web.GetFolderByServerRelativeUrl("/sites/oshirodev/Library1/Folder1");

    var roleDefinition = _ClientContext.Site.RootWeb.RoleDefinitions.GetByType(_SharePoint.RoleType.Reader);
    var roleBindings = new _SharePoint.RoleDefinitionBindingCollection(_ClientContext) { roleDefinition };

    _folder.ListItemAllFields.BreakRoleInheritance(true, false);
    _folder.ListItemAllFields.RoleAssignments.Add(user, roleBindings);

    _ClientContext.ExecuteQuery();
}

但此代码在 SharePoint 2010 中不起作用,因为 ListItemAllFields 直到 Sharepoint 2013 才引入 SharePoint。

我的问题是,如何设置这些文件夹的权限,以便只有特定用户才能访问它们?理想情况下,如果可以在创建文件夹时进行设置,那就太好了。如果不行,那么在创建文件夹后设置权限就可以了。

应用程序是一个 WinForm。

【问题讨论】:

  • 明确地说,您是否期望提供的代码仅更改一个文件夹的权限?它有一个注释掉的行,它获取对文件夹的引用,但不做任何事情;其余代码显式更改库的权限,而不是文件夹的权限。
  • 文件夹 1、文件夹 2 和文件夹 3 在 Library1 中吗?这将更改整个文档库而不是文件夹 1 的权限。
  • 这个问题已经被问及解决了here. ------------------------------- ----------------------

标签: c# .net c#-4.0 sharepoint-2010 .net-4.0


【解决方案1】:

正如您所说,因为 ListItemAllFields 允许您向特定用户授予权限仅在 SharePoint 2013 中引入,因此您无法利用它。

但是,如果您检查 this answer,您会发现有一种解决方法,通过安装 this plugin 并创建您自己的 ListExtensions 类,因为它仍然不会暴露。

static class ListExtensions
{
    /// <summary>
    /// Load List Item by Url 
    /// </summary>
    /// <param name="list"></param>
    /// <param name="url"></param>
    /// <returns></returns>
    public static ListItem LoadItemByUrl(this List list, string url)
    {
        var context = list.Context;
        var query = new CamlQuery
        {
            ViewXml = String.Format("<View><RowLimit>1</RowLimit><Query><Where><Eq><FieldRef Name='FileRef'/><Value> Type='Url'>{0}</Value></Eq></Where></Query></View>", url),
        };
        var items = list.GetItems(query);
        context.Load(items);
        context.ExecuteQuery();
        return items.Count > 0 ? items[0] : null;
    }
}

CamlQuery 是其中最关键的部分,它允许您使用类似的东西来更新权限

public void ChangeFldPerms2()
{
    SharePoint.ClientContext _ClientContext = new _SharePoint.ClientContext("https://sharepoint.oshiro.com/sites/oshirodev/");
    _ClientContext.Credentials = new NetworkCredential("user", "pass", "oshiro.com");
    _SharePoint.Principal _user = _ClientContext.Web.EnsureUser(@"oshiro\tom");

    var list = _ClientContext.Web.Lists.GetByTitle("Library1");
    var folderItem = list.LoadItemByUrl("/sites/oshirodev/Library1/Folder1");
    var roleDefinition = _ClientContext.Site.RootWeb.RoleDefinitions.GetByType(_SharePoint.RoleType.Reader);
    var roleBindings = new _SharePoint.RoleDefinitionBindingCollection(_ClientContext) { roleDefinition };
    folderItem.BreakRoleInheritance(true, false);
    folderItem.RoleAssignments.Add(user, roleBindings);

    _ClientContext.ExecuteQuery();
}

【讨论】:

    【解决方案2】:

    另一种方式。如果您可以访问应用服务器,您可以授予创建 iis 应用程序池的权限。

    【讨论】:

      猜你喜欢
      • 2014-09-28
      • 2015-10-18
      • 2023-03-28
      • 2017-10-05
      • 2013-12-22
      • 2012-03-02
      • 2012-09-04
      • 1970-01-01
      • 2010-09-19
      相关资源
      最近更新 更多