1
votes

I'm looking for a way to get the link that you get when you press copy link on a document from sharepoint online document library. I need to retrieve this link programmatically using powershell. any advice and thoughts?

Here is a picture of the link highlighted in blue.

enter image description here

Thank you

3

3 Answers

1
votes

You need to use the Microsoft Graph API. There are examples and documentation around the web but it is in Beta so keep in mind the process might change.

You can use Get on the sharepoint site and see the drive objects, Then you can connect to them using the Drive ID and look at the webURL property of the files.

https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/resources/drive

1
votes

I ended up constructing the link dynamically. So Here is what I ended up doing to get the link:

  1. use the cmdlet Get-PnPListItem
  2. retrieve each UniqueID for each item in the library
  3. Here is an example

    $UniqueID = (Get-PnPListItem -id 231 -List Budget).FieldValues.UniqueID

  4. the $UniqueID = e1a1f20f-4b7d-4y16-x021-3469bde98088

Here are the link steps:

  1. https://mycompany.sharepoint.com/Site/subsite/_layouts/15/WopiFrame.aspx?sourcedoc={
  2. e1a1f20f-4b7d-4y16-x021-3469bde98088
  3. }&action=edit

at the end just construct all steps to build your link dynamically. https://mycompany.sharepoint.com/Site/subsite/_layouts/15/WopiFrame.aspx?sourcedoc={e1a1f20f-4b7d-4y16-x021-3469bde98088}&action=edit

In the end the only id you will be changing is the unique id between the curly brackets.

1
votes

Adding to what Nick says, we can use the Graph API to achieve this requirement in particular the "createLink" endpoint.

We can do a POST request with following body (or similar)

{
  "type": "edit",
  "scope": "organization"
}

and the request URL can be one of the following

  • /v1.0/sites/{site-id}/lists/{list-id}/items/{item-id}/driveItem/createLink
  • /v1.0/sites/{site-id}/drive/items/{drive-item-id}/createLink
  • /v1.0/sites/{site-id}/drives/{drive-id}/items/{drive-item-id}/createLink