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.

Posted: Sep 14 2004, 12:49 AM by AvnerK | with 4 comment(s)
Filed under: ,

Comments

TrackBack said:

# April 4, 2005 9:13 PM

Brian Culver said:

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

# July 26, 2006 12:00 PM

AvnerK said:

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. :)

# July 26, 2006 12:43 PM

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

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

# October 25, 2006 5:08 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)