SharePoint Blogs / SharePoint University
SharePoint Blogs and SharePoint University - all in one place!
Need SharePoint Training? Attend a SharePoint Bootcamp!

Please delete cookies related to sharepointblogs.com and sharepointu.com to resolve login issues!

The ClassResourcePath for custom webparts in MOSS 2007

At Macaw we've been using standard project templates for most of our development projects for years. In the project template for custom webparts for SharePoint 2003 we've had a basewebpart. All custom webparts for SharePoint 2003 inherit from the basewebpart. On of the things the basewebpart takes care of is determining the ClassResourcePath.

Custom web parts can be deployed to the wwwroot/bin directory or to the global assembly cache (GAC). The deployment location also affects the location where resources (for instance usercontrols and images) that are required by the web part are stored. For GAC deployments, the directory is mapped to /_wpresources/[assembly_name]. For bin directory deployments, the directory is mapped to /wpresources/[assembly_name].

In order to determine the resource path at runtime you use the ClassResourcePath of Microsoft.SharePoint.WebPartPages.WebPart.


For SharePoint 2007 Microsoft recommends inheriting custom webparts from System.Web.UI.WebControls.WebParts.WebPart. Unfortunately this class doesn't contain an equivelant of the ClassResourcePath. However if you're building a basewebpart that will be used by a lot of custom web parts you do want to be able to determine the path where the resources are stored at runtime, since you don't beforehand know whether a web part will be deployed in the bin or in the GAC.

In order to be able to do this I've used SPWebPartManager.GetClassResourcePath. You can use this class, that is inherited from Microsoft.SharePoint.WebPartPages and at the same time inherit the web part itself from System.Web.UI.WebControls.WebParts.WebPart in order to comply with Microsoft recommendations.

The code getting the resource path will look like this:

SPWeb currentWeb = SPControl.GetContextWeb(Context);
Type currentType = BaseWebPart.GetType();
string classResourcePath = SPWebPartManager.GetClassResourcePath(currentWeb, currentType);


Posted 01-17-2007 8:53 PM by Mirjam

Comments

Anthony Graves wrote re: The ClassResourcePath for custom webparts in MOSS 2007
on 11-09-2007 5:27 PM

Freaking finally, why are you the only person I can find searching through Google that knows this? Why isn't it mentioned in MSDN?!?

By the way, I can't get BaseWebPart.GetType(), any ideas?

Anthony Graves wrote re: The ClassResourcePath for custom webparts in MOSS 2007
on 11-09-2007 5:32 PM

Oh never mind about my previous comment, i just type "getType()" and visual studio picked it up.

Seems to have worked just fine.  Thanks again!

Mirjam wrote re: The ClassResourcePath for custom webparts in MOSS 2007
on 11-09-2007 5:46 PM

Thanks Anthony!

This was one of the first things I ran into when I started working with MOSS 2007 and I was starting to wonder whether I was the only one that encountered this problem..

Tayonee wrote re: The ClassResourcePath for custom webparts in MOSS 2007
on 01-06-2008 3:44 PM

I am glad that someone finally addressed this issue, thank you! I was wondering if you might clarify one point - I am writing in VB and was wondering if this solution hinges on multiple inheritance capabilities of C#, or am I missing a critical point?

Thanks again!

vbs reading moss 2007 sharepoint logs wrote vbs reading moss 2007 sharepoint logs
on 05-12-2008 5:39 PM

Pingback from  vbs reading moss 2007 sharepoint logs

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Need SharePoint Training? Attend a SharePoint Bootcamp!
Posts (c) their respective authors. Everything else (c) 2009 SharePoint Experts, Inc.