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;
-
-
-
- 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)
- {
-
- 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();
-
-
- 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;
- }
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.
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.
- ListItemCollection docs = ctx.Web.Lists.GetByTitle(libTitle).GetItems(CamlQuery.CreateAllItemsQuery());
- ctx.Load(docs, i => i.Include(u => u.HasUniqueRoleAssignments));
- ctx.ExecuteQuery();
- foreach (var doc in docs)
- {
- if (doc.HasUniqueRoleAssignments)
- {
-
- doc.ResetRoleInheritance();
- }
- }
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: