Generating Sharing Links Report And Removing Sharing Links Using SharePoint Online CSOM

In the previous post, we have seen how to see the sharing links and delete them from the browser, which is not useful when we want to generate a report of all the sharing links within a document library with high volume. Let us explore how to use the SharePoint CSOM code to do the same operations for larger data sets.
 

Generate a Report of all the Sharing Links created

 
This is a sample console app code which assumes you have installed the ‘Microsoft.SharePointOnline.CSOM’ nugget package (I am using version - 16.1.9021.1200)
 
This snippet reads all the items within a document library and checks if the document has unique permissions or not. If a document has unique permissions we then use the Web.GetObjectSharingSettings method to fetch the sharing details of a document
  1. static void Main(string[] args)  
  2.         {  
  3.             string siteUrl = "https://<tenant-name>.sharepoint.com/sites/contosoteam";  
  4.             string libTitle = "Documents";  
  5.   
  6.             string username = "<username>@<tenant-name>.onmicrosoft.com";  
  7.             string pwd = "spo password";  
  8.             SecureString password = new SecureString();  
  9.             foreach (char c in pwd)  
  10.             {  
  11.                 password.AppendChar(c);  
  12.             }  
  13.             SharePointOnlineCredentials creds = new SharePointOnlineCredentials(username, password);  
  14.             ClientContext ctx = new ClientContext(siteUrl);  
  15.             ctx.Credentials = creds;  
  16.   
  17.             // Fetch the details of all files from the document library.  
  18.             // For very large lists, change this code to include pagination and to fetch list items in batches  
  19.             ListItemCollection docs = ctx.Web.Lists.GetByTitle(libTitle).GetItems(CamlQuery.CreateAllItemsQuery());  
  20.             ctx.Load(docs, i => i.Include(u => u.HasUniqueRoleAssignments,  
  21.                 u => u["EncodedAbsUrl"]  
  22.                 ));  
  23.             ctx.ExecuteQuery();  
  24.   
  25.   
  26.             foreach (var doc in docs)  
  27.             {  
  28.                 if (doc.HasUniqueRoleAssignments)  
  29.                 {  
  30.                     // Find all the documents which have unique permissions  
  31.                     Console.WriteLine(doc["EncodedAbsUrl"]);  
  32.                     GetSharingLinks(ctx, doc["EncodedAbsUrl"].ToString());  
  33.                     Console.WriteLine("---------------------------------------------------");  
  34.                 }  
  35.             }  
  36.         }  
  37.   
  38.   
  39.         static void GetSharingLinks(ClientContext clientContext, string fullDocumentUrl)  
  40.         {  
  41.             ObjectSharingSettings info = Web.GetObjectSharingSettings(clientContext, fullDocumentUrl, 0, true);  
  42.             clientContext.Load(info.ObjectSharingInformation);  
  43.             clientContext.ExecuteQuery();  
  44.   
  45.             //This property has all the sharing links for a given document.  
  46.             var links = info.ObjectSharingInformation.SharingLinks;  
  47.             Console.ForegroundColor = ConsoleColor.Yellow;  
  48.             Console.WriteLine("Below are the sharing links");  
  49.             foreach (var url in info.ObjectSharingInformation.SharingLinks) {  
  50.                 if(!string.IsNullOrEmpty(url.Url))  
  51.                     Console.WriteLine("{0}", url.Url);  
  52.                   
  53.             }  
  54.             Console.ForegroundColor = ConsoleColor.White;  
  55.         } 
The ObjectSharingInformation class of the SharePoint CSOM have some useful properties which can be used to find the sharing details. Two important properties which can be used for generating reports are the ‘ObjectSharingInformation. SharedWithUsersCollection’, which shows the list of users for whom the sharing links are accessible and ‘ObjectSharingInformation.SharingLinks’, which gives the sharing link URLs for a given document.
 
Generating Sharing Links Report And Removing Sharing Links Using SharePoint Online CSOM
 
This snippet shows the list of documents which have Sharing Links created and the list of sharing links for it. This snippet can be extended to generate reports on which all users created the sharing links, who all are invited to view or edit a document etc..
 

Delete all the Sharing Links within a library

 
As mentioned in the previous post, the recommended way to remove all sharing links on a document library is to delete the unique permissions on the document. Doing this not only removes the unique permissions but also deletes all the sharing links on the document. The below snippet can be used to find all documents with unique permissions and reset the permissions, and in turn remove all the sharing links.
  1. ListItemCollection docs = ctx.Web.Lists.GetByTitle(libTitle).GetItems(CamlQuery.CreateAllItemsQuery());  
  2. ctx.Load(docs, i => i.Include(u => u.HasUniqueRoleAssignments));  
  3. ctx.ExecuteQuery();    
  4. foreach (var doc in docs)  
  5. {  
  6.     if (doc.HasUniqueRoleAssignments)  
  7.     {  
  8.         // Reset Unique permissions. This will also delete the sharing links of the document  
  9.         doc.ResetRoleInheritance();  
  10.     }  

Hope this series of posts about Sharing Links is useful to understand about the relation between Sharing Links and Unique Permissions and plan on how to use the CSOM code to effectively generate reports and remove all the sharing links within a document library when needed.
 
Here are the links to the previous posts for reference: