SharePoint 2010 Dev TidBit - Use the tokens in Visual Studio 2010!

Thursday, December 3, 2009 2:02 AM

When creating SharePoint 2007 solutions, such as custom application or site pages that include code behind files, you’re often stuck having to manually enter the fully qualified strong name (aka: 5-part name) of the class of the code behind piece of the page within the <% @Page %> directive. Specifically you’re doing something like this in the Inherits attribute: Namespace.ClassName, AssemblyName, Version=, Culture=neutral, PublicKeyToken=UnreadableString.

Thankfully Visual Studio 2010 addresses this for us and makes like SIGNIFICANTLY better. There are now a slew of tokens that are replaced when the project is built. So you could now instead put something like this in your custom site and application pages:

<% @Page […] Inherits="$SharePoint.Project.AssemblyFullName$” %>

Then, when the project is built it will automatically replace this token with the real value… very slick! Here’s a list of all the available tokens Visual Studio 2010 replaces for you.

By default not all file types run through this replacement process. The reason not all files are part of this replacement process is due to performance reasons… only the most common file types are addressed: ASPX, ASCX, XML, WEBPART and DWP (is that thing still around?).

However if you’re looking to include another file type in the list of files to be part of this replacement process you can add to this list. for instance when you are creating custom WCF Services (*.SVC). You can do this either in your project or for your entire workstation. To do this for your project, open the project file (eg: *.csproj) and within a add a new entry who’s contents are just the file extension. If you want to make it global to all projects on your workstation, follow the same process above except do it to the SharePoint MSBuild targets file (c:\Program Files\MSBuild\Microsoft\VisualStudio\v10.0\SharePointTools\Microsoft.VisualStudio.SharePoint.targets). This file is used in all builds of projects that use the Visual Studio 2010 SharePoint Tools.

*note this only happens after the project is compiled so you shouldn’t use these in code files.

comments powered by Disqus