SharePoint 2013: Creating Site Columns using the JavaScript Object Model
Today's tricky task is creating a SharePoint 2013 site column using the JavaScript object model. It's not actually difficult, but at the time of writing the product documentation doesn't make it clear how to go about it.
If you want to create a site column programmatically in server-side code, you can go about it in various ways. To start with, you retrieve the Fields collection of a SharePoint web. You can then add a field to the collection by calling SPFieldCollection.Add or SPFieldCollection.AddFieldAsXml. However, the client-side object models are a little more limited.
The JSOM and the managed client object models both include a FieldCollection.Add method. However, you can't use these methods to add a create a new field (believe me, I spent some time trying). Instead, these methods are used for adding existing fields to new collections - for example, adding a site column to the fields collection of a list. 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 - you build your field schema as a CAML string and pass it as an argument to the method:
The FieldCollection.AddFieldAsXml overload in the example takes three arguments. The first is the CAML string that defines your column. The second is a Boolean value that indicates whether or not you want to add the field to the default view - this is a moot setting in this case as we're creating a site column rather than a list column. Finally, the third is an SP.AddFieldOptions enumeration value that enables you to combine various options to specify how your column is created.
If you want to create a site column programmatically in server-side code, you can go about it in various ways. To start with, you retrieve the Fields collection of a SharePoint web. You can then add a field to the collection by calling SPFieldCollection.Add or SPFieldCollection.AddFieldAsXml. However, the client-side object models are a little more limited.
The JSOM and the managed client object models both include a FieldCollection.Add method. However, you can't use these methods to add a create a new field (believe me, I spent some time trying). Instead, these methods are used for adding existing fields to new collections - for example, adding a site column to the fields collection of a list. 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 - you build your field schema as a CAML string and pass it as an argument to the method:
var context;
var web;
var fields;
var addField = function () {
context = new SP.ClientContext.get_current();
web = context.get_web();
fields = web.get_fields();
var fieldSchema = '<Field Type="DateTime" \
Name="ExpiryDate" \
DisplayName="Expiry Date" \
Format="DateOnly" \
Required="TRUE" \
Group="Contoso Columns" />';
fields.addFieldAsXml(fieldSchema, false, SP.AddFieldOptions.addFieldCheckDisplayName);
context.executeQueryAsync(onAddFieldSuccess, onAddFieldFail);
}
var onAddFieldSuccess = function () {
alert('Field created');
}
var onAddFieldFail = function () {
alert('Something went wrong');
}
var web;
var fields;
var addField = function () {
context = new SP.ClientContext.get_current();
web = context.get_web();
fields = web.get_fields();
var fieldSchema = '<Field Type="DateTime" \
Name="ExpiryDate" \
DisplayName="Expiry Date" \
Format="DateOnly" \
Required="TRUE" \
Group="Contoso Columns" />';
fields.addFieldAsXml(fieldSchema, false, SP.AddFieldOptions.addFieldCheckDisplayName);
context.executeQueryAsync(onAddFieldSuccess, onAddFieldFail);
}
var onAddFieldSuccess = function () {
alert('Field created');
}
var onAddFieldFail = function () {
alert('Something went wrong');
}
The FieldCollection.AddFieldAsXml overload in the example takes three arguments. The first is the CAML string that defines your column. The second is a Boolean value that indicates whether or not you want to add the field to the default view - this is a moot setting in this case as we're creating a site column rather than a list column. Finally, the third is an SP.AddFieldOptions enumeration value that enables you to combine various options to specify how your column is created.
how do u mutli line text
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDelete<&script language="javascript" type="text/javascript">
ReplyDeletevar context;
var web;
var fields;
//alert('here');
function CreateSiteColMultiline() {
context = new SP.ClientContext.get_current();
web = context.get_web();
fields = web.get_fields();
//alert('Atleast we are here');
var fieldSchema = '';
fields.addFieldAsXml(fieldSchema, false, SP.AddFieldOptions.addFieldCheckDisplayName);
context.executeQueryAsync(onAddFieldSuccess, onAddFieldFail);
}
var onAddFieldSuccess = function () {
alert('Field created');
}
var onAddFieldFail = function () {
alert('Something went wrong');
}
<&/script><&input class="ms-input" id="btnCreateColumn" onclick="CreateSiteColMultiline(); return false" type="Submit" value="Create"/> ??? ??
Äny text here ????
?
?
another script to add multiline Text
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteChoice Field
ReplyDelete<&Field ID="{8e2b16ee-86ce-49c5-b217-91a1eee4e664}"
Name="Project"
StaticName="Project"
DisplayName="Project"
Description=""
Group="My Columns"
Type="Choice"
SourceID="http://schemas.microsoft.com/sharepoint/v3/fields"
Format="[Dropdown/RadioButtons]"
FillInChoice="FALSE">
Project A
Project B
No
DateTime Field
<&Field ID="{b0cda4bf-3588-498b-8f5b-dcd5f860ecf0}"
Name="DueDate"
StaticName="DueDate"
DisplayName="Due Date"
Description=""
Group="My Columns"
Type="DateTime"
Format="[DateTime/DateOnly]"
SourceID="http://schemas.microsoft.com/sharepoint/v3/fields"/>
Note Field
<&Field ID="{c8da1af1-5a97-4ea9-8055-bfd9ea4e89e1}"
Name="Comments"
StaticName="Comments"
DisplayName="Comments"
Description=""
Group="My Columns"
Type="Note"
NumLines="6"
AppendOnly="TRUE"
RichText="TRUE"
RichTextMode="[Compatible/FullHtml]"
SourceID="http://schemas.microsoft.com/sharepoint/v3/fields"/>
This schema will help you in building any fields reqiured
ReplyDeleteThis comment has been removed by the author.
ReplyDeletevar fieldSchema = '<&Field Type="Note" \
ReplyDeleteName="LongText" \
DisplayName="Long Text" \
Required="TRUE" \
NumLines="6" \
AppendOnly="TRUE" \
RichText="TRUE" \
RichTextMode="[Compatible/FullHtml]" \
Group="Columns" />';
What if more than one site columns needs to be created?
ReplyDeleteHow to modify the above scripts?