SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's have, for example, this url:

Is there an efficient way to analyse this full url to extract site and web url?

  • The web application url is (this one is easy to find, as it's the scheme + host)
  • The site url is
  • The web url is

When a SPContext is available, this is easy, but what about an external application? Typically, what about from a PowerShell script?

$fullUrl = ""
$siteUrl = ___($fullUrl)
$site = Get-SPSite $siteUrl

I know I could play with Get-SPSite -Limit All and compare url, but it may be tricky when nesting site collection, or have a large number of site collection.

Actually, I'm hoping Microsoft have an utility class that will do the job (but I didn't find it yet).

Any advise?

share|improve this question
up vote 5 down vote accepted

The normal constructor for a SPSite accepts any url and opens the Site Collection which is a best match. If you from that SPSite call OpenWeb() with no parameters you get the SPWeb corresponding to the Site which best match the url specified when created the SPSite. Unfortunately the PowerShell cmdlets don't allow you to do that, but then you just revert to using .Net directly like this:

$fullUrl = ''
$site = New-Object -Type Microsoft.SharePoint.SPSite -ArgumentList $fullUrl
$web = $site.OpenWeb()
share|improve this answer
Thanks, this would do the trick. I imagine there are some limitation, regarding permissions, but I can live with that (or use RunWithElevatedPrivileges) – Steve B Sep 24 '13 at 10:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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