SPContext Class

Very confusing definitions —
SPWebApplication – Web Site (Web Application) inside IIS
SPSite — Site Collection
SPSiteCollection – A Collection of Site Collections (above)
SPWeb – Each site under a site collection
SPWebCollection (a group of SPWeb, above)

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

Use the SPContext class to return context information about such objects as the current Web application, site collection, site, list, or list item, e.g.,
SPList oListCur = SPContext.Current.List;
SPWeb oWeb = SPContext.Current.Web;
SPSite oSite = SPContext.Current.Site;
SPWebApplication oWebApplicationCur = SPContext.Current.Site.WebApplication;
SPListItem item = (SPListItem)SPContext.Current.Item;
item need to cast to SPListItem

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

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.
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”);

  

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: