I use the SharePoint API to access a tenant SharePoint with a SharePoint REST API call that looks like {site_prefix}/_api/web/GetFileByServerRelativeUrl({path})/... where site_prefix is usually something like /sites/siteName but I think that it can sometimes also be /teams/siteName. Given a webUrl of a driveItem, I was using a regular expression like re.match(r"/sites/([^/]+)", webUrlPath).group() and this usually works because most sites prefixes start with /sites/.

However, I've run into a tenant that does not have a prefix like this, but rather their own custom prefix, so their item's webUrl looks like https://contoso.sharepoint.com/customname/. I would like to know how to determine that the site prefix here is customname so that I can plug it into the SharePoint REST API call that I mentioned in the beginning. I assume that this has something to do with "Managed Paths", but I do not know how to find the managed paths programmatically. If necessary, I also have access to the Graph API.

I have considered just trying /sites/([^/]+), /teams/([^/]+), and as last resort ([^/]+), but I think this may not always work because (please correct if I'm wrong) the prefix may also have multiple slashes making it hard to know where the prefix ends and path begins.

  • Which version of SharePoint you are using? Why are you using hardcoded site URL? If you are calling any API from SharePoint pages, you can get the current site URL from page context. Apr 19 at 13:16
  • @GaneshSanap I'm not the one using SP per se -- it's the customer/tenant and I am accessing information about their SP using the SP REST and Graph APIs. I'm not hardcoding any site URLs -- I'm trying to determine the site prefix of the site collection of a given drive item or site. There is no page context. Apr 19 at 14:18

It was simpler than I initially thought once I realized that I can get the siteUrl from any given drive item. I'm using the graph API to get the SharepointIds with ?$select=sharepointids followed by parsing out the path with urllib.parse.urlparse(result["sharepointIds"]["siteUrl"]).path (Python). This is working so far, as I get /sites/name for most sites and /customname on the problem site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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