0

I am developing a command set & looking for a way to get the Share URL of selected documents. I have tried these to get other urls.

event.selectedRows.getValueByName('.spItemUrl');
event.selectedRows.getValueByName('FileRef');

But I want the URL look like below, which we will get something when we Share or Copy Link:

https://contoso.sharepoint.com/:i:/s/Personal/EZtdo-T3MAtJldnaju5gtb4BBmXWDdNdljhdDywGBQJAgA?e=WYgQZ5

How to get this?

1 Answer 1

3

To get the Share URL, you can use any of the 3 approaches mentioned below:

Option 1 - using ServerRedirectedEmbedUrl

You can make use the ServerRedirectedEmbedUrl to get the URL as below:

event.selectedRows.getValueByName('ServerRedirectedEmbedUrl');

Option 2 - using ?web=1 query string parameter

Another thing you can do is append the ?web=1 query string parameter and form the absolute url of the file.

const absoluteUrl:string = this.context.pageContext.site.absoluteUrl;

const relativeUrl:string = this.context.pageContext.site.serverRelativeUrl;

const docRelativeUrl:string = event.selectedRows[0].getValueByName('FileRef');

const docShareUrl = `${absoluteUrl}${docRelativeUrl.substr(relativeUrl.length)}?web=1`;

However, both above approaches have the document opening in a preview or read mode. I have noticed that the OOTB Share or Copy command generates the URL and when opened, the document is in edit mode.

So, to open it in edit mode, you can use option 3.

Option 3 - Open in edit mode

Here you need to form the url dynamically yourself as below:

The url format is: <Site URL>/_layouts/15/WopiFrame.aspx?sourcedoc=<Doc URL>&action=edit

const absoluteUrl:string = this.context.pageContext.site.absoluteUrl;

const docRelativeUrl:string = event.selectedRows[0].getValueByName('FileRef');

const docShareUrl = `${absoluteUrl}/_layouts/15/WopiFrame.aspx?sourcedoc=${docRelativeUrl}&action=edit`;
5
  • Thanks for the response. Do you know why 'ServerRedirectedEmbedUrl' is empty for me? Commented Apr 6, 2018 at 13:46
  • 1
    Could be the case that its not an Office document. I tested this with docx, xlsx and pptx files and it worked. Commented Apr 6, 2018 at 13:58
  • I see why you are hardcoding format in option 3. getValueByName('.spItemUrl') + '&$select=webUrl' is not working.. Commented Apr 6, 2018 at 16:10
  • 1
    Sorry Arun didnt get your point. Which one is not working ? Couldn't spot getValueByName('.spItemUrl') in the answer :) Safest and best option IMHO would be option 2. Some of our clients dont like stuff opening in edit mode, so i usually use option 2 to open in read mode. Commented Apr 6, 2018 at 16:14
  • Never mind, I was reading your another answer, you use "&[email protected]" & I thought similarly "webUrl" can be used instead of hardcoded "...WopiFrame.aspx?sourcedoc=.." like in option 3.. anyway thanks for assistance.. Commented Apr 6, 2018 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.