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
- static void Main(string[] args)
- {
- string siteUrl = "https://<tenant-name>.sharepoint.com/sites/contosoteam";
- string libTitle = "Documents";
- string username = "<username>@<tenant-name>.onmicrosoft.com";
- string pwd = "spo password";
- SecureString password = new SecureString();
- foreach (char c in pwd)
- {
- password.AppendChar(c);
- }
- SharePointOnlineCredentials creds = new SharePointOnlineCredentials(username, password);
- ClientContext ctx = new ClientContext(siteUrl);
- ctx.Credentials = creds;
- // Fetch the details of all files from the document library.
- // For very large lists, change this code to include pagination and to fetch list items in batches
- ListItemCollection docs = ctx.Web.Lists.GetByTitle(libTitle).GetItems(CamlQuery.CreateAllItemsQuery());
- ctx.Load(docs, i => i.Include(u => u.HasUniqueRoleAssignments,
- u => u["EncodedAbsUrl"]
- ));
- ctx.ExecuteQuery();
- foreach (var doc in docs)
- {
- if (doc.HasUniqueRoleAssignments)
- {
- // Find all the documents which have unique permissions
- Console.WriteLine(doc["EncodedAbsUrl"]);
- GetSharingLinks(ctx, doc["EncodedAbsUrl"].ToString());
- Console.WriteLine("---------------------------------------------------");
- }
- }
- }
- static void GetSharingLinks(ClientContext clientContext, string fullDocumentUrl)
- {
- ObjectSharingSettings info = Web.GetObjectSharingSettings(clientContext, fullDocumentUrl, 0, true);
- clientContext.Load(info.ObjectSharingInformation);
- clientContext.ExecuteQuery();
- //This property has all the sharing links for a given document.
- var links = info.ObjectSharingInformation.SharingLinks;
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("Below are the sharing links");
- foreach (var url in info.ObjectSharingInformation.SharingLinks) {
- if(!string.IsNullOrEmpty(url.Url))
- Console.WriteLine("{0}", url.Url);
- }
- Console.ForegroundColor = ConsoleColor.White;
- }


Keith VolleroPosted Jul 17, 2024, 12:18 PM
How would one write this script for SharePoint 2019 On-prem?
Azar SPosted May 27, 2020, 2:12 AM
Thanks for the info. I used Get-SPOUser -Site [root site] to list users that have access to a site and found an external user listed. Groups column has "SharingLinks.[unique id]. Flexible. [uniqueid]. Is there a way to find what was shared with the user? Or is there a way to search all items for the sharing link unique id?
Sri SunkavalliPosted Sep 6, 2019, 2:01 PM
Hi, Do you know how to remove a specific user from a Sharing link without resetting inheritance?