Replacement Tokens for VS2010 and SharePoint Projects

Problem:

In a recent project, I needed to create an asp.net handler that returns an RSS feed for a custom web part. I added a new text file with the appropriate file extension and set the contents of the file to:

<%@ Assembly Name=&quot;Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot; %>
<%@ Assembly Name=&quot;$SharePoint.Project.AssemblyFullName$&quot; %>
<%@ WebHandler Class=&quot;IdeaCenterWebParts.Code.SiteActivityRSSHandler&quot; %>

After compiling and deploying the project to SharePoint, each time I accessed the handler I received an error about an assembly not being found. To avoid delaying the project, I simply provided the full assembly name (IdeaCenterWebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5e88564ecf55ef51) and went about my business.

I received some requests to change the project and to try to avoid versioning conflicts I updated my file version from 1.0.0.0 to 1.5.0.0 and then my error returned. This lead me down a path of discovery.

Discovery:

One of the nice things about developing in Visual Studio for SharePoint 2010 projects are replacement tokens provided in the project. First, tokens are replaced at the end of the packaging of the solution so it will only work with non-compiled files. This is great for features and other code depending on these tokens to be dynamically created. However, there are only a handful of files where tokens are searched and replaced. The default list includes many of the common ones that you would typically use in a project:

  • XML
  • ASCX
  • ASPX
  • Webpart
  • DWP

Notice, WCF Services Files (SVC), ASP.NET Handler Files (ASHX) and ASP.NET SOAP Web Services are not included in the list. This means the files will be ignored and the token will remain. I figured there has to be a way to resolve this so I read on further in the article on MSDN. According to this article, the simple way to fix the problem is to open the project file (.csproj/.vbproj) and edit the XML to include your additional targets. This is done on a project by project basis.

Simply add a line similar to the following to the first PropertyGroup:

<TokenReplacementFileExtensions>ashx;asmx</TokenReplacementFileExtensions>

Here’s my project’s first PropertyGroup node:

<PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{3B59383A-D773-43CD-94D2-EF58AAA613CD}</ProjectGuid>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>IdeaCenterWebParts</RootNamespace>
    <AssemblyName>IdeaCenterWebParts</AssemblyName>
    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <ProjectTypeGuids>{BB1F664B-9266-4fd6-B973-E1E44974B511};{14822709-B5A1-4724-98CA-57A101D1B079};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
    <SandboxedSolution>False</SandboxedSolution>
    <TokenReplacementFileExtensions>ashx</TokenReplacementFileExtensions>
</PropertyGroup>

Now, if I change the version number or assembly name of the project my references stay up-to-date!

Advertisements

One thought on “Replacement Tokens for VS2010 and SharePoint Projects

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s