0
我想在OneDrive中的文件夾上設置共享權限。我知道有關於ListItems的一篇文章,但我需要在文件夾級別。首先,這是可能的還是我在浪費我的時間?我試過以下內容:CSOM在OneDrive文件夾上共享設置
我能夠獲得網站對象,但我無法獲取文件夾以便共享它。該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, "[email protected]", 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();
}