How to automatically get sharepoint links (Link for 'People with existing access') to all files in a folder?

Viewed 913

I've got a load of files in a Sharepoint folder and I need to share the link to some of the individual files with someone external to the organisation who has access to the folder. I can obtain the link by manually selecting each file, change the link option to 'People with existing access' and click Apply. This would give me a link that looks something like: <https://.sharepoint.com/:p:/r/sites/Marketing/Shared%20Documents/Branding%20Elements.pptx?d=w1bae13735fdb4b1d9dd0ae217bc2d911&csf=1&e=aojURR>

However, doing this for hundereds of files isn't practical. Can someone please help with an easier/quicker solution/automation?

Thanks a ton!

1 Answers

You can use the SharePoint client object model (CSOM) to get the share links using your user credentials (if the site has MFA enabled you'll need to use app credentials as described here):

using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using System;
using System.Security;
static void Main()
{
    string siteUrl = "https://contoso.sharepoint.com/sites/demo";

    SecureString password = new SecureString();
    foreach (char c in "password".ToCharArray()) password.AppendChar(c);

    using (var cc = new AuthenticationManager().GetSharePointOnlineAuthenticatedContextTenant(siteUrl, "username", password))
    {
        string folderUrl = @"https://contoso.sharepoint.com/sites/demo/Shared Documents/TestFolder";
        Uri folderUri = new Uri(folderUrl);

        Folder folder = cc.Web.GetFolderByServerRelativeUrl(folderUri.AbsolutePath);
        cc.ExecuteQuery();

        cc.Load(folder.Files);
        cc.ExecuteQuery();
        FileCollection fileCol = folder.Files;
        foreach (File file in fileCol)
        {
            Console.WriteLine(file.LinkingUrl);
        }
    }
}

The above code should give you the "People with existing access" link. The code below shows how to create other types of links:

using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using System;
using System.Security;
static void Main()
{
    string siteUrl = "https://contoso.sharepoint.com/sites/demo";
    SecureString password = new SecureString();
    foreach (char c in "password".ToCharArray()) password.AppendChar(c);

    //Use the commented out line for App Only Authentication
    //using (var cc = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, "ClientID", "ClientSecret"))
    using (var cc = new AuthenticationManager().GetSharePointOnlineAuthenticatedContextTenant(siteUrl, "username", password))
    {
        string fileUrl = @"https://contoso.sharepoint.com/sites/demo/Shared Documents/TestFolder/File.xlsx";
        Uri fileUri = new Uri(fileUrl);

        Microsoft.SharePoint.Client.File file = cc.Web.GetFileByServerRelativeUrl(fileUri.AbsolutePath);
        cc.Load(file);

        //Existing Access Link (i.e. "People with existing access")
        //file.LinkingUrl

        //Organization Link (i.e. "People in org with link")
        bool isEdit = true;
        var orgEditLink = Microsoft.SharePoint.Client.Web.CreateOrganizationSharingLink(cc, fileUrl, isEdit);

        //Anonymous Link (i.e. "Anyone with the link")
        ClientResult<String> anonEditLink = Microsoft.SharePoint.Client.Web.CreateAnonymousLink(cc, fileUrl, isEdit);
        string anonEditLink2 = cc.Web.CreateAnonymousLinkForDocument(fileUrl, ExternalSharingDocumentOption.Edit);

        //Anonymous Link with Expiration (not available in GUI)
        DateTime five_days = DateTime.Now.AddDays(5);
        ClientResult<String> anonExpEditLink = Microsoft.SharePoint.Client.Web.CreateAnonymousLinkWithExpiration(cc, fileUrl, isEdit, five_days.ToString("yyyy-MM-ddThh:mm:ssZ"));
        string anonExpEditLink2 = cc.Web.CreateAnonymousLinkWithExpirationForDocument(fileUrl, ExternalSharingDocumentOption.Edit, five_days);

        //User Share Link (i.e. "People you specify can edit")
        String target_email = "email@example.com";
        bool send_email = false;
        SharingResult userEditLink = cc.Web.ShareDocument(fileUrl, target_email, ExternalSharingDocumentOption.Edit, send_email);

        cc.ExecuteQuery();

        Console.WriteLine(file.LinkingUrl);
        Console.WriteLine(orgEditLink.Value);
        Console.WriteLine(anonEditLink.Value);
        Console.WriteLine(anonEditLink2);
        Console.WriteLine(anonExpEditLink.Value);
        Console.WriteLine(anonExpEditLink2);
        foreach (SPInvitationCreationResult invuser in userEditLink.InvitedUsers)
        {
            Console.WriteLine(invuser.InvitationLink);
        }

    }

}
Related