We can get the 'AnonymousEditLink', 'AnonymousViewLink', and 'SharedWithUsersCollection' details using 'ObjectSharingInformation'.
In my below example, I have mentioned all the steps followed to retrieve all sharing link details and Shared User details for a SharePoint list item using CSOM programmatically. I have configured both 'AnonymousEditLink', 'AnonymousViewLink' for the item used in the below example.
I have used a simple console application to get the information of all sharing links for an SP list item. You can also get information like, sharing link creating date, expiration date, and many other properties by using 'SharingLinkInfo' object.
The code block for this is mentioned below.
- using System;
- using OfficeDevPnP.Core;
- using OfficeDevPnP.Core.Pages;
- using Microsoft.SharePoint.Client;
- using System.Security;
- namespace ConsoleApp4 {
- class Program {
- static void Main(string[] args) {
- Web web = null;
- List list = null;
- ClientContext ctx = null;
- ObjectSharingInformation sharingInfo = null;
- string siteUrl = string.Empty;
- string userName = string.Empty;
- string password = string.Empty;
-
- siteUrl = "https://softreetechnologytesting.sharepoint.com/sites/BibhutiTestSite";
-
- userName = "[email protected]";
-
- password = "Softree2016";
-
- using(ctx = new ClientContext(siteUrl)) {
- try {
- SecureString passWord = new SecureString();
- foreach(char c in password.ToCharArray())
- passWord.AppendChar(c);
- ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);
- web = ctx.Web;
-
- ctx.Load(web);
- ctx.ExecuteQueryRetry();
-
- list = web.Lists.GetByTitle("Documents");
-
- CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
- ListItemCollection items = list.GetItems(query);
-
- ctx.Load(items, Itms => Itms.Include(Itm => Itm.Id));
- ctx.ExecuteQuery();
- if (items != null && items.Count > 0) {
- foreach(ListItem item in items) {
- try {
- sharingInfo = ObjectSharingInformation.GetObjectSharingInformation(ctx, item, false, true, false, true, true, true, true);
- ctx.Load(sharingInfo);
- ctx.ExecuteQuery();
-
- string anonymousEditLink = sharingInfo.AnonymousEditLink;
-
- string anonymousViewLink = sharingInfo.AnonymousViewLink;
- if (sharingInfo != null && sharingInfo.SharedWithUsersCollection != null) {
-
- foreach(ObjectSharingInformationUser sharingUser in sharingInfo.SharedWithUsersCollection) {
- try {
- if (!string.IsNullOrEmpty(sharingUser.LoginName)) {
- string sharingUserLoginName = sharingUser.LoginName;
- Console.WriteLine("Shared User Login Name: " + sharingUserLoginName);
- }
- } catch (Exception ex) {}
- }
- }
- if (sharingInfo != null && sharingInfo.SharingLinks != null) {
-
- foreach(SharingLinkInfo sharingLinkInfo in sharingInfo.SharingLinks) {
- try {
- if (!string.IsNullOrEmpty(sharingLinkInfo.Url)) {
- string sharingLink = sharingLinkInfo.Url;
- Console.WriteLine("Sharing Link Url: " + sharingLink);
- }
- } catch (Exception ex) {}
- }
- }
- } catch (Exception ex) {}
- }
- }
- Console.ReadLine();
- } catch (Exception ex) {}
- }
- }
- }
- }
To verify the retrieved shared links' value, you can compare those with the original ones.