There is a big piece of the puzzle i am missing here.
I have a list with content types enabled. I am programatically trying to add an option to some choice fields in the featureupgrading method. The fields in the field collection are updating, but the fields in list's content types are not.
Code below:
EventReciever.cs
public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
{
SPWeb web = (SPWeb)properties.Feature.Parent;
switch ( upgradeActionName ) {
case "AddChoice":
AddChoice(web.Fields[new Guid(parameters["FieldID"])] as SPFieldMultiChoice, parameters["Choice"]);
break;
}
}
protected static void AddChoice(SPFieldMultiChoice field, string choice) {
if ( field == null ) {
throw new ArgumentNullException("field");
}
field.Choices.Add(choice);
field.Update(true);
}
Template.xml
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="http://schemas.microsoft.com/sharepoint/">
<UpgradeActions>
<VersionRange BeginVersion="0.0.0.0" EndVersion="1.0.1.0">
<CustomUpgradeAction Name="AddChoice">
<Parameters>
<Parameter Name="FieldID">{82E08EC3-0C5C-4565-B0D1-740BDBE4FA93}</Parameter>
<Parameter Name="Choice">METT</Parameter>
</Parameters>
</CustomUpgradeAction>
<CustomUpgradeAction Name="AddChoice">
<Parameters>
<Parameter Name="FieldID">{1C2947AF-86EB-4DD0-95AF-D27FBA8EE3BC}</Parameter>
<Parameter Name="Choice">METT</Parameter>
</Parameters>
</CustomUpgradeAction>
<CustomUpgradeAction Name="AddChoice">
<Parameters>
<Parameter Name="FieldID">{EBA64EAA-000E-46EE-B1DE-C9761899A31B}</Parameter>
<Parameter Name="Choice">METT</Parameter>
</Parameters>
</CustomUpgradeAction>
</VersionRange>
</UpgradeActions>
</Feature>
Thanks for your time.
Update:
According to Derek Gusoff's comment below, i have changed my AddChoice method to the following code below. Still not working. I'm still very lost on how you are supposed to do this.
protected static void AddChoice(SPWeb web, SPFieldMultiChoice field, string choice) {
if ( field == null ) {
throw new ArgumentNullException("field");
}
field.Choices.Add(choice);
field.Update(true);
SPFieldLink fieldLink = new SPFieldLink(field);
foreach ( SPList list in web.Lists ) {
if ( !list.ContentTypesEnabled ) continue;
bool update = false;
foreach ( SPContentType contentType in list.ContentTypes ) {
if ( contentType.FieldLinks[field.Id] == null ) continue;
update = true;
string[] order = ( from SPFieldLink f in contentType.FieldLinks select f.Name ).ToArray();
contentType.FieldLinks.Delete(field.Id);
contentType.FieldLinks.Add(fieldLink);
contentType.FieldLinks.Reorder(order);
contentType.Update(true);
}
if ( update ) {
list.Update(true);
}
}
}