Update Assembly References in SharePoint Related .asmx Files Automatically

It has been quite a while without a post and I better start with an easy one: how to automatically keep your assembly reference in a SharePoint related .asmx file updated.

If you wonder how .asmx files relate to SharePoint solutions you can find some information about that here: AJAX Style Web Parts in SharePoint the Easy Way.

Your .asmx file will consist of a single line like the one below. Getting the assembly’s strong name is a challenge of it’s own but having to keep it up to date is what will worry you most:

<%@ WebService Language="C#" Class="AjaxWP.AjaxWS, AjaxWP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d76004e99c897b9f" %>

Change that line to the one below to let MSBuild do the work for you:

<%@ WebService Language="C#" Class="AjaxWP.AjaxWS, $SharePoint.Project.AssemblyFullName$" %>

The final step is to tell MSBuild to replace tokens in .asmx files. You can do that for a specific project or for all projects you build on that machine.

Replacing Tokens for a Specific Project

To let MSBuild replace tokens for a specific project add a TokenReplacementFileExtensions element to your .csproj file like shown below. To which PropertyGroup element you add it doesn’t matter – it just has to be added before the Import elements.

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build"
  xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- ... -->
  <PropertyGroup>
    <SignAssembly>true</SignAssembly>
    <TokenReplacementFileExtensions>asmx</TokenReplacementFileExtensions>
  </PropertyGroup>
  <!-- ... -->
  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\SharePointTools\Microsoft.VisualStudio.SharePoint.targets" />
</Project>

Replacing Tokens for All Projects

To let MSBuild replace tokens for all projects edit C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\SharePointTools\Microsoft.VisualStudio.SharePoint.targets – be shure to make a backup copy first. Open the editor as administrator and in the file extend the TokenReplacementFileExtensions element by adding “;asmx” like shown below.

<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- ... -->
  <PropertyGroup>
    <TokenReplacementFileExtensions>$(TokenReplacementFileExtensions);xml;aspx;ascx;webpart;dwp;asmx</TokenReplacementFileExtensions>
  </PropertyGroup>
  <!-- ... -->
</Project>

External References

Microsoft’s documentation on replaceable parameters

Advertisements
This entry was posted in SharePoint and tagged , . Bookmark the permalink.

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