OpenWeb Method ()
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

SPSite.OpenWeb Method

Windows SharePoint Services 3

Returns the site that is associated with the URL that is used in an SPSite constructor.

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

public SPWeb OpenWeb()

Return Value

Type: Microsoft.SharePoint.SPWeb
An SPWeb object that represents the site.

When used in conjunction with an SPSite constructor, the OpenWeb method returns the lowest-level site specified by the URL that is passed as parameter for the constructor. For more information about using the OpenWeb method with an SPSite constructor, see the OpenWeb method overload.

The following code example returns the Web site that is located at http://Server_Name/sites/Site_Name/Subsite_Name.

string strUrl = 
   "http://Server_Name/sites/Site_Name/Subsite_Name/default.aspx";
using (SPSite oSiteCollection = new SPSite(strUrl))
{
    using(SPWeb oWebsite = oSiteCollection.OpenWeb())
    {
        ...
    }
}
NoteNote

Certain objects implement the IDisposable interface, and you must avoid retaining these objects in memory after they are no longer needed. For information about good coding practices, see Best Practices: Using Disposable Windows SharePoint Services Objects.

Show:
© 2016 Microsoft