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=1.0.0.0, 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:

[ToolboxItemAttribute(false)]
[Guid("A4D3BE9B-E2D6-42A4-B4F9-D78911C214E8")]
public class MyWebPart : WebPart{...}

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

$SharePoint.Type.GUID.FullName$

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.

6 Comments

  • Trackback said

    Yesterday Todd Bleeker (SharePoint MVP) wrote a post about the SharePoint Project SPI's where he explains how SPI effectively are folders within a SharePoint solution. I thought that I should continue...

  • Trackback said

    One of the really great features in SharePoint 2010 is the Sandbox, which allows the end-users to upload solutions using the web interface, instead of relying on administrators adding the solutions di...

  • jjiyiepx said

    Qcr1WC <a href="http://cqilvwnevpll.com/">cqilvwnevpll</a>, [url=http://gcsqnsojmmix.com/]gcsqnsojmmix[/url], [link=http://hvyiccfmmabx.com/]hvyiccfmmabx[/link], http://pqawwwmhusem.com/

  • Trackback said

    Today an article was published on the SharePoint Developer Team Blog called Calling a WCF Service using jQuery. The content and purpose of the article is good and interesting but the way it is impleme...

Add a Comment

Facebook

About Wictor...

Wictor Wilén is a Director and SharePoint Architect working at Connecta AB. 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 three consecutive years.

And a word from our sponsors...

SharePoint 2010 Web Parts in Action