SPContext class
代表中Microsoft SharePoint Foundation的 HTTP 要求的內容。
Inheritance hierarchy
System.Object
Microsoft.SharePoint.SPContext
Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in Microsoft.SharePoint.dll)
Syntax
'宣告
Public NotInheritable Class SPContext
'用途
Dim instance As SPContext
public sealed class SPContext
備註
使用SPContext類別,會傳回目前 Web 應用程式、 網站集合、 網站、 清單或清單項目等物件的內容資訊。
Examples
下列範例說明如何使用SPContext類別的屬性來傳回目前的清單、 網站和網站集合。
Dim currentList As SPList = SPContext.Current.List
Dim currentSite As SPWeb = SPContext.Current.Web
Dim currentSiteCollection As SPSite = SPContext.Current.Site
Dim currentWebApplication As SPWebApplication = SPContext.Current.Site.WebApplication
[C#]
SPList oListCur = SPContext.Current.List;
SPWeb oWeb = SPContext.Current.Web;
SPSite oSite = SPContext.Current.Site;
SPWebApplication oWebApplicationCur = SPContext.Current.Site.WebApplication;
Item屬性的值轉換為SPListItem物件傳回目前的清單項目,如下:
SPListItem item = (SPListItem)SPContext.Current.Item;
下列範例使用SPContext屬性,在目前的網站集合中開啟網站,並傳回目前的使用者。
Dim site As SPWeb = SPContext.Current.Site.OpenWeb(guid)
Dim user As SPUser = SPContext.Current.Web.CurrentUser
[C#]
using(SPWeb oWeb = SPContext.Current.Site.OpenWeb(guidWebsite))
{
SPUser oUser = SPContext.Current.Web.CurrentUser;
}
下一個範例會使用SPContext類別,來執行查詢的情況下,項目識別碼大於 100 的目前網站上。它會將結果寫入伺服器上的檔案中。這個範例要求using指示詞 (在 Visual Basic 的Imports ) 的System.Data和Microsoft.SharePoint命名空間。
Dim siteQuery As New SPSiteDataQuery()
siteQuery.Query = "<Where><Gt><FieldRef Name=""ID"" />" + "<Value Type = ""Number"">100</Value></Gt></Where>"
siteQuery.ViewFields = "<FieldRef Name=""Title""/>"
Dim queryResults As DataTable = SPContext.Current.Web.GetSiteData(siteQuery)
queryResults.TableName = "queryTable"
queryResults.WriteXml("C:\queryTable.xml")
[C#]
SPSiteDataQuery oSiteQuery = new SPSiteDataQuery();
oSiteQuery.Query = "<Where><Gt><FieldRef Name=\"ID\" />" +
"<Value Type = \"Number\">100</Value></Gt></Where>";
oSiteQuery.ViewFields = "<FieldRef Name=\"Title\"/>";
DataTable oQueryResults = SPContext.Current.Web.GetSiteData(oSiteQuery);
oQueryResults.TableName = "queryTable";
oQueryResults.WriteXml("C:\\queryTable.xml");
前一個範例會具現化的SPSiteDataQuery物件,並將Collaborative Application Markup Language (CAML)中的字串指派給它的Query屬性。這個字串會定義查詢 (請參閱 [ Query Schema])。**GetSiteData()**方法會傳回System.Data.DataTable物件,其內容會寫入為 XML 檔案。
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.