Logo
  • Contact form
  • Events & slides
  • Buy me a snack
👋🏻
/
📝
All Blogs
/Use Power Automate to create sharing links
Use Power Automate to create sharing links
Use Power Automate to create sharing links

Use Power Automate to create sharing links

Table of contents

  • A quick overview
  • How to
  • Get the drive id of the library
  • Get the drive item id of the sharing item/file
  • Create sharing links
  • Company links
  • User scoped links
  • Existing access view
  • Anonymous view / edit
  • Anonymous view blocking downloads
  • Processing the response
☝
TL;DR - You can programmatically create sharing links with Power Automate for any file or item in SharePoint Online and OneDrive. You can use standard actions sending HTTP request to the SharePoint API.

A quick overview

The base of this request is the endpoint for drives from the Graph API: Share a file with a link - Microsoft Graph v1.0 | Microsoft Learn. We will use “Send HTTP Request to SharePoint” for all our actions, which means that this doesn't require premium licenses or app registrations in Entra ID. The actions of the flow will run with delegated permissions, which means that it is important that the person who runs the flow has access to the libraries and items in question.

How to

Use these headers for all requests:

image

Get the drive id of the library

The drive id is different to the library id in SharePoint. At the end it describes a unique id for that library or list (across SharePoint environments). You might have seen some of it when using Excel actions in Power Automate.

image
_api/v2.0/drives?$select=name,id

Unfortunately, the drives endpoint doesn't support filtering. This means you will have to do the filtering after getting all drives in your SharePoint site collection. We can do this by using the “Filter array” action next. Paste in the librarys display name as it is shown at the top (not the one from the URL) when you open it in SharePoint. The filter array should leave us exactly with one library.

image
//From
body('Send_an_HTTP_request_to_SharePoint_-_Get_all_drives')?['value']
//Filter Query
item()?['name']

Get the drive item id of the sharing item/file

Similarly, the item or file has a unique id (across SharePoint environments)- the drive item id. It is also a different id from the SharePoint id. The easiest is getting the id by filtering for the filename including the file extension. Im using “Document1.docx” as the file name. If your filename contains spaces simply add them, no need to escape those.

image

In the URI we will use the result of the previous action, where we filtered for the library in which the item lies. The filter array itself will return an array. Therefore, we simply use the expression first() to access the first object in that array. From that first object we want to use the id property.

first(body('Filter_array_-_Library'))?['id']

Above you can see the URI for a file that is at the top level of the library. If the file is inside a folder (TestFolder1) your URI would look like this: …root:/TestFolder1/Dokument.docx. There is different way to get to the file: Get driveItem - Microsoft Graph v1.0 | Microsoft Learn

You will be able to get the drive item id from a folder, but creating a link with it in Power Automate won’t work.

Create sharing links

Technically all parameters in the body are optional. If you leave the body empty, you will get the default settings for a link that is set by the admin center.

Company links

Can be used by everyone in the company (which means that also Copilot will find it).

image
//id in URI
body('Send_an_HTTP_request_to_SharePoint_-_Drive_Item_Id')?['id']

//body
//type can be edit or view
{
  "type": "edit",
  "scope": "organization"
}

User scoped links

These links are scoped to users, which means that only specified user will be able to use the link generated. You can either use the users email address or the objectId found in Entra ID.

Change the type to “view” or “edit” as necessary.

{
    "type": "view",
    "scope": "users",
    "recipients": [
        {
            "objectId": "Someone's objectId"
        },
        {
            "email": "Someone's email"
        }
    ],
    "sendNotification":"true"
}

Existing access view

Using the graph explorer and leaving the body empty, interestingly gets you a link with existing access. In Power Automate you get a random sharing link that is already applied to the item.

Anonymous view / edit

Anonymous links are meant to be send to external users that might not even have a Microsoft account. They can access the file by using a temporary 8 digit code they will receive via email. For anonymous links SharePoint or OneDrive needs to have anyone sharing enabled from the admin center. If there are constraints with lifetime of those links, you will need to comply with those.

“password” and “expirationDateTime” is an optional parameter that doesn’t need to be specified.

Change the type to “view” or “edit” as necessary.

{
    "type": "view",
    "scope": "anonymous",
    "password": "ThisIsMyPrivatePassword",
    "expirationDateTime": "2025-01-30T13:39:54Z",
    "retainInheritedPermissions": true
}

Anonymous view blocking downloads

{
    "type": "blocksDownload",
    "scope": "anonymous",
    "password": "ThisIsMyPrivatePassword",
    "expirationDateTime": "2025-07-01T13:39:54Z",
    "retainInheritedPermissions": true
}

Processing the response

Lets say the action that produces the link is called “Send an HTTP request to SharePoint - Create block downloads view link”. Then this would be the link you can use to send out to recipients.

body('Send_an_HTTP_request_to_SharePoint_-_Create_block_downloads_view_link')?['link']?['webUrl']

Thanks for reading! 💕

Logo

Events & slides

Buy me a snack

Contact form

Data privacy policy

About

This blog is made with ♥️ on Notion and made public with Super.so. Rocket icon created by RIkas Dzihab - Flaticon.

RedditLinkedIn