Condividi tramite


Listing the permissions of every document in a document library

Here is a code sample I wrote to list every user/group who has permissions on each document in a document library in Office 365 using CSOM

  using System;
 using System.Net;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using Microsoft.SharePoint.Client;
 
 namespace ConsoleApplication1
 {
 class Program
 {
 static void Main(string[] args)
 {
 string siteUrl = "https://<your sharepoint site URL>/";
 
 ClientContext clientContext = new ClientContext(siteUrl);
 System.Security.SecureString password = new System.Security.SecureString();
 password.InsertAt(0, 'p');
 password.InsertAt(1, 'a');
 password.InsertAt(2, 's');
 password.InsertAt(3, 's');
 password.InsertAt(4, '@');
 password.InsertAt(5, 'w');
 password.InsertAt(6, 'o');
 password.InsertAt(7, 'r');
 password.InsertAt(8, 'd');
 password.InsertAt(9, '1');
 
 clientContext.Credentials = new SharePointOnlineCredentials("your office 365 UPN", password);
 
 Web site = clientContext.Web;
 List targetList = site.Lists.GetByTitle("Documents");
 
 clientContext.Load(site);
 clientContext.Load(targetList);
 clientContext.ExecuteQuery();
 ListItem li = null;
 RoleAssignmentCollection rac;
 for (int x = 1; x <= targetList.ItemCount; x++)
 {
 
 clientContext.Load(targetList);
 clientContext.ExecuteQuery();
 
 li = targetList.GetItemById(x);
 clientContext.Load(li.File);
 clientContext.ExecuteQuery();
 
 Console.WriteLine(li.File.Name);
 
 rac = targetList.GetItemById(x).RoleAssignments;
 
 clientContext.Load(rac);
 clientContext.ExecuteQuery();
 RoleAssignment ra = null;
 if (rac.AreItemsAvailable)
 {
 for (int i = 0; i < rac.Count; i++)
 {
 ra = rac.ElementAt(i);
 
 clientContext.Load(ra.Member);
 clientContext.ExecuteQuery();
 
 Console.WriteLine(ra.Member.LoginName);
 }
 }
 
 Console.WriteLine('\n');
 }
 }
 }
 }

 

ConsoleApplication1.zip