5 out of 7 rated this helpful - Rate this topic

SPContext Class

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

Namespace: Microsoft.SharePoint
Assembly: Microsoft.SharePoint (in microsoft.sharepoint.dll)
[SharePointPermissionAttribute(SecurityAction.LinkDemand, ObjectModel=true)] 
[SharePointPermissionAttribute(SecurityAction.InheritanceDemand, ObjectModel=true)] 
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 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.

System.Object
  Microsoft.SharePoint.SPContext
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
SPContext returns null everytime
i have tried so many times on the Sharepoint installed machine itself, but SPContext.current returns NULL every time. Can you please tell me when this would be accessible and when not.
How to create Http Context
Here's a quick way that I use to create my HttpContext objects outside sharepoint domain. (ie. creating unit tests for sharepoint)
http://novatecno.blogspot.com/2010/02/how-to-create-sharepoint-http-context.html

I hope it helps
-Edge
This is a lie
This is a lie, SPContext.Current Always is null.
They can be two things: that those who write these articles do not know absolutely nothing, or Sharepoint It is a garbage that does not serve for anything.