in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Mirjam's blog

Blogging about SharePoint related stuff

The use of SPContext

When building custom web parts for Windows SharePoint Services V3. You can use the SPContext object to get for instance the site or web from the current context.  But that's not all you can get from SPContext. You can also get the listitem and the webfeatures.

In WSS V2 you would use SPControl.GetContextSite() or SPControl.GetContextWeb() but that's a lot less flexible and it's slower as well.

A couple of different uses of SPContext are shown below:

*************************************************************************

SPList currentList = SPContext.Current.List;

SPWeb currentSite = SPContext.Current.Web;

SPSite currentSiteCollection = SPContext.Current.Site;

SPWebApplication currentWebApplication = SPContext.Current.Site.WebApplication;

*************************************************************************

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

*************************************************************************

SPWeb site = SPContext.Current.Site.OpenWeb(guid);

SPUser user = SPContext.Current.Web.CurrentUser;

*************************************************************************

SPSiteDataQuery siteQuery = new SPSiteDataQuery();

siteQuery.Query = "<Where><Gt><FieldRef Name=\"ID\" />" +

    "<Value Type = \"Number\">100</Value></Gt></Where>";

siteQuery.ViewFields = "<FieldRef Name=\"Title\"/>";

DataTable queryResults = SPContext.Current.Web.GetSiteData(siteQuery);

queryResults.TableName = "queryTable";

queryResults.WriteXml("C:\\queryTable.xml");

The last example makes use of the SPSiteDataQuery object.  In WSS V3 there are two different query objects. Besides SPSiteDataQuery object there is also the SPQuery object.
The difference between the two is that SPQuery can only be used for queries within a single folder in a single list. SPSiteDataQuery can be used for cross lists and even cross site queries.

Published Feb 23 2007, 08:37 PM by Mirjam
Filed under: ,

Comments

 

Ali B said:

April 10, 2008 9:10 PM
 

Sohbet said:

Thank you

June 14, 2008 10:07 AM
 

Amit Agrawal said:

Hi Mirjam,

How can I get the reference of SPContext ?

August 18, 2008 5:18 AM

Leave a Comment

(required )  
(optional )
(required )  
Add

About Mirjam

Mirjam van Olst works as a lead SharePoint developer at Macaw in the Netherlands. She started working for Macaw in April 2004. Before that she had her own company (Van Olst Websolutions) with which she build web applications. Mirjam has specialized in SharePoint development since June 2004. She started working with the 2007 Office system in may 2006. She has implemented several world-wide intranet environments based on SharePoint. She also gives presentations about MOSS 2007 on a regular basis and occasionally writes articles about MOSS related subjects.

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts