Managing Resources in a Custom Web Part

When you write a custom SharePoint web part you often need to include references to resources of some kind, such as images, CSS, client-side scripts or other file system based resources. You can manage these resources in several different ways; which method is best depends on your specific situation. This article looks at how resources come into play within web parts and what additional steps you need to keep in mind when writing SharePoint web parts.

In pre-ASP.NET 2.0 days, you had to deploy resources alongside your controls and bundle them together with some sort of installer. That led to deployment issues and significant frustration. However, ASP.NET 2.0 introduced a new option—Web Resources—which lets you embed resources directly within your control's assembly.

The Windows SharePoint Services (WSS) 3.0 framework has its own methods for dealing with file system based resources, so your custom web parts can take advantage of two different architectures for resource deployment: embedded resources and linked resources.

Embedded Resources

Embedded resources let you leverage built-in ASP.NET methods for resource management. Using this technique, you embed your resources directly into your control's compiled assembly, and then use framework-provided methods to reference those resources.

Embedded resources are advantageous, because the DLL and your resource files are always kept together, ensuring that when you update your DLL you can also update your resources at the same time by altering only one file. However that also means that if you want to make simple changes to a resource, such as minor changes to a JavaScript file or updating an image, you'll have to redeploy your entire assembly.

Resource embedding works by referencing a specific URL in your rendered HTML. This URL points to a virtual file called WebResource.axd and includes a key within the query string. Here's an example:


In a nutshell, ASP.Net provides an HttpHandler that handles requests for WebResource.axd and returns the resource matching the key.

To embed a resource in this manner you must complete three steps:

Tell Visual Studio to Embed the Resource In the Assembly

First, add the resource to your Visual Studio project. For simplicity, I recommend adding resources at the root level of your project. Next, in the Project Properties window, set the Build Action for this resource to "Embedded Resource" (see Figure 1).

Figure 1. Embedding a Resource:
Set the Build Action to "Embedded Resource" in the project properties.

Update the AssemblyInfo File to Reference the Resource from the Assembly

Figure 2. Show All Files:
Click this button to view the AssemblyInfo file in VB.NET projects.

Under the Properties area of your project, you'll find a file (for C# applications) called AssembyInfo.cs. In VB.NET projects, the file resides in the ‘My Project’ folder, and will be called AssemblyInfo.vb; however, to be able to see the AssemblyInfo.vb file in VB you need to click on the "Show All Files" button on the Solution Explorer toolbar (see Figure 2).

Open the AssemblyInfo file and add code similar to the following for each resource:

// in C#
[assembly: System.Web.UI.WebResource(
' in VB
<Assembly: System.Web.UI.WebResource(

Note that the preceding line is syntactically correct, but you'll need to substitute specific information for these items:

  • namespace: Substitute the root namespace for your project.
  • filename.ext: Use the filename and extension for your resource.
  • mime: Insert the MIME type appropriate for this resource (e.g. "text/javascript" for JavaScript files).

Get a URL Reference to this Resource in Your Code

At some point in the code for your custom web part you are going to need to retrieve the URL for this resource. That could be when setting the ImageUrl property of an Image control or when embedding a linked JavaScript file onto the page using RegisterClientScriptInclude. To get the resource URL, call the ScriptManager class's GetWebResourceURL method. There is an instance of the ScriptManager class named ClientScript on every ASP.Net page. Here's an example:

// in C#
String strResourceURL = Page.ClientScript.GetWebResourceURL(
' in VB
Dim strResourceURL as String = _
   Page.ClientScript.GetWebResourceURL(Me.GetType(), _

After building and deploying your code to a SharePoint site, you can check the rendered HTML to see the URL reference to WebResource.axd (which will look similar to the example toward the start of this section.)

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.