10. February 2012 13:17
Hi all! Today I’m going to show example of how to write clean and understandable code when using feature upgrade. Of course, solution which we are using is not perfect, but it rather solid, easy to use, easy to read, easy to extend. If you have any suggestions, improvements, or event better and cooler solution – you are welcome in comments . Ok, lets start.
You are already know, that with sharepoint 2010 you can upgrade your features by adding this sample xml:
<VersionRange BeginVersion="0.0.0.0" EndVersion="0.0.0.09">
<Parameter Name="TitlePrefix">Upgrade Time:</Parameter>
CustomUpgradeAction is not the only allowed tag inside VersionRange, but I’m going to focus on it, because with this tag we can specify actions, which invokes by code. To use above action you need to specify upgrade actions receiver and override FeatureUpgrading method. Very straightforward and simple implementation of this method for our above example can looks like this one:
public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
var web = properties.Feature.Parent as SPWeb;
var list = web.Lists["TestUpgradeList"];
var item = list.Items.Add();
item["Title"] = parameters["TitlePrefix"] + DateTime.Now.ToShortTimeString();
//do other stuff
Pretty cool, but what if you have 20-30 different actions? More...