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