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



Filter the file values returned by the SharePoint HTTP action by the filename Adatum 4.pdf.

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

6 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Adi
Adi
1 month 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
1 month 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
1 month 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!!