Add to My Yahoo! | Google Reader or Homepage | Add to Windows Live | Add to Windows Live Alerts

Wictor Wilén

Microsoft Certified Master (MCM) - SharePoint 2010 | Microsoft Most Valuable Professional (MVP) - SharePoint Server MVP | Author

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

Posted at 2010-08-03 01:38 by Wictor Wilén in Visual Studio , Web Parts , SharePoint 2010 with 6 comments.

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.

Comments and trackbacks

#  Understanding folders and namespaces in Visual Studio 2010 SharePoint Solutions, Packages and Features by Trackback
Screenshot from websnpr 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...
#  Understanding folders and namespaces in Visual Studio 2010 SharePoint Solutions, Packages and Features by Trackback
Screenshot from websnpr Yesterday Todd Bleeker (SharePoint MVP ) wrote a post about the SharePoint Project SPI 's where he
#  Sandboxed workflow activities in SharePoint 2010 by Trackback
Screenshot from websnpr 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...
#  Sandboxed workflow activities in SharePoint 2010 by Trackback
Screenshot from websnpr One of the really great features in SharePoint 2010 is the Sandbox, which allows the end-users to upload
#  DMSjcSZGtO by jjiyiepx
Screenshot from websnpr Qcr1WC cqilvwnevpll, [url=http://gcsqnsojmmix.com/]gcsqnsojmmix[/url], [link=http://hvyiccfmmabx.com/]hvyiccfmmabx[/link], http://pqawwwmhusem.com/
#  Calling a WCF Service using jQuery in SharePoint - the correct way by Trackback
Screenshot from websnpr 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...
Make a comment on this post:
Subject:  

Your name:  
Your Url:  
Note: submissions may have to be approved before being visible, so don't submit your comment multiple times.