CSOM在OneDrive文件夹上设置共享

时间:2016-03-02 19:30:47

标签: c# permissions onedrive csom

我想在OneDrive中设置文件夹的共享权限。我知道有关于ListItems的帖子,但我需要在文件夹级别。首先,这是可能还是我在浪费时间?我尝试了以下内容:

我能够获取网站对象,但我无法获取该文件夹以便共享它。 Web对象没有可用于枚举的文件夹。它说它没有初始化。以下代码成功运行,但文件夹对象无效:

static void Main(string[] args)
    {
        var webUrl = "https://tenant-my.sharepoint.com/personal/me_tenant_com";
        var userName = "me";
        string securePassword = "mypassword";
        SecureString sec_pass = new SecureString();
        Array.ForEach(securePassword.ToArray(), sec_pass.AppendChar);

        using (var ctx = new ClientContext(webUrl))
        {
            ctx.Credentials = new SharePointOnlineCredentials(userName, sec_pass);

            var web = ctx.Web;
            ClientResult<Microsoft.SharePoint.Client.Utilities.PrincipalInfo> persons = Microsoft.SharePoint.Client.Utilities.Utility.ResolvePrincipal(ctx, ctx.Web, "dpunchak@AvvenireInc.com", Microsoft.SharePoint.Client.Utilities.PrincipalType.User, Microsoft.SharePoint.Client.Utilities.PrincipalSource.All, null, true);
            ctx.ExecuteQuery();

            var folder = ctx.Web.GetFolderByServerRelativeUrl("/documents/Test Folder");
            Microsoft.SharePoint.Client.Utilities.PrincipalInfo person = persons.Value;

            //ShareListItem(folder, person, "Read");
        }
    }

    public static void ShareListItem(ListItem listItem, Principal principal, string permissionLevelName)
    {
        var ctx = listItem.Context as ClientContext;
        var roleDefinition = ctx.Site.RootWeb.RoleDefinitions.GetByName(permissionLevelName);
        listItem.BreakRoleInheritance(true, false);
        var roleBindings = new RoleDefinitionBindingCollection(ctx) { roleDefinition };
        listItem.RoleAssignments.Add(principal, roleBindings);
        ctx.ExecuteQuery();


    }

1 个答案:

答案 0 :(得分:1)

我认为你必须将folder.ListItemAllFields属性传递给ShareListItem()

为避免收集尚未初始化错误,您可以尝试将RoleAssignments.Add()置于ctx.ExecuteQuery()内:

ctx.ExecuteQuery(listItem.RoleAssignments.Add(principal, roleBindings);