SPWeb class

表示SharePoint Foundation網站。

Inheritance hierarchy

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

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

Syntax

'宣告
Public Class SPWeb _
    Inherits SPSecurableObject _
    Implements IDisposable
'用途
Dim instance As SPWeb
public class SPWeb : SPSecurableObject, IDisposable

備註

許多方法和Microsoft.SharePoint命名空間中的屬性可傳回單一網站。您可以使用SPWeb類別的Webs屬性來傳回某個網站,底下的所有直接子網站不包含這些子網站的子系。您也可以使用SPSite類別的AllWebs屬性來傳回網站集合 ; 內的所有網站或使用SPWeb的GetSubwebsForCurrentUser方法來傳回目前使用者的所有網站。

若要從集合傳回單一網站使用索引子。例如,如果集合指派給一個名為collWebSites變數,使用在 C# 的collWebSites[index]或Visual Basic,在collWebSites(index)index所在集合,該網站的網站或 GUID 的顯示名稱中的網站索引編號。

Examples

使用SPContext類別的Web屬性,傳回SPWeb物件,表示目前的網站,如下所示:

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

若要返回網站集合的最上層網站,您可以使用SPContext類別的 [ Site() ] 屬性和SPSite類別的RootWeb屬性,如下所示:

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

若要傳回特定的網站,您可以使用SPSite類別的OpenWeb方法,如下所示。

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

您可以也使用SPSite建構函式具現化網站集合,然後再使用其中一個類別的成員SPSite ,這先前所述,傳回最上層網站或子網站,如下所示:

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)
        {
           ...
        }
    }
}

如果您取得由呼叫成員,例如,先前的程式碼範例所示的SPWeb物件,最佳的作法就是實作using陳述式或Dispose方法来處置的物件。不過,如果您擁有參考至共用的資源,例如當您取得網站物件從 Web 組件中的SPContext物件藉由使用SPContext.Current.Web,請勿使用任何一種方法來關閉物件。共用資源上使用這兩種方法會導致存取違規錯誤發生。在其中有共用資源的參考的情況下,讓Microsoft SharePoint Foundation或入口網站的應用程式而是管理該物件。如需有關良好的程式碼撰寫方式的詳細資訊,請參閱Disposing Objects

Thread safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

請參閱

參照

SPWeb members

Microsoft.SharePoint namespace