Click to Rate and Give Feedback
SPWeb Class (Microsoft.SharePoint)
Represents a Windows SharePoint Services Web site.

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

Visual Basic (Declaration)
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel:=True)> _
Public Class SPWeb
    Implements IDisposable, ISecurableObject
Visual Basic (Usage)
Dim instance As SPWeb
C#
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel=true)] 
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
public class SPWeb : IDisposable, ISecurableObject

Many methods and properties in the Microsoft.SharePoint namespace can return a single Web site. You can use the Webs property of the SPWeb class to return all the immediate child Web sites beneath a Web site, excluding children of those child Web sites. You can also use the AllWebs property of the SPSite class to return all Web sites within the site collection; or use the GetSubwebsForCurrentUser method of SPWeb to return all Web sites for the current user.

Use an indexer to return a single Web site from the collection. For example, if the collection is assigned to a variable named collWebSites, use collWebSites[index] in Microsoft C#, or collWebSites(index) in Microsoft Visual Basic, where index is the index number of the site in the collection, the display name of the Web site, or the GUID for the site.

Use the Web property of the SPContext class to return an SPWeb object that represents the current Web site, as follows:

Visual Basic
Dim oWebsite As SPWeb = SPContext.Current.Web
C#
SPWeb oWebsite = SPContext.Current.Web;


To return the top-level Web site for the site collection, you can use the Site property of the SPContext class and the RootWeb property of the SPSite class as follows:

Visual Basic
Using oWebsiteRoot As SPWeb = SPContext.Current.Site.RootWeb
    ...
End Using
C#
using (SPWeb oWebsiteRoot = SPContext.Current.Site.RootWeb)
{
    ...
}

To return a specific Web site, you can use the OpenWeb method of the SPSite class as follows.

Visual Basic
Using oWebsite As SPWeb = SPContext.Current.Site.OpenWeb("Website_URL")
    ...
End Using
C#
using(SPWeb oWebsite = SPContext.Current.Site.OpenWeb("Website_URL"))
{
    ...
}


You can also use the SPSite constructor to instantiate a site collection, and then use one of the members of the SPSite class, which were mentioned earlier, to return the top-level site or a subsite as follows:

Visual Basic
Using oSiteCollection As New SPSite("http://Server_Name")
    Using oWebsite As SPWeb = oSiteCollection.OpenWeb("Website_URL")
        Using oWebsiteRoot As SPWeb = oSiteCollection.RootWeb
            ...
        End Using
    End Using 
End Using
C#
using(SPSite oSiteCollection = new SPSite("http://Server_Name"))
{
    using(SPWeb oWebsite = oSiteCollection.OpenWeb("Website_URL"))
    {
        using(SPWeb oWebsiteRoot = oSiteCollection.RootWeb)
        {
           ...
        }
    }
}

If you obtain an SPWeb object by calling members such as those demonstrated in previous code samples, the best practice is to implement the using statement or the Dispose method to dispose of the object. However, if you have a reference to a shared resource, such as when you obtain the Web site object from the SPContext object in a Web Part by using SPContext.Current.Web, do not use either method to close the object. Using either of these methods on a shared resource causes an access violation error to occur. In scenarios where you have a reference to a shared resource, let Windows SharePoint Services or your portal application manage the object instead. For more information about good coding practices, see Best Practices: Using Disposable Windows SharePoint Services Objects.

System.Object
  Microsoft.SharePoint.SPWeb
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker