1

We have a requirement to retrieve all sharing links from particular document of document library via SharePoint CSOM C#. We are able to get Anonymous sharing links and Organization Sharing links using below piece of code:

var sharingInfo = ObjectSharingInformation.GetObjectSharingInformation(clientContext, listItem, false, true, false, true, true, true, true);                
clientContext.Load(sharingInfo);
clientContext.Load(sharingInfo.SharedWithUsersCollection);
clientContext.ExecuteQuery();

// Getting Anonymous Edit Link from sharing object  
string anonymousEditLink = sharingInfo.AnonymousEditLink;
Console.WriteLine(anonymousEditLink);

// Getting Anonymous View Link from sharing object  
string anonymousViewLink = sharingInfo.AnonymousViewLink;
Console.WriteLine(anonymousViewLink);


if (sharingInfo != null && sharingInfo.SharingLinks != null)
{
    // Looping all sharing links from ObjectSharingInformation  
    foreach (SharingLinkInfo sharingLinkInfo in sharingInfo.SharingLinks)
    {
        try
        {
            if (!string.IsNullOrEmpty(sharingLinkInfo.Url))
            {
                Console.WriteLine("URL: {0}", sharingLinkInfo.Url.ToString());
            }
        }catch(Exception)
        {}
    }
}

Using above code, I got the link details but it doesn't provide to whom this link is shared and who has created/shared this document link.

Can anybody please help me to retrieve these details for sharing links in SharePoint Online via CSOM C#?

1 Answer 1

1

There are 7 types of Links we have in SharePoint Online. Please see links type with Enum value as below: enter image description here

Reference: https://docs.microsoft.com/en-us/previous-versions/office/sharepoint-csom/mt684093(v=office.15)

Sharing Links list

Initially when we create a site, Sharing Links is not created by default. As soon as any document is shared from the site, the list gets created and first entry is made to the list.

Here are the important fields in the hidden list: enter image description here

AvailableLinks field stores the Share Id, Expiration Details, Role definition, inviter and invitee details and other required information in the form of json payload

Here is a sample payload:

[{"LinkKind":6,"ExpirationDateTime":null,"ExpirationModifiedDate":"\/Date(1589866980166)\/","IsActive":true,"ShareId":"6756a647-d0c0-44fd-8322-be5e87dcadd2","AuthKey":"ARiB4jWeIjWnoN5OqlXSF0k","CreatedById":14,"LastModifiedById":14,"CreatedDate":"\/Date(1589817470116)\/","LastModifiedDate":"\/Date(1589866980151)\/","ObjectType":1,"Flags":1048,"GroupId":81,"RoleDefinitionId":1073741826,"IsDeleted":false,"HasLinkClaim":false,"IsDefault":true,"Invitees":[{"Type":3,"Email":"[email protected]","InvitedBy":14,"InvitedOn":"\/Date(1589817469991)\/"},{"Type":1,"PId":61,"ShareByEmailGuest":true,"InvitedBy":14,"InvitedOn":"\/Date(1589866980088)\/"}]},


{"LinkKind":6,"ExpirationDateTime":null,"ExpirationModifiedDate":"\/Date(1589822683075)\/","IsActive":true,"ShareId":"569a7240-3017-4b3e-8580-212242c4bb0a","AuthKey":"AXsC0cf0X8-YTN8w3OvN9fk","CreatedById":14,"LastModifiedById":14,"CreatedDate":"\/Date(1589822683075)\/","LastModifiedDate":"\/Date(1589822683075)\/","ObjectType":1,"Flags":1048,"GroupId":82,"RoleDefinitionId":1073741827,"IsDeleted":false,"HasLinkClaim":false,"IsDefault":true,"Invitees":[{"Type":2,"PId":16,"InvitedBy":14,"InvitedOn":"\/Date(1589822682981)\/"}]},

{"LinkKind":6,"ExpirationDateTime":null,"ExpirationModifiedDate":"\/Date(1589826794964)\/","IsActive":true,"ShareId":"6623c477-e00e-48e2-8f3c-1750578dc59a","AuthKey":"AfmW421MMH6cQXyWKQhY_S4","CreatedById":14,"LastModifiedById":14,"CreatedDate":"\/Date(1589826794948)\/","LastModifiedDate":"\/Date(1589826794948)\/","ObjectType":1,"Flags":1048,"GroupId":84,"RoleDefinitionId":1073741832,"IsDeleted":false,"HasLinkClaim":false,"IsDefault":true,"Invitees":[{"Type":1,"PId":83,"InvitedBy":14,"InvitedOn":"\/Date(1589826794776)\/"}]}]

CSOM C# code to get this information:

using (var cc = new OfficeDevPnP.Core.AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, clientId, clientSecret))
{
    ListItemCollection temp = cc.Web.Lists.GetByTitle("Sharing Links").GetItems(CamlQuery.CreateAllItemsQuery());
    cc.Load(temp);
    cc.ExecuteQuery();

    foreach (var data in temp)
    {
        Console.WriteLine("Id: " + data.Id);
        Console.WriteLine("SharingDocId: " + data.FieldValues["SharingDocId"]);
        Console.WriteLine("AvailableLinks: " + data.FieldValues["AvailableLinks"]);
    }
}

The JSON details we get from AvailableLinks column would be as below: enter image description here

Reference:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.