1

I'm trying to create site-columns with following types: Text, Note, Number, Choice, User, MultiUser, MultiChoice

I've implemented it for all the types except for User, MultiChoice and MultiUser. Can't seem to find how to achieve it. This is what I've achieved so far.

StringBuilder choiceFieldGenericShema = new StringBuilder();
                    choiceFieldGenericShema.Append("<Field Type='Choice' Format='Dropdown' Group='{1}' Name='{0}' DisplayName='{2}' Status='{3}' > <CHOICES> ");
                    choiceFieldGenericShema.Append("</CHOICES>  </Field> ");
                    string choicFieldSchema = string.Format(choiceFieldGenericShema.ToString(), columnItem.Title, columnItem.GroupName, columnItem.Name, columnItem.Status);
                    var newField = web.Fields.AddFieldAsXml(choicFieldSchema, true, AddFieldOptions.DefaultValue);
                    clientContext.Load(newField);
                    clientContext.ExecuteQuery();

Can anyone please suggest a way following the above method to achieve my task? Thanks in advance!

2

i have used below code for multichoice and choice site columns.

string choiceFieldListOfValues = "A;B;C";
                string[] optionSetArray = choiceFieldListOfValues.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
                StringBuilder choiceFieldGenericShema = new StringBuilder();
                if (spColumnType == "Choice")
                {
                    choiceFieldGenericShema.Append("<Field Group='{3}' Name='{0}' DisplayName='{1}' Type='{2}' " + strValue + " > <CHOICES> ");
                }
                else if (spColumnType == "MultiChoice")
                {
                    choiceFieldGenericShema.Append("<Field Group='{3}' Name='{0}' DisplayName='{1}' Type='{2}' > <CHOICES> ");
                }
                foreach (string optionValue in optionSetArray)
                {
                    if (!string.IsNullOrEmpty(optionValue))
                    {
                        choiceFieldGenericShema.Append("<CHOICE>" + optionValue.Trim() + "</CHOICE> ");
                    }
                }
                choiceFieldGenericShema.Append("</CHOICES>  </Field> ");
                columnGenericSchema = choiceFieldGenericShema.ToString();
                string columnSchema = string.Format(columnGenericSchema, spColumn[0], spColumn[0], spColumnType, ConfigurationConstants.SharepointSiteColumsGroupName);
                var newField = rootWeb.Fields.AddFieldAsXml(columnSchema, true, SP.AddFieldOptions.AddFieldInternalNameHint);
                clientContext.ExecuteQuery();
1

One of the easiest ways to know the XML of a filed is to create the field from UI first, then use a tool like SharePoint Manager, to check the Xml Schema of the field. The issue in your Choice field is that, you haven't defined any choice options and second you have defined a property named Status which I don't think is a valid property. Here is a sample xml of a choice field:

<Field Type="Choice" DisplayName="Colors" Required="FALSE" Format="Dropdown" FillInChoice="FALSE" Name="Colors">
    <CHOICES>
      <CHOICE>Red</CHOICE>
      <CHOICE>Green</CHOICE>
      <CHOICE>Blue</CHOICE>
    </CHOICES>
</Field>

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.