Search code examples
sharepoint-2013caml

Adding choices using caml query


I am writing a caml query to add column in sharepoint. The field is 'choice' type.

I have written the query like, choice1 choice2 />

This is throwing an error that name can't start with '<' character. Can anyone please help to tell how I can add choices for a field using caml query.

Thanks in advance.


Solution

  • Caml Query is used to filter field value not adding column, if you want to add choices into a choice type field, you could add the AddFieldAsXml like this which will involve choices, write all the needed choices in the xml:

            string siteUrl = "http://sp/sites/dev";
            ClientContext clientContext = new ClientContext(siteUrl);
            clientContext.Credentials = new NetworkCredential("Administrator", "Access1", "Contoso");
            List oList = clientContext.Web.Lists.GetByTitle("DemoList1");
            Field catField = oList.Fields.AddFieldAsXml(@"
                <Field Type='Choice' DisplayName='Category' Format='Dropdown'>
                    <Default>IT</Default>
                    <CHOICES>
                      <CHOICE>IT</CHOICE>
                      <CHOICE>Sales</CHOICE>
                    </CHOICES>
            </Field>", true, AddFieldOptions.DefaultValue);
            oList.Update();
            clientContext.ExecuteQuery();
    

    enter image description here