SPWeb 类

Represents a SharePoint Foundation website.

继承层次结构

System.Object
  Microsoft.SharePoint.SPSecurableObject
    Microsoft.SharePoint.SPWeb

命名空间:  Microsoft.SharePoint
程序集:  Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)

语法

声明
Public Class SPWeb _
    Inherits SPSecurableObject _
    Implements IDisposable
用法
Dim instance As SPWeb
public class SPWeb : SPSecurableObject, IDisposable

备注

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

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

示例

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

Dim oWebsite As SPWeb = SPContext.Current.Web
SPWeb oWebsite = SPContext.Current.Web;

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

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

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

Using oWebsite As SPWeb = SPContext.Current.Site.OpenWeb("Website_URL")
    ...
End Using
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:

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
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 website 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 Microsoft SharePoint Foundation or your portal application manage the object instead. For more information about good coding practices, see Disposing Objects.

线程安全性

该类型的任何公共 静态 (已共享 在 Visual Basic 中) 成员都是线程安全的。不保证任何实例成员都是线程安全的。

另请参阅

引用

SPWeb 成员

Microsoft.SharePoint 命名空间