in

Dot Net Mafia

Group site for developer blogs dealing with (usually) Ionic, .NET, SharePoint, Office 365, Mobile Development, and other Microsoft products, as well as some discussion of general programming related concepts.

This Blog

Syndication

Archives

Corey Roth [MVP]

A SharePoint MVP bringing you the latest time saving tips for Ionic, SharePoint, and Office 365.

How to: Get a SPWeb object given a full URL

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

Published Feb 14 2008, 04:26 PM by CoreyRoth
Filed under:

Comments

 

KyleKelin said:

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.

February 15, 2008 8:27 AM
 

K said:

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 ! :)

February 28, 2011 4:21 AM
 

Ketan said:

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

June 17, 2011 10:44 AM
 

CoreyRoth said:

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

June 20, 2011 4:01 PM
 

CoreyRoth said:

Corrected!

July 1, 2011 9:42 AM
 

Khushi said:

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

November 2, 2011 4:39 PM
 

Steven Yandell said:

Thanks Corey!

February 16, 2012 1:48 PM
 

CoreyRoth said:

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

February 16, 2012 10:25 PM
 

Matthew Lamb said:

You haven't disposed your SPWeb object.

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

March 20, 2012 11:07 AM
 

MAIR said:

Any notion on how to do this with csom?

January 20, 2014 10:02 PM
 

CoreyRoth said:

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

February 12, 2014 8:41 AM
 

CoreyRoth said:

@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

February 12, 2014 4:09 PM

Leave a Comment

(required)  
(optional)
(required)  
Add

About CoreyRoth

Corey Roth is an independent SharePoint consultant specializing in ECM, Apps, and Search.
2018 dotnetmafia.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems