设置文件夹权限 [英] Set Folder Permissions

查看:105
本文介绍了设置文件夹权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码在创建文件夹时工作良好:

I have the following code which works fine when creating folders:

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

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

I understand how to change the permissions of everything within a library for example:

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

但是我不想更改所有权限,我只想设置我创建的新文件夹的权限。

But I don't want to change the permissions for everything, I only want to set the permissions for the new folders I am creating.

所以我尝试了此操作:

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中不起作用,因为直到Sharepoint 2013为止,ListItemAllFields 才引入SharePoint。

But this code does not work in SharePoint 2010 because ListItemAllFields was not introduced to SharePoint until Sharepoint 2013.

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

My question is, how do I set permissions for these folders so only a specific user has access to them? Ideally, if this can be set while creating the folders, that would be great. If that is not possible, then settings the permissions after creating the folders will be fine.

应用程序是WinForm。

The application is a WinForm.

推荐答案

正如您所说,由于 ListItemAllFields 允许您向特定用户授予权限,是在SharePoint 2013中引入的,您无法利用它。

As you said since ListItemAllFields that would allow you to give permissions to a specific user was only introduced in SharePoint 2013, you can't leverage it.

如果您结帐此答案但是,通过安装此插件,并创建您自己的 ListExtensions 类,因为它仍然不会公开。

If you checkout this answer however, you can see that there is a way around it, by installing this plugin and create your own ListExtensions class, since it would still not be exposed.


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 是其中最关键的部分,可让您使用类似的东西来更新权限

Where the CamlQuery is the most critical part of it, and would allow you to update the permissions by using something like

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

这篇关于设置文件夹权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆