i had create a list programmatically on Feature_activated. Now i want to add a new Field to my list with an XML File on UpgradeActions from the Feature. I know the solution for add a Field with C#.



  • As I understood you need to add new field to existing list in method FeatureUpgrading in Feature Receiver. Am I right? – Andrew Apr 25 '13 at 15:12
  • Yes you are right, i need an example with xml – gidion1983 Apr 26 '13 at 8:35

I implemented similar behavior some time ago, I used xml file with following structure:

<?xml version="1.0" encoding="utf-8" ?>
<Fields ListUrl="Lists/Test">
  <Field ID="{1174073D-CD08-4A8A-A70E-5199200E04AC}" 
         DisplayName="TestField" />

To deserialize xml file I used following class:

public class FieldsConfiguration
    public string ListUrl { get; set; }

    public List<Field> Add { get; set; }

public class Field
    public string ID { get; set; }

    public string Name { get; set; }

    public string Type { get; set; }

    public string List { get; set; }

I added xml file to feature definition. To get xml file name in my feature receiver I used code:

string fileName = Path.Combine(properties.Definition.RootDirectory, "FileName.xml");

Method that applies changes to list:

    /// <summary>
    /// Method adds fields to list from xml file.
    /// </summary>
    /// <param name="config">Deserialized xml file</param>
    /// <param name="doc">XmlDocument of xml file</param>
    /// <param name="web">Current web</param>
    /// <param name="list">Current list</param>
    protected void AddFieldsFromFile(FieldsConfiguration config, XmlDocument doc, SPWeb web, SPList list)
        foreach (var field in config.Add)
            //check if field is already exist
            if (list.Fields.ContainsField(field.Name))
            //get field schema from xml
            var fieldSchema = doc.SelectSingleNode(string.Format("/Fields/Add/Field[@Name='{0}']", field.Name));
            if (fieldSchema == null || fieldSchema.Attributes == null)
            //hack for Lookup fields
                var lookupList = web.GetListNoThrow(field.List);
                if (lookupList == null)

                fieldSchema.Attributes["List"].Value = lookupList.ID.ToString();
            list.Fields.AddFieldAsXml(fieldSchema.OuterXml, false, SPAddFieldOptions.AddFieldInternalNameHint);

Provided code supports all field types except Taxonomy fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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