Friday, December 30, 2011

Upgrading WebParts as Part of Feature Upgrade

Have you ever had an opportunity where you need to add web parts using SP upgrade? Alternatively, you have successfully upgraded, but web parts don't show up in the Web Gallery? This happens as the code gets deployed and you can refer to webpart via code, however it will not show up in the gallery.
When you upgrade a feature, SP simply ignores all the ElementManifests it finds in the Feature.XML file. The other thing you have to realize is that by default provisioning only occurs during feature activation and not during the upgrade. How do you ensure that during the feature upgrade a webpart is successfully provisioned and gets displayed in the WebPart Gallery? Here are the steps you have to follow using the easier declarative method:
Assumptions:
Name of Feature : MyFeature
Web Part Provisioned : MobilePasswordReset
Step 1: In your solution, open MyFeature.Template.XML file, this is the file that will have the name of the feature followed by template.XML.
Step 2: This is how your file will look for most of the cases unless there is some upgrade statements.

Step 3: Need to make a few changes to the above file:
-Add Id of Feature.
-Add Scope
-Upgrade Actions->ApplyElementManifest->ElementManifest

This is how your XML should look as per the image.

Now, when you run the upgrade power shell, the web part will be provisioned. What will happen is that SP will look in this location and it the web part gallery. The other thing to note is that anything you write in customupgradeaction will not help, unless you write code in that event to add webpart to gallery.