Miscellaneous Debris

Avner Kashtan's Frustrations and Exultations
How to get the current SPS Area in a Web-Part

When we write a WebPart or WebControl that resides in a SPS Area, we will want to know at runtime which area we reside in.
This, surprisingly enough, is not trivial. It's not even easy. In fact, it's downright obscure

You can easily get the current SPWeb you're in using the SPControl object, but while the Area class can expose the underlying SPWeb, an SPWeb can't give you the equivalent Area - which makes sense, since SPS's Areas are built on top of WSS's SPWebs.

This data is stored quite nicely in the database - there's a WebCat table that cross-references Area GUIDs with Web GUIDs - but we really don't want to access the DB directly, do we?

The (obscure) solution is this:

From the Context property of the webpart/control, access the stored PageInfo object stored in the hashtable:

PageInfo pi = (PageInfo)Context.Items["SPS_PageInfo"];

The PageInfo class is defined in Microsoft.SharePoint.Portal.WebControls namespace, and is apprently undocumented in any SDK I found. It contains a property named CategoryID, which is the Guid of the current Area:

Guid currAreaGuid = pi.CategoryID;

Once we have the Guid, it's clear sailing all the way:

Area currArea = AreaManager.GetArea(PortalContext.Current, currAreaGuid);

And there was much rejoicing.

Published Tuesday, September 14, 2004 12:49 AM by AvnerK

Filed under: ,

Comments

# How to get the Current SPS Area@ Monday, April 04, 2005 9:13 PM

TrackBack

# re: How to get the current SPS Area in a Web-Part@ Wednesday, July 26, 2006 12:00 PM

I very much appreciate you sharing that info.  I have been trying to tackle this task for the last 5 hours.

Brian Culver

# re: How to get the current SPS Area in a Web-Part@ Wednesday, July 26, 2006 12:43 PM

If I remember correctly, I've since learned that this is actually a roundabout way of getting this information.

I think you can simply call AreaManager.GetArea(), passing it the the SPWeb.GetContextWeb().ID guid, and it will do the translation for you. I'm not entirely sure since it's been two years since I wrote this, but it's worth a try. :)

AvnerK

# Récupérer l'area courante dans Sharepoint@ Wednesday, October 25, 2006 5:08 AM

J'ai vu pas mal de code super compliqué sur le net pour récupérer l'area courante :http://weblogs.asp.net/avnerk/archive/2004/09/14/229033.aspxhttp://www.sharepointu.com/forums/Working_with_Portal_Areas/m_7014/tm.htmpourtant ce la peut se faire simple

Blog de Jérémie Clabaut (ak MinSou)

Leave a Comment

(required) 
(required) 
(optional)
(required)