Update existing site column properties with CSOM

Hi,

Some time we want to update existing Site Column. Reason may be any thing i.e. I want to add more choices in Choice list & want to set default choice value as well.


This is very easy in CSOM.

Here is my default xml from existing site column :

<Field Type="Choice" DisplayName="Office Location"
    Required="FALSE" EnforceUniqueValues="FALSE"
    Indexed="FALSE" Format="Dropdown"
    FillInChoice="FALSE" Group="CustomColumns"
    ID="{e57a1e08-f062-4b3c-9dc1-47ee2d0805b0}"
    SourceID="{ad0c8572-a4b6-40c3-a359-d282eae885ce}"
    StaticName="OfficeLocation" Name="OfficeLocation"
    Version="2" CustomFormatter="">
        <Default>Main Location</Default>
        <CHOICES>
            <CHOICE>Main Location</CHOICE>
            <CHOICE>Head Quarter</CHOICE>
            <CHOICE>Branch</CHOICE>
        </CHOICES>
</Field>


Now what I want here :
    1. Add new choice Main Office
    2. Set this choice as well as default value for the choice column
   
This can be achievable by CSOM. How let's see :


    string xml="<Field Type="Choice" DisplayName="Office Location"
    Required="FALSE" EnforceUniqueValues="FALSE"
    Indexed="FALSE" Format="Dropdown"
    FillInChoice="FALSE" Group="CustomColumns"
    ID="{e57a1e08-f062-4b3c-9dc1-47ee2d0805b0}"
    SourceID="{ad0c8572-a4b6-40c3-a359-d282eae885ce}"
    StaticName="OfficeLocation" Name="OfficeLocation"
    Version="2" CustomFormatter="">
        <Default>Main Office</Default>
        <CHOICES>
            <CHOICE>Main Office</CHOICE>
            <CHOICE>Main Location</CHOICE>
            <CHOICE>Head Quarter</CHOICE>
            <CHOICE>Branch</CHOICE>
        </CHOICES>
</Field>
";


    using (ClientContext clientContext = new ClientContext(shpSite))
            {
                clientContext.Credentials = onlineCredentials;
                Field sitefield = clientContext.Site.RootWeb.Fields.GetByTitle("Office Location");
                sitefield.SchemaXml = xml;
                sitefield.UpdateAndPushChanges(true);
                clientContext.ExecuteQuery();
            }
   

That's it.

Comments

Popular Posts

SharePoint Interview Questions and Answers

Download Infopath Form Templates

How to get current logged user information using JavaScript ?

Steps to set Form based authentication (FBA) for SharePoint 2010

SharePoint Interview Questions and Answers II

Get List Items - JavaScript

Change Language for current user with JSOM in SharePoint Online

Cross Site List Rollup Web Part for SharePoint 2010

Hide Recently Modified Items

SharePoint 2010 CSS Chart