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;
- //Site Url to scan
- siteUrl = "https://softreetechnologytesting.sharepoint.com/sites/BibhutiTestSite";
- //Login User name
- userName = "[email protected]";
- //Login password
- password = "Softree2016";
- //Creating SharePoint Client Context
- 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;
- //loading sharepoint web instance
- ctx.Load(web);
- ctx.ExecuteQueryRetry();
- //get list by using list name
- list = web.Lists.GetByTitle("Documents");
- // This will get first 100 items from the list including folders.
- CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
- ListItemCollection items = list.GetItems(query);
- // Retrieve items from List
- 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();
- // Getting Anonymous Edit Link from sharing object
- string anonymousEditLink = sharingInfo.AnonymousEditLink;
- // Getting Anonymous View Link from sharing object
- string anonymousViewLink = sharingInfo.AnonymousViewLink;
- if (sharingInfo != null && sharingInfo.SharedWithUsersCollection != null) {
- // Looping all shared users information from ObjectSharingInformation
- 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) {
- // Looping all sharing links from ObjectSharingInformation
- 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.

venkat reddyPosted Apr 8, 2022, 9:37 AM
Using OfficeDevPnP.Core; using OfficeDevPnP.Core.Pages; are not available in the nuget package manager
venkat reddyPosted Apr 8, 2022, 9:37 AM
How can I get this code, downloaded. using OfficeDevPnP.Core; using OfficeDevPnP.Core.Pages;