我使用以下代码创建了一个文件夹:
context.Credentials = new SharePointOnlineCredentials(username, password);
//login in to SharePoint online
context.ExecuteQuery();
//OneDrive is acctually a Document list
List docs = context.Web.Lists.GetByTitle("Documents");
context.ExecuteQuery();
context.Web.Folders.Add("https://xxx-my.sharepoint.com/personal/xx_xx-xx_xx/Documents/myforder1");
context.ExecuteQuery();
一切正常
现在我想分享这个文件夹。
有没有人知道如何在c#中使用csom在onedrive中共享文件夹?
答案 0 :(得分:1)
以下方法可用于共享List Item
对象:
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();
}
示例强>
以下示例演示了如何向每个用户授予Read
权限(我们在SPO中使用名为Everyone
的安全组来实现此目的)
using(var ctx = new ClientContext(webUrl))
{
ctx.Credentials = new SharePointOnlineCredentials(userName,securePassword);
var list = ctx.Web.Lists.GetByTitle(listTitle);
var listItem = list.GetItemById(listItemId); //get Folder item by Id
var everyoneSecGroup = ctx.Web.SiteUsers.GetById(4); //get Everyone security group
ShareListItem(listItem,everyoneSecGroup, "Read");
}
<强>结果强>
<强>更新强>
如何枚举角色定义和打印权限名称:
var roleDefinitions = ctx.Site.RootWeb.RoleDefinitions;
ctx.Load(roleDefinitions);
ctx.ExecuteQuery();
foreach (var roleDef in roleDefinitions)
{
Console.WriteLine(roleDef.Name);
}