This post is migrated from previous hosting provider. There are still some issues with old posts. Please make a comment on this post with any issues.

Nifty trick with Visual Studio 2010 replaceable parameters for SharePoint 2010 Web Parts

Tags: Visual Studio, Web Parts, SharePoint 2010

MP900408848[1]If you have been working with SharePoint 2010 development using Visual Studio 2010 you have most certainly stumbled upon the new replaceable parameters that replaces data in your solution files during the packaging process. For instance Visual Studio uses $SharePoint.Project.AssemblyFullName$ in the Web Part control description (.webpart) files and this is replaced with the assembly full name (strong name) during packaging. By default it looks like this when you create a new Web Part:

type name="Project.MyWebPart, $SharePoint.Project.AssemblyFullName$" />

After the packaging and when deployed into SharePoint it looks like this:

type name="Project.MyWebPart, Project, Version=, Culture=neutral, PublicKeyToken=54c0c201dd8d1c31" />

This saves you some time when changing versions etc of the assembly.

But what about if you change the name of the class or the namespace, then you have to rename a whole lot of things; the CS file, the .webpart file, optionally the element manifest and of course all references. A better way is to use another replaceable parameter that replaces the token with the full name of the type. First of all you need to specify a Guid attribute on the type (Web Part class in this case) like this:

public class MyWebPart : WebPart{...}

Then you update the .webpart file to use the replaceable parameter that has the format of:


The GUID must be lower-case and your updated .webpart file should look like this after copying the Guid value from the attribute:

type name="$SharePoint.Type.a4d3be9b-e2d6-42a4-b4f9-d78911c214e8.FullName$, $SharePoint.Project.AssemblyFullName$" />

Visual Studio 2010 will now replace this during runtime with whatever you Web Part class name and namespace is, so you can feel safe renaming and refactoring.

Even better is that this works for all other cases where you need to reference a type in an element manifest, user control or similar. Out-of-the-box the following file types will be parsed and parameters replaced; XML, webpart, DWP, ASCX and ASPX. For instance you might have added a event receiver for a content type - just add the same two tokens used in the sample above in the Assembly and Class elements of the Receiver element.


Comments have been disabled for this content.

About Wictor...

Wictor Wilén is the Nordic Digital Workplace Lead working at Avanade. Wictor has achieved the Microsoft Certified Architect (MCA) - SharePoint 2010, Microsoft Certified Solutions Master (MCSM) - SharePoint  and Microsoft Certified Master (MCM) - SharePoint 2010 certifications. He has also been awarded Microsoft Most Valuable Professional (MVP) for seven consecutive years.

And a word from our sponsors...