Share via


SPContext Class

Represents the context of an HTTP request in Windows SharePoint Services.

Inheritance Hierarchy

System.Object
  Microsoft.SharePoint.SPContext

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

Syntax

<SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel := True)> _
<SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel := True)> _
Public NotInheritable Class SPContext

Dim instance As SPContext
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel = true)]
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel = true)]
public sealed class SPContext

Remarks

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

Examples

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 snippets 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 Core 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.

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.

See Also

Reference

SPContext Members

Microsoft.SharePoint Namespace