Retrieve Sharing Links Information For A SharePoint Item Using CSOM

There is no direct method present in item object to get /retrieve the sharing links information, we can get item sharing links information by using ‘ObjectSharingInformation’ class. We can get the ‘AnonymousEditLink’, ‘AnonymousViewLink’ and ‘SharedWithUsersCollection’ details using ‘ObjectSharingInformation’.

In my below example I have mentioned all the steps how to retrieve all sharing links 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 all sharing links information 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 = "";  
            //Login User name  
            userName = "";  
            //Login password  
            password = "Softree2016";  
            //Creating SharePoint Client Context  
            using(ctx = new ClientContext(siteUrl)) {  
                try {  
                    SecureString passWord = new SecureString();  
                    foreach(char c in password.ToCharArray())  
                    ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);  
                    web = ctx.Web;  
                    //loading sharepoint web instance  
                    //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));  
                    if (items != null && items.Count > 0) {  
                        foreach(ListItem item in items) {  
                            try {  
                                sharingInfo = ObjectSharingInformation.GetObjectSharingInformation(ctx, item, false, true, false, true, true, true, true);  
                                // 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) {}  
                } catch (Exception ex) {}  

To verify the retrieved shared links’ value, you can compare those with the original ones.

This solution is brought to you by our SharePoint professionals.

Softree Technology employs SharePoint consultants; we are a technology services provider with the aim to help companies achieve exceptional performance through SharePoint. Our dedicated team of SharePoint consultants has the right bent of mind to understand and execute customer requirements.

Be it SPFx or SharePoint add-in developments, SharePoint 2019 developments, web part developments, migrating from SharePoint 2010/2013 to SharePoint 2013/2016/Office 365, Office 365, SharePoint hosted apps development or something else in SharePoint, we strive to deliver the best

Tags: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *