Adding SharePoint class resources to your feature using WSPBuilder

I was having some troubles adding class resources to my SharePoint feature, while using WSPBuilder. I searched quite some, and here’s what I did to make it work.

First you need to start with a good project. You can build one pretty fast if you create a new WSPBuilder project and add a “WebPart with feature” to the project. Now the WSPBuilder templates should have created a project with the 12-hive folder inside your project where your webpart feature should be specified.

folderstructureIf you want to add a nice .jpg image to your feature, there are two ways. Embedding it in a .resx file (that’s another blogpost) or copying it to the filesystem of the SharePoint server. Of course not manually, but from inside your feature. Here’s how.

  1. Add a new folder and name it “80”
  2. Add a new folder and give it the name of your project
  3. Add the .jpg to this folder, or create subfolders if you’d like.
  4. Add the following code to your webpart to retrieve the image.

Imagine that your project is called “WebPartResources” then you’d have the structure in your project as shown in the image on the right. Right-click the project, build the WSP and you should have the roughly the following manifest.xml inside your .wsp file.

<?xml version="1.0" encoding="utf-8"?>
<!-- Solution created by WSPBuilder. 7/23/2009 10:46:15 PM  -->
<Solution xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" SolutionId="c8d22333-f09b-4819-b026-a6cb7d64b41a" ResetWebServer="True" xmlns="http://schemas.microsoft.com/sharepoint/">
  <Assemblies>
    <Assembly Location="WebPartResources.dll" DeploymentTarget="GlobalAssemblyCache">
      <SafeControls>
        <SafeControl Assembly="WebPartResources, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6763c0814d065309" Namespace="WebPartResources" TypeName="*" Safe="True" />
      </SafeControls>
      <ClassResources>
        <ClassResource Location="dennis.jpg" FileName="dennis.jpg" />
      </ClassResources>
    </Assembly>
  </Assemblies>
  <ApplicationResourceFiles>
    <ApplicationResourceFile Location="WebPartResourcesdennis.jpg" />
  </ApplicationResourceFiles>
  <FeatureManifests>
    <FeatureManifest Location="WebPartWithResourcesfeature.xml" />
  </FeatureManifests>
</Solution>

After deploying it, you should be able with the following code to retrieve the image from this folder, although version and publickeytoken could possible change! 🙂

C:Program FilesCommon FilesMicrosoft Sharedweb server extensionswpresourcesWebPartResources1.0.0.0__6763c0814d065309

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

Image image = new Image();
image.ImageUrl = classResourcePath + "/dennis.jpg";

6 Responses

  1. Dennis van der Stelt says:

    Wow, my first post on SharePoint, although I’ve been busy with SharePoint pretty long now… More posts coming hopefully! 🙂

  2. Hi,

    I am creating a WSP solution file for one of my WebPart project. I want to deploy my resource files under 80/WPRESOURCES directory instead of 12 Hives and application 80/resource directory but unable to create it with manifest and ddf files.
    Have you any idead about it ???
    Please suggest me…

    Thank you,
    Saurabh Kumar Singh

  3. Dennis van der Stelt says:

    Not from the top of my head, I’d have to check wspbuilder.codeplex.com myself. You’d better ask someone there.

  4. I found out that the class ‘Microsoft.SharePoint.WebPartPages.WebPart’ has a variable or property – ‘ClassResourcePath’ (with capital ‘C’) which does exactly that!

  5. Craig says:

    Hmm. When I tried it, the file was deployed to a folder named ‘resources’ in the site, instead of ‘wpresources’. It wasn’t a typo. Any ideas?

    Thanks.

  6. Dan says:

    I’m trying to put some common images/css into sharepoint , any idea where these should go?

Click on a tab to select how you'd like to leave your comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.