Some thoughts on programming stuff

Get web URL from document URL in SharePoint

Maybe you need to get web URL from document URL in SharePoint, or even just grab the Site Collection URL and do some awesome logic and update some data in your site. But you are aware that it can be costy and require some array splits, substrings and those kind of tricks. So here is a code snippet to save your time to enjoy a good cup of coffee with your colleagues!

The document URL usually is composed by a site collection part, the specific web part, the Library name and then the final part containing the folders and the file name. The script basically will perform a request to a SharePoint API. Our parameter is the folder or library URL (cutting the filename) that will be made automatically. Finally, the result will come with the Web and Site Collection urls for that context.

Get web URL from document URL in SharePoint - A bunch of files in folders inside drawers (SharePoint)
A bunch of files in folders inside drawers (SharePoint) (Source: Howtogeek)

Grabbing the web URL from document URL with JavaScript

Here is the script when where the File Url will initialize the variable fullUrl. So you can send and adapt it to a promise or other type of asynchronous method you want to use. After a few moments the JSON will be returned with what you want!

Final thoughts in this SharePoint case

Hope this blog post can save you some time when you need to get web URL from document URL in SharePoint. It can be quite useful if you need to grab some data from an specific Web when presenting a list of files in a Web Part.

Also you can check other blog posts related to JavaScript solutions here.

Well, that’s all for this post today! Best of luck in your SharePoint adventures!

References:
XML Http Request
Complete basic operations with SharePoint Rest Endpoints

2 Comments

  1. Alain

    Hi Wiliam
    I don’t find the scrip ypu had mentioned:

    Here is the script when where the File Url will initialize the variable fullUrl. So you can send and adapt it to a promise or other type of asynchronous method you want to use. After a few moments the JSON will be returned with what you want!

    • Wiliam Rocha

      Hey! I use Github Gists to present the code. Try again accessing the page, maybe when you were reading it the Github API was unavailable.
      Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

© 2024 wiliammbr's blog

Theme by Anders NorenUp ↑