Logo SharePoint Thoughts   Downloads   About   Up to Bluedog Limited
Web Part Class Resources - a forgotten storage location
Posted on 8/6/2009 10:41 AM by Maurice Prather
Over the past few months, I've had the opportunity to code review several different private and commercial products built on SharePoint.
 
One of the things that I noticed is that folks are putting Web Part related files (css, js, images, ascx) all over the place...
 
Naturally, I have to ask why...  often times, folks just dump files into the locations that seemingly make sense... for example, images often times go in "layouts/images" or any number of other layouts locations, user controls get dumped into controltemplates, sometimes they are embedded into the web part assembly, sometime they are pushed into doc libs, etc...
 
Reasonable logic but when I ask why the web part resources folder wasn't used, some folks stop and ask "what is that?"
 
So, it seems that web part resources have somewhat fallen off the radar... 
 
What are Web Part resources?  Each Web Part assembly has an opportunity to associate any number of files with itself.  This is defined in the Solution file via the ClassResource element.
 
The files will be deployed to one of two locations, depending on your installation type (GAC or bin).  The locations are nothing more than virtual directories that live on the web front ends.  The bin version (/wpresources) does not allow certain types of files to be rendered (.asax, .ascx, .ashx, .asmx, .aspx).  The GAC version (/_wpresources) is not as restrictive and pretty much available to host all page types.
 
Why is it good to put resources such as js, css, images, ascx files in the Web Part resources folder?
  • You automatically get our own storage location.  SharePoint creates a unique folder for your assembly.
  • Since it's on the file system, you get the inherent benefits of not hitting the database and automatically utilizing IIS for managing file caching and/or compression (for those instance where related files are put into sharepoint itself)
  • You don't even have to worry about the path... SharePoint gives that you.

The path is retrieved using the static method GetServerRelativeClassResourcePath that hangs off the SPWebPartManager class.

Throw your images, css, .ascx files, and whatever else you need in there!

Wait... .ascx files?  Don't user controls have to go into ControlTemplates?  Don't they have to be registered in the SafeControls list?

The answer depends on how you call the ascx files.  A lot of folks use .ascx files as templates for their Web Parts (meaning they have a shell Web Part that calls Page.LoadControl to load their ascx file).  If your Web Part directly loads the user control, then using class resources is perfectly viable opportunity.  The SafeControls list is only applied to controls that are explicitly created via page markup.  The key point to remember here is that your Web Part is SafeControl-approved because it was explicitly created and parsed as part of the page; however once the code is running, it can do anything it wants (as allowed by the CAS policy) .  Thus, loading a file off the system is perfectly valid and not routed through the SafeControls monitor.

On the other hand, if you are deploying ascx files as standalone controls then your best best continues to be controltemplates (given the location and the SafeControl entry exists).  The hard limitation here is the fact that the bin-version of wpresources does not allow .ascx files to be rendered directly (in other words, even if you had the right SafeControls entry, iis would prohibit the control from being rendered).

The next time you deploy a Web Part that has some support files, don't forget to look at ClassResources before putting files elsewhere.

-Maurice

 

re: Web Part Class Resources - a forgotten storage location
Great post Maurice!
 
I vaguely remember coming across that a couple years ago when studying for a TS test. 
 
Guilty as charged for putting images into the layouts/images folder!
Russ Houberg @ 8/6/2009 12:12 PM
re: Web Part Class Resources - a forgotten storage location
Wouldn't attaching CSS files in the resources directory require re-compiling the webpart each time you want to change a font size, or CSS attribute.
 
I do see a pro in the deployment, as I would not have to create all my resources manually across the site.
 
Just something to consider depending on your audience or application.
Eric Schrader @ 8/19/2009 1:14 PM
re: Web Part Class Resources - a forgotten storage location
Cool article thank you...
 
I tend to upload images to the style gallery in a sub folder.... or TEMPLATE\LAYOUTS\IMAGES... But you raise a valid point I just reviewed a few different products I have installed and there isnt consistency.... How do we propose it? and then enforce it?
 
 
Cheers,
 
-Ivan
 
ps how is the sailing?
Ivan Sanders @ 10/4/2009 12:28 AM
re: Web Part Class Resources - a forgotten storage location
I'm using WSPBuilder and cannot deploy to wpresources since this is outside of the 12-hive.  My workaround is simply going to be to use a subdirectory of the layouts folder to store my webpart resources. 
 
Would anyone be able to tell me why this is a bad thing?  My web parts are wrapped user controls and I just don't see how to cleanly use wpresources with the WSP deployment method.  Thanks.
Mike @ 3/23/2010 8:57 AM
re: Web Part Class Resources - a forgotten storage location
The post outlines some thoughts on why you might prefer to use WPResources or its limitations. 
 
If you are using a tool and it has limitations that prevent you from utilizing a portion of the product, then you have a few choices: contact the tool author for an update or live the existing limitation and effectively use the space that you do have.
 
-Maurice
 
Maurice Prather @ 3/31/2010 8:51 PM
RSS feed
Microsoft Certified Master
MVP Logo
Follow me on Twitter!
Keyword Search
 
View by category
 

Disclaimer:
The contents of this site represent thoughts and opinions of the authors , not those of anyone else - such as past, present and future employers.  This a forum of the exchange of ideas centered on SharePoint technologies.  It is not a support channel.  :)

Copyright © 2004-2010 Maurice Prather, Inc. All rights reserved.