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 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#.

regards

gidion

share|improve this question
    
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
add comment

1 Answer

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">
 <Add>
  <Field ID="{1174073D-CD08-4A8A-A70E-5199200E04AC}" 
         Name="TestField"  
         Required="TRUE" 
         Indexed="TRUE" 
         MaxLength="255" 
         Type="Text" 
         DisplayName="TestField" />
 </Add>
</Fields>

To deserialize xml file I used following class:

[Serializable]
[XmlRoot("Fields")]
public class FieldsConfiguration
{
    [XmlAttribute]
    public string ListUrl { get; set; }

    [XmlArrayItem(typeof(Field))]
    public List<Field> Add { get; set; }
}

public class Field
{
    [XmlAttribute]
    public string ID { get; set; }

    [XmlAttribute]
    public string Name { get; set; }

    [XmlAttribute]
    public string Type { get; set; }

    [XmlAttribute]
    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))
                continue;
            //get field schema from xml
            var fieldSchema = doc.SelectSingleNode(string.Format("/Fields/Add/Field[@Name='{0}']", field.Name));
            if (fieldSchema == null || fieldSchema.Attributes == null)
                continue;
            //hack for Lookup fields
            if(!string.IsNullOrEmpty(field.List))
            {
                var lookupList = web.GetListNoThrow(field.List);
                if (lookupList == null)
                    continue;

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

Provided code supports all field types except Taxonomy fields.

share|improve this answer
    
Thank you for your code, in your example, i must write Code for add a new field to a SharePoint list. Can i use your xml file to add a fiedd on feature upgrade without code? –  gidion1983 Apr 29 '13 at 8:07
    
Check this link blogs.msdn.com/b/sanjaynarang/archive/2012/02/06/… –  Andrew Apr 29 '13 at 8:33
1  
This is not the Solution i need, i found this post sohilmakwana.wordpress.com/2012/11/15/… the MyField2 are add to an ContentTyp but i do not have a content Type i was create the List with c#. Is it posible to Update this List without c# ? –  gidion1983 Apr 29 '13 at 15:45
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.