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 Library
• Create A Sharing Link In Power Automate
• Send The Sharing Link In An Email
Sharing Link For Specific Users
• Get The Drive ID For The Sharing Link File
• Obtain The File ID For The Sharing Link File
• Create Sharing Link For Specific Users
• Send The Sharing Link To Specific Users Via Email
• Run The Flow To Send A Sharing Link For Specific Users
Introduction: The Vendor Invoice Sharing Link Flow
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.
Sharing Link For Anonymous Users Or People In Your Organization
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
Create A Sharing Link In Power Automate
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>
Send The Sharing Link In An Email
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.
Sharing Link For Specific Users
Get The Drive ID For The Sharing Link File
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.
Key | Value |
Accept | application/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']
Obtain The File ID For The Sharing Link File
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.
Key | Value |
Accept | application/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']
Create Sharing Link For Specific Users
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.
Key | Value |
Accept | application/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')}
}
Send The Sharing Link To Specific Users Via Email
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>
Run The Flow To Send A Sharing Link For Specific Users
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.
Did You Enjoy This Article? 😺
Subscribe to get new Power Apps & Power Automate articles sent to your inbox each week for FREE
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.
That’s awesome, thanks, Matthew! Can we also set an expiry datetime with the latter method like we can with the former, please?
Adi,
You can add this key-value pair to the body of the HTTP request:
“expirationDateTime”: “yyyy-MM-ddTHH:mm:ssZ”
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!
Lobo,
Yes, creating the share link does add to the number of unique permissions.
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!!
Matthew,
Well thank you. I appreciate you leaving me a kind comment.
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?
James,
Send the link back to the app using the Respond In Power Apps action.
Is it possible to block the ‘Download’ option that comes with the sharing link?
Yana,
Yes, if you are using the Graph API to create the link you can choose BlocksDownload and it will do what you are asking.
https://learn.microsoft.com/en-us/graph/api/listitem-createlink?view=graph-rest-beta&tabs=http#link-types
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?
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?