Hi
All,
Earlier
We have seen How to create Site Columns using Declarative XML(http://sharepointkitchen.blogspot.in/2012/12/creating-sharepoint-site.html),
In current post we will see the same using ECMA Script.
In a following example we are going to do
1. Creation of Site Column
2. Adding the Site Column to Content Type.
'use strict';
var SPAppWebUrl;
var spHostUrl;
var contentTypeCollection;
var siteColumnColl;
var web;
var field;
(function
() {
// This code runs when the DOM is ready and creates
a context object which is
// needed to use the SharePoint object model
$(document).ready(function
() {
spHostUrl =
decodeURIComponent(getQueryStringParameter('SPHostUrl'));
SPAppWebUrl =
decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
$('#btnCreate').on('click',
function () {
CreateSiteColumn();
});
});
function
CreateSiteColumn() {
var Context = new
SP.ClientContext.get_current();
if (Context !==
undefined && Context !== null) {
var parentContext = new
SP.AppContextSite(Context, spHostUrl);
web =
parentContext.get_web();
//get field collection
siteColumnColl =
web.get_fields();
//XML Field
var xmlField = '<Field ID ="{fc571c6c-51ee-48df-953d-2464628d741b}"
Type ="Text" Name ="Answer" DisplayName ="Answer"
Group ="SiteColumnGrp" Required ="FALSE" AllowDeletion
="TRUE" Overwrite ="TRUE" OverwriteInChildScopes="TRUE"></Field>';
siteColumnColl.addFieldAsXml(xmlField, false,
SP.AddFieldOptions.AddToNoContentType);
Context.load(siteColumnColl);
Context.executeQueryAsync(onCreationSuccess, onCreationFail);
}
function
onCreationSuccess() {
addToContentType();
}
function
addToContentType() {
//get all Content Types
contentTypeCollection = web.get_contentTypes();
//get the Field
field =
web.get_fields().getByTitle('Answer');
Context.load(contentTypeCollection);
Context.load(field);
Context.executeQueryAsync(onAddingToCTSuccess, onCreationFail);
}
//Used
to Add, SiteColumn to ContentType.
function
onAddingToCTSuccess() {
var isExisted = false;
var name = 'TestCType';
var exContentType;
var contentEnumerator =
contentTypeCollection.getEnumerator();
while
(contentEnumerator.moveNext()) {
var currentCT =
contentEnumerator.get_current();
//checking whether Contentype is exists or not.
if (currentCT.get_name()
== name) {
isExisted
= true;
exContentType = currentCT;
break;
}
}
if (isExisted) {
if (isExisted !== null) {
//Creating FieldLink
var fieldAdd = new
SP.FieldLinkCreationInformation();
fieldAdd.set_field(field);
//Add FieldLink to ContentType
exContentType.get_fieldLinks().add(fieldAdd);
exContentType.update(true);
Context.load(exContentType);
Context.executeQueryAsync(onAddedCTToSuccess, onCreationFail);
}
}
}
function
onAddedCTToSuccess() {
alert('Site Column Created and added to Content Type');
}
function
onCreationFail(sender, args) {
alert('Request failed. ' +
args.get_message() + '\n' + args.get_stackTrace());
}
}
function
getQueryStringParameter(urlParameterKey) {
var params =
document.URL.split('?')[1].split('&');
var strParams = '';
for (var i = 0; i <
params.length; i = i + 1) {
var singleParam =
params[i].split('=');
if (singleParam[0] ==
urlParameterKey)
return
decodeURIComponent(singleParam[1]);
}
}
Thanks.
0 comments:
Post a Comment