only My site

Monday, June 25, 2012

SPSite.OpenWeb In-Depth

Usually there is some confusion in using the open web method with parameter. Without parameter it will open the site which is mentioned in the string URL of the sitecollection. The below MSDN link provides a detailed information with samples in Matrix format to clear all the doubts. It is worth reading this MSDN article.

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

Sample for OpenWeb()

string strUrl = "http://MyServer/MyWebSite/Lists/Announcements/AllItems.aspx";

using (SPSite oSiteCollection = new SPSite(strUrl))
{
    using(SPWeb oWebsite = oSiteCollection.OpenWeb("MyWebSite/MySubSite"))
    {
        Console.WriteLine("Website: " + oWebsite.Url);
    }
}

No comments: