- Business logic
- application development
- Visual C#
- Visual Studio
- Microsoft Office
- Exchange server
- SharePoint 2010
- document management
- Web development
- Web sites
- Web Parts
- SharePoint Service Account
- SharePoint administration
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.
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( "namespace.filename.ext","mime")] ' in VB <Assembly: System.Web.UI.WebResource( "namespace.filename.ext","mime")>
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.
Get a URL Reference to this Resource in Your Code
// in C# String strResourceURL = Page.ClientScript.GetWebResourceURL( this.GetType(),"namespace.filename.ext"); ' in VB Dim strResourceURL as String = _ Page.ClientScript.GetWebResourceURL(Me.GetType(), _ "namespace.filename.ext")
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.)