Site columns
A site column is a reusable column definition or a template, which you can assign to the multiple lists, Content Types, across multiple SharePoint sites.
Site columns are useful, if your organization wants to establish some consistent settings across the lists and the libraries.
- $(document).ready(function()
- {
- addField();
- });
-
- function addField() {
- var dfd = $.Deferred();
- var context = new SP.ClientContext.get_current();
- var web = context.get_web();
- var fields = web.get_fields();
- var fldSchema = '<Field Type="Text" \
- Name="TestSiteColumn" \
- DisplayName="Test SiteColumn" \
- Required="TRUE" \
- Group="Gowtham Columns" />';
- fields.addFieldAsXml(fldSchema);
- context.executeQueryAsync(Success,Fail);
- return dfd.promise();
- }
-
- function Success() {
- alert('SiteColumn created');
- dfd.resolve();
- }
-
- function Fail() {
- alert('site column Creation Failed');
- dfd.reject();
- }
- </script>
If you want to create a site column in client-side code, you must use the FieldCollection.AddFieldAsXml method. This method works in exactly the same way as its Server-side equivalent, where you built your field schema as a CAML string and pass it as an argument to the method.
Thanks for reading my blog. Please post your feedback, so that I can improve my future articles and blogs.