How To Create A Sharing Link For Files In Power Automate

How To Create A Sharing Link For Files In Power Automate

Sharing links can be used to give access to a file without granting access to the entire document library or site. With Power Automate, you can create a sharing link for anonymous users, people in your organization or specific internal or external users. You can define whether users have edit or view permissions and even set an expiration date. In this article I will show you how to create a sharing link for files in Power Automate.

Table of Contents
• Introduction: The Vendor Invoice Sharing Link Flow

Sharing Link For Anonymous Users Or People In Your Organization 
• Setup The SharePoint Document LibraryCreate A Sharing Link In Power AutomateSend The Sharing Link In An Email

Sharing Link For Specific Users
• Get The Drive ID For The Sharing Link FileObtain The File ID For The Sharing Link FileCreate Sharing Link For Specific UsersSend The Sharing Link To Specific Users Via EmailRun The Flow To Send A Sharing Link For Specific Users

Employees in the Accounts Payable department of a company share use an automation to share invoices with colleagues in other departments. The flow creates a share link so that users can access the file without being granted permission to the entire document library.





Setup The SharePoint Document Library

Open SharePoint and create a new document library called Vendor Invoices. Add several files to the document library to share with other users.

Choose a file to share using Power Automate. In this example we will share the following file:

  • Name – Adatum 4.pdf
  • ID – 13
  • Document Library – Vendor Invoices

In Power Automate start a new flow with an instant trigger. Add a SharePoint – Create Sharing Link For A File Or Folder action. Choose the Vendor Invoices library and supply the Item Id for the file we want to share. The Link Type controls whether the user can edit or view the file.



Select who can access the file using the Link Scope. Anyone with the link, including anonymous will allow users outside of the organization to access the file. People in your organization limits access to only users in the same tenant.



After the link is created we want to send it to a user. Insert the Outlook – Send An Email (V2) action and choose a recipient.



Use this code in the Body of the Send An Email action to include a clickable link to the file.

<p>
A file has been shared from Power Automate:
<a href="@{outputs('Create_sharing_link_for_a_file_or_folder')?['body/link/webUrl']}">Adatum 4.pdf</a>
</p>

Now that the basic flow is created we will test sending the user a sharing link via email. Save the flow and test it manually.



The recipient will receive an email. Ask them to click the link to the file.



For an Anonymous link type anyone can see the file without the need to login. Or for a People in your organization link type the user will need to login with a company email.




To create a sharing link for a specific internal user we must utilize the SharePoint Rest API. This will require us to know the Drive ID for the SharePoint document library and the Item ID for the file.

First we will find the Drive ID. Add a SharePoint – Send An HTTP Request action to the flow and select the GET Method.



Use this URI to return a list of all document libraries for the selected site.

_api/v2.0/drives



Include the following headers in the request.

KeyValue
Acceptapplication/json



Filter the values returned by the SharePoint HTTP action where the drive name equals Vendor Invoices.

item()['name']



Store the result of the Data Operations – Filter Array action in a variable named varDriveId. Use the following expression to get the ID of the first value in the array.

first(body('Filter_array:_name_eq_Vendor_Invoices'))?['id']

The Item ID for Adatum 4.pdf can be found using a similar method to the Drive ID. Add another SharePoint – Send An HTTP Request action to the flow and use the GET method.



Use this code in the Uri to get all of the files in the Vendor Invoices document library’s root directory.

_api/v2.0/drives/@{variables('varDriveId')}/items/root/children



Include these headers in the request.

KeyValue
Acceptapplication/json



Then, in the Filter action use this code in the From field to operate on the file values returned by the SharePoint HTTP action.

body('Send_an_HTTP_request:_Get_Items')['value']



Use this code in the left side of the filter comparison to check the name field.

item()['name']



Then save the Item ID in a variable named varItemId using this expression.

first(body('Filter_array:_name_eq_Filename'))?['id']

Now that we have the Drive ID and the Item ID we can create the sharing link for specific users. Load an array of user emails into a Data Operations – Compose action. Then create the sharing link with a SharePoint – Send An HTTP Request action using the POST method.



Construct this array inside of the Compose action and replace the email addresses your own email addresses. Notice that both external and internal email addresses are included.

[
  {
    "email": "[email protected]"
  },
  {
    "email": "[email protected]"
  },
  {
    "email": "[email protected]"
  }
]



Use this code in the Uri property of the SharePoint HTTP action to create a sharing link.

_api/v2.0/drives/@{variables('varDriveId')}/items/@{variables('varItemId')}/createLink



Include these values in the HTTP Request headers.

KeyValue
Acceptapplication/json



Supply this JSON payload in the body of the HTTP request. Type can be either view or edit. Scope is set to users meaning specific users must be defined. Tell the HTTP request who can use the sharing link in the recipients property.

{
  "type": "edit",
  "scope": "users",
  "recipients": @{outputs('Compose:_Specific_Users')}
}

The sharing link has been created for specific users. Now it must be sent to those users in an email. Start by adding a Data Operations – Select action and loading the From field with the Outputs of the Compose: Specific Users action used earlier in the flow.



Switch the Select action to Text Mode and use this code in the Map property.

item()?['email']



Here is a screenshot showing what the Select action does.



Then insert an Outlook – Send An Email (V2) action with the subject Sharing Link To File Adatum 4.



Use this code in the To field to join the email addresses with a semi-colon as the delimeter.

join(body('Select:_User_Emails'),';')



Write this HTML in the body of the message to display the sharing link.

<p>
A file has been shared from Power Automate:
<a href="@{body('Send_an_HTTP_request:_Sharing_Link_Specific_Users')?['link']?['webUrl']}">Adatum 4.pdf</a>
</p>

We’re done. Save and run the flow to send the sharing link.



The sharing link email appears in the inbox of the users it was sent to.



The specific user is able to open the file as expected.




Questions?

If you have any questions or feedback about How To Create A Sharing Link For Files In Power Automate please leave a message in the comments section below. You can post using your email address and are not required to create an account to join the discussion.

Matthew Devaney

Subscribe
Notify of
guest

23 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Adi
Adi
11 months ago

That’s awesome, thanks, Matthew! Can we also set an expiry datetime with the latter method like we can with the former, please?

Lobo
Lobo
11 months ago

Hello! Just a comment. MS states the max limit for unique permissions is 50000, but the recommended limit is 5000.

https://learn.microsoft.com/en-us/sharepoint/troubleshoot/lists-and-libraries/error-share-break-inheritance

This adds to that limit, right? I think it’s important to have that in mind.

Thanks!

Matthew Zebari
11 months ago

Matthew, you have the best Power Platform content, imo. I love your articles and all the details and photos. You help solve issues I run into, but then I see articles like this and ideas start spinning about what I can do for clients w Power Automate.

Thank you!!

James
James
9 months ago

Hi, thank you for the detailed explanation.
After i got the link to the SharePoint Folder, how do i get that link from PowerApp instead of sending it via email?

Yana
Yana
9 months ago

Is it possible to block the ‘Download’ option that comes with the sharing link?

Susan h
Susan h
9 months ago

Hi Mathew, this is a great article, I have a question about sharing link for external users does this require the sites settings for sharing files being set to new and existing guest? And if Sharing policies in the tenant allows certain domains only, will it require to whitelist the domain?

Last edited 9 months ago by Susan h
Pablo
Pablo
8 months ago

I need your help with a specific problem, if you have time to help me out. It’s related to the permissions to see the files. Long story short: I got >200 certificates in a OneDrive folder I need to send, I created the sharing link of each individual file using PowerAutomare, but here’s the thing: since some people don’t belong to my organization I have to grant access to “Anyone” so they can see their certificates, but doing it manually will take quite a time, I mean is more than 200 certificates. Is there a way to automate this with Power Automate or PowerShell?

Santiago
Santiago
8 months ago

I have a flow that is trigger when a file is created in sharepoint
and after the flow is done It send an email with information of what just happened…
How can i create the share link getting dynamically the ID of the file?

Claude
Claude
7 months ago

Hi, thanks for this tip. I’d really like to implement it in our organization, but I’m encountering an issue and I’m not sure if you’d be able to help me. When I insert the filter array after sending an HTTP request, the only option I have to insert into the ‘from’ input is the ‘body’ of the previous action. I don’t have the ‘value’ option, nor the ‘name’ option. Thanks

My options.jpg
Dariusz Kowalski
Dariusz Kowalski
6 months ago
Reply to  Claude

Hello @Matthew, I have the same issue.

Did you miss some part of the code? 🙂

Ben
Ben
5 months ago

Hi Matthew,

Thanks so much for the great tutorial. I am having the same issue as Dariusz and Claude. Can you add a screenshot of how you’re getting the Filter array to populate the name field? I am getting “The expression is invalid”. Do you enter that code in an expression or…?

Thanks again.

Adriaan
Adriaan
4 months ago

Hi Matthew,

I am trying this method to get Item ID of a subfolder and then create a sharing link for the subfolder.

However when I use the “Send an HTTP request to SharePoint” action with the uri: _api/v2.0/drives/{DriveID}/items/root/children to get the ItemID then I get this response: “@deprecated.Decorator”: “decorator has been deprecated. Refer to folder.decorator”

Can you confirm if this is also happening to you now? Do you perhaps have a workaround?

Adriaan
Adriaan
4 months ago

https://make.powerautomate.com/
I believe this is the URL for the new editor. I am not sure.

I have however found a different way to do all of the above.

Navigate to: Create a sharing link for a file or folder in SharePoint – Evolvous -> “Share SharePoint files or folders using flow”

Josh
Josh
4 months ago

Maybe an edge case here… I’m curious how one would get the OneDrive or SharePoint file details from a share link.

My team works as external staff and often the internal staff send “anyone in org” links to them. They will send me that link, and I’ll create a new share link that directly gives them access.

I’d love to figure out a way to automate this in power apps.

John
John
4 months ago

Great help! Especially the fact that this flow is capable of providing access to a shared folder or file to an external user! I hope I’ll be able to figure out, how to withdraw access again with another flow. I imagine this could be even trickier, as some users will share a link, while others will have their own sharing link.

Akshay
Akshay
3 months ago

Hey Mathew, I get the entire SP List link to that particular ID instead of the url to the attachment. How do I resolve ?