Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am creating a custom WSP in Visual Studio. This solution has several custom content types with custom fields as well as a list instance with content types enabled. Unfortunately, i need to change the static name of a few of my fields & deploy to several different servers.

I have tried the following code:

Feature.Web.Template.xml

<Feature xmlns="http://schemas.microsoft.com/sharepoint/">
    <UpgradeActions>
        <VersionRange BeginVersion="0.0.0.0" EndVersion="1.0.0.1">
            <CustomUpgradeAction Name="RenameField">
                <Parameters>
                    <Parameter Name="FieldID">{060A9145-5D22-4D6C-A049-2438D8E0CDD8}</Parameter>
                    <Parameter Name="NewName">ProductDescription</Parameter>
                </Parameters>
            </CustomUpgradeAction>

            <CustomUpgradeAction Name="RenameField">
                <Parameters>
                    <Parameter Name="FieldID">{FBDDC10E-4872-4A4D-98C1-E2FD19658D24}</Parameter>
                    <Parameter Name="NewName">DiscussionAccessOther</Parameter>
                </Parameters>
            </CustomUpgradeAction>
        </VersionRange>
    </UpgradeActions>
</Feature>

Feature.EventReceiver.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 "RenameField":
            RenameField(web, new Guid(parameters["FieldID"]), parameters["NewName"]);
            break;
    }
}

protected void RenameField(SPWeb web, Guid fieldId, string newName) {
    if ( web.Fields.Contains(fieldId) ) {
        RenameField(web.Fields[fieldId], newName);
    }

    foreach ( SPList l in web.Lists ) {
        if ( l.Fields.Contains(fieldId) ) {
            RenameField(l.Fields[fieldId], newName);
        }

        if ( l.ContentTypesEnabled ) {
            foreach ( SPContentType ct in l.ContentTypes ) {
                if ( ct.Fields.Contains(fieldId) ) {
                    RenameField(l.Fields[fieldId], newName);
                }
            }
        }
    }
}

private void RenameField(SPField field, string newName) {
    field.StaticName = newName;
    field.Update();
}

This updates the field in the field gallery, but not the list instance. What am i missing?

Thanks.

share|improve this question
add comment

1 Answer

Couple of things:

  1. You should be updating SPContentType.FieldLinks, and not SPContentType.Fields. The latter is only a construct that represents the merge between the field's list definition and any specific modifications for that content type in that list.

  2. Try using SPField.Update(boolean) to propagate changes down the hierarchy from the site-level definitions.

share|improve this answer
    
Thanks for the insight. Unfortunately, SPFieldLink.Name is read only. Using SPField.Update(true) by itself didn't work. –  Markus Apr 12 '12 at 14:10
    
Ah, you're right, SPFieldLink.Name maps to SPField.InternalName. So I guess there is no static name for a fieldref inside a content type, it's always bound to the underlying field. There's no need (and no way) to update it in the content type then. The problem must be in the List loop. –  Louis Apr 13 '12 at 1:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.