Referencing SharePoint SPSite.OpenWeb

The SPSite.OpenWeb method is designed to return specific SharePoint site from the collection of sites. This method implements several overload functions including: OpenWeb(), OpenWeb(GUID), OpenWeb(String), OpenWeb(String, Bool). All of these methods utilize constructor in it in order to get an instance of the site.

The SPSite’s OpenWeb method always returns a low node of the site specified as one of the parameters being passed for its constructor.

Here is an example of OpenWeb usage in the SharePoint

using System;
using Microsoft.SharePoint;
 
namespace contentTypeTest
{
    class SharePointApp
    {
        static void Main(string[] args)
        {
 
            SPSite site = SPContext.Current.Site;
            SPWeb web = site.OpenWeb();
 
        }
    }
}
Note: always Dispose() of your site and web objects unless they are called from within SharePonit context as in the above example. This is the reason you don’t see Dispose().

Featured pages