How to: Get a SPWeb object given a full URL

Posted Thursday, February 14, 2008 4:26 PM by CoreyRoth

I have been using Enterprise Search lately to find sites programatically.  The issue I ran into is that I needed to manipulate data on each site that was returned in the results and the only starting point I had was a fully qualified URL.  At first, this task seems quite complicated because after examing the SPWeb object you will notice there is no constructor.  I was hoping, I could just pass it the URL but alas that does not work.  Really, the only way to create a SPWeb object is to use the SPSite object.  So at first thought, I was like well how do I know what site collection something is in, given only its URL.

After a little digging in the documentation, it turns out when specifying a URL in the constructor of the SPSite object, you do not have to specify the path of the root of the site collection.  You can specify the path to anything in the site collection and it will return an SPSite object representing the site collection you want.  Consider the following example.  I have a document at http://MyServer/MySiteCollection/MySubSite/MyDocumentLibrary/Document1.docx.  In this case the site collection is at http://MyServer/MySiteCollection and the subsite is at http://MyServer/MySiteCollection.  To get the SPWeb object, I start by passing the full URL of the document to the constructor of SPSite.  I then just call OpenWeb with the default constructor to get the SPWeb object I need.

using (SPSite siteCollection = new SPSite("http://MyServer/MySiteCollection/MySubSite/MyDocumentLibrary/Document1.docx"))

{

    SPWeb myWeb = siteCollection.OpenWeb();

}

It turns out that the functionality of OpenWeb differs greatly depending on what was passed into the constructor of SPSite.  In this case, if you call OpenWeb with no parameters, it will return the SPWeb of the site where the document exists.  For more information take a look at the URL below.

http://msdn2.microsoft.com/en-us/library/ms474633.aspx

Filed under:

Comments

# re: How to: Get a SPWeb object given a full URL

Friday, February 15, 2008 8:27 AM by KyleKelin

Good post. This comes from MS and their confusing terminology. They couldn't even keep it straight and the API means of site and web are different than in the UI.

# re: How to: Get a SPWeb object given a full URL

Monday, February 28, 2011 4:21 AM by K

Thanks for the post, I was also confused by how to load a SPWeb object directly from the URL.

I was already thinking of complex mechanisms to compare urls after loading the SPSite object etc.

Your post saved me a lot of time ! :)

# re: How to: Get a SPWeb object given a full URL

Friday, June 17, 2011 10:44 AM by Ketan

Awesome post Corey - with the architecture of social feature in 2010 - I am so sure most developers are going to bump into this.

# re: How to: Get a SPWeb object given a full URL

Monday, June 20, 2011 4:01 PM by CoreyRoth

@Ketan The architecture of the social features in SharePoint 2010 will require its own post.  Too much to discuss in a comment.

# re: How to: Get a SPWeb object given a full URL

Friday, July 1, 2011 9:42 AM by CoreyRoth

Corrected!

# re: How to: Get a SPWeb object given a full URL

Wednesday, November 2, 2011 4:39 PM by Khushi

Hey my scenario is somewhat similar to you but still could not figure out the solution. I have a webpart that can be placed on the Portal Home Page or some where else viz other site pages of the portal or subsite's home page or subsite's pages.

I just want to know if this webpart is on the PORTAL HOME page or some other location. Because this web part is going to be displayed differenly based on where it is placed.

could you please suggest me how can i get the location of the webpart?

Thanks

Khushi

# re: How to: Get a SPWeb object given a full URL

Thursday, February 16, 2012 1:48 PM by Steven Yandell

Thanks Corey!

# re: How to: Get a SPWeb object given a full URL

Thursday, February 16, 2012 10:25 PM by CoreyRoth

@Yandell No problem buddy!  Hope you're doing well.

# re: How to: Get a SPWeb object given a full URL

Tuesday, March 20, 2012 11:07 AM by Matthew Lamb

You haven't disposed your SPWeb object.

You should use a second "using" block to ensure it is correctly disposed.

# re: How to: Get a SPWeb object given a full URL

Monday, January 20, 2014 10:02 PM by MAIR

Any notion on how to do this with csom?

# re: How to: Get a SPWeb object given a full URL

Wednesday, February 12, 2014 8:41 AM by CoreyRoth

@MAIR Yes.  You can do this with /_api/contextinfo.  I plan on posting something on this soon.

# re: How to: Get a SPWeb object given a full URL

Wednesday, February 12, 2014 4:09 PM by CoreyRoth

@MAIR check out this example on how to do the same technique with CSOM.  

www.dotnetmafia.com/.../how-to-get-a-clientcontext-for-a-site-given-a-full-url.aspx

Leave a Comment

(required)
(required)
(optional)
(required)