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.
  1. using System;
  2. using OfficeDevPnP.Core;
  3. using OfficeDevPnP.Core.Pages;
  4. using Microsoft.SharePoint.Client;
  5. using System.Security;
  6. namespace ConsoleApp4 {
  7. class Program {
  8. static void Main(string[] args) {
  9. Web web = null;
  10. List list = null;
  11. ClientContext ctx = null;
  12. ObjectSharingInformation sharingInfo = null;
  13. string siteUrl = string.Empty;
  14. string userName = string.Empty;
  15. string password = string.Empty;
  16. //Site Url to scan
  17. siteUrl = "https://softreetechnologytesting.sharepoint.com/sites/BibhutiTestSite";
  18. //Login User name
  19. userName = "[email protected]";
  20. //Login password
  21. password = "Softree2016";
  22. //Creating SharePoint Client Context
  23. using(ctx = new ClientContext(siteUrl)) {
  24. try {
  25. SecureString passWord = new SecureString();
  26. foreach(char c in password.ToCharArray())
  27. passWord.AppendChar(c);
  28. ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);
  29. web = ctx.Web;
  30. //loading sharepoint web instance
  31. ctx.Load(web);
  32. ctx.ExecuteQueryRetry();
  33. //get list by using list name
  34. list = web.Lists.GetByTitle("Documents");
  35. // This will get first 100 items from the list including folders.
  36. CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
  37. ListItemCollection items = list.GetItems(query);
  38. // Retrieve items from List
  39. ctx.Load(items, Itms => Itms.Include(Itm => Itm.Id));
  40. ctx.ExecuteQuery();
  41. if (items != null && items.Count > 0) {
  42. foreach(ListItem item in items) {
  43. try {
  44. sharingInfo = ObjectSharingInformation.GetObjectSharingInformation(ctx, item, false, true, false, true, true, true, true);
  45. ctx.Load(sharingInfo);
  46. ctx.ExecuteQuery();
  47. // Getting Anonymous Edit Link from sharing object
  48. string anonymousEditLink = sharingInfo.AnonymousEditLink;
  49. // Getting Anonymous View Link from sharing object
  50. string anonymousViewLink = sharingInfo.AnonymousViewLink;
  51. if (sharingInfo != null && sharingInfo.SharedWithUsersCollection != null) {
  52. // Looping all shared users information from ObjectSharingInformation
  53. foreach(ObjectSharingInformationUser sharingUser in sharingInfo.SharedWithUsersCollection) {
  54. try {
  55. if (!string.IsNullOrEmpty(sharingUser.LoginName)) {
  56. string sharingUserLoginName = sharingUser.LoginName;
  57. Console.WriteLine("Shared User Login Name: " + sharingUserLoginName);
  58. }
  59. } catch (Exception ex) {}
  60. }
  61. }
  62. if (sharingInfo != null && sharingInfo.SharingLinks != null) {
  63. // Looping all sharing links from ObjectSharingInformation
  64. foreach(SharingLinkInfo sharingLinkInfo in sharingInfo.SharingLinks) {
  65. try {
  66. if (!string.IsNullOrEmpty(sharingLinkInfo.Url)) {
  67. string sharingLink = sharingLinkInfo.Url;
  68. Console.WriteLine("Sharing Link Url: " + sharingLink);
  69. }
  70. } catch (Exception ex) {}
  71. }
  72. }
  73. } catch (Exception ex) {}
  74. }
  75. }
  76. Console.ReadLine();
  77. } catch (Exception ex) {}
  78. }
  79. }
  80. }
  81. }
To verify the retrieved shared links' value, you can compare those with the original ones.