Export (0) Print
Expand All

SPContext class

Represents the context of an HTTP request in Microsoft SharePoint Foundation.

System.Object
  Microsoft.SharePoint.SPContext

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

public sealed class SPContext

Use the SPContext class to return context information about such objects as the current Web application, site collection, site, list, or list item.

The following examples illustrate how to use properties of the SPContext class to return the current list, site, and site collection.

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;

Cast the value of the Item property as an SPListItem object to return the current list item, as follows:

SPListItem item = (SPListItem)SPContext.Current.Item;

The following examples use SPContext properties to open a Web site in the current site collection and to return the current user.

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;
}

The next example uses the SPContext class to perform a query on the current site for cases where item IDs are greater than 100. It writes the results in a file on the server. This example requires using directives (Imports in Visual Basic) for the System.Data and Microsoft.SharePoint namespaces.

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");

The previous example instantiates an SPSiteDataQuery object and assigns a string in Collaborative Application Markup Language (CAML) schemas to its Query property. This string defines the query (see Query Schema). The GetSiteData() method returns a System.Data.DataTable object whose contents are written as an XML file.

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