Thursday 1 August 2013

SharePoint 2010 MSBuild Replacable Tokens

If you have the following error:
Could not load the assembly '$SharePoint.Project.AssemblyFullName$'. Make sure that it is compiled before accessing the page. 
This may be because Visual Studio has not replaced the tokens in some of your project files. These tokens are used inside certain files as the actual value is not known at design time.
In our project the the developer had written a ASMX webservice and by default the token within the asmx file is not replaced by the compiler.
The asmx file before compilation had the following:

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ WebService Language="C#" Class="foo.bar.WebServices.MyProjectsService" %>

It should be converted to the following after compilation:
<%@ Assembly Name="Foo.Bar.Project, Version=1.0.0.0, Culture=neutral, PublicKeyToken=21120a5e44d08c21" %> <%@ WebService Language="C#" Class="foo.bar.WebServices.MyProjectsService" %>

By default Visual Studio will only replace tokens in certain files, although it can be configured to replace more.

Adding additional file extensions for token replacement

You can either change this for the Visual Studio environment, or for a particular project.  Having the setting int he project file ensures that a team project (e.g. where many people are using TFS) will have consistent builds.

To Change for the current Project

Go to the Project file


    Debug
    AnyCPU
.
.
.
    
myextension;yourextension


    To change for ALL sharePoint Projects on the local machine.   
1. Goto the following file: Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\SharePointTools\Microsoft.VisualStudio.SharePoint.targets
2. Add the files extensions into the following section:


$(TokenReplacementFileExtensions);xml;aspx;ascx;webpart;dwp;asmx

This will replace the tokens after the project is compiled.

References:
It's worth reading the following: http://msdn.microsoft.com/en-us/library/ee231545.aspx