Skip to content

Instantly share code, notes, and snippets.

@patrickbussmann
Forked from vgrem/ContentTypeExtensions.cs
Last active February 28, 2016 02:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patrickbussmann/21104e655d11dedc916d to your computer and use it in GitHub Desktop.
Save patrickbussmann/21104e655d11dedc916d to your computer and use it in GitHub Desktop.
Creates a content type based on the specified schema in SharePoint 2010 (CSOM)
/**
* Creates a content type based on the specified schema (CSOM)
*/
var ContentTypeExtensions = new function () {
/**
* Creates a content type based on the specified schema.
* @param {Object} Client Context
* @param {Object} A Collaborative Application Markup Language (CAML) string that contains the schema.
* @return {Object} A newly created client object of a content type.
*/
this.addContentTypeAsXml = function (clientContext, schemaXml) {
var xmlDoc = null;
if (window.DOMParser) {
var parser = new DOMParser();
xmlDoc = parser.parseFromString(schemaXml, 'text/xml');
}
else // Internet Explorer
{
xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = false;
xmlDoc.loadXML(schemaXml);
}
for(var x = 0; x < xmlDoc.childNodes.length; x++)
{
if (xmlDoc.childNodes[x].localName == 'ContentType')
{
this.load(clientContext, clientContext.get_web().get_contentTypes(), xmlDoc.childNodes[x]);
}
}
};
this.load = function (clientContext, cts, xrdr) {
var contentTypeCreation = new SP.ContentTypeCreationInformation();
if (typeof(xrdr.attributes['Name']) !== 'undefined') {
contentTypeCreation.set_name(xrdr.attributes['Name'].value);
}
if (typeof(xrdr.attributes['Group']) !== 'undefined') {
contentTypeCreation.set_group(xrdr.attributes['Group'].value);
}
if (typeof(xrdr.attributes['Description']) !== 'undefined') {
contentTypeCreation.set_description(xrdr.attributes['Description'].value);
}
if (typeof(xrdr.attributes['ID']) !== 'undefined') {
contentTypeCreation.set_parentContentType(this.getParentContentType(clientContext, xrdr.attributes['ID'].value));
}
var contentType = cts.add(contentTypeCreation);
// Additional properties
if (typeof(xrdr.attributes['Hidden']) !== 'undefined') {
contentType.set_hidden(xrdr.attributes['Hidden'].value == 'TRUE');
}
if (typeof(xrdr.attributes['ReadOnly']) !== 'undefined') {
contentType.set_readOnly(xrdr.attributes['ReadOnly'].value == 'TRUE');
}
var promises = [];
for (var x = 0; x < xrdr.childNodes.length; x++) {
if (xrdr.childNodes[x].localName == 'Fields') {
for (var y = 0; y < xrdr.childNodes[x].childNodes.length; y++) {
if (xrdr.childNodes[x].childNodes[y].localName == 'Field') {
promises.push(this.loadFieldLinks(clientContext, contentType, xrdr.childNodes[x].childNodes[y]));
}
}
}
}
return new Promise(function (resolve, reject) {
Promise.all(promises).then(function () {
resolve(contentType);
}, reject);
});
};
this.loadFieldLinks = function (clientContext, contentType, xrdr) {
var promises = [];
for (var x = 0; x < xrdr.childNodes.length; x++) {
if (xrdr.childNodes[x].localName == 'FieldRef') {
promises.push(new Promise(function (resolve, reject) {
this.loadFieldLink(clientContext, contentType.get_fieldLinks(), xrdr.childNodes[x]).then(function() {
contentType.update(false);
clientContext.executeQueryAsync(function () {
resolve(field);
}, function (sender, args) {
reject({ sender: sender, args: args });
});
}, reject);
}));
}
}
return Promise.all(promises);
};
this.loadFieldLink = function (clientContext, fieldLinks, xrdr) {
return new Promise(function (resolve, reject) {
this.getField(clientContext, xrdr).then(function (hostField) {
var linkCreationInfo = new SP.FieldLinkCreationInformation();
linkCreationInfo.set_field(hostField);
var fieldLink = fieldLinks.add(linkCreationInfo);
if (typeof (xrdr.attributes['Required']) !== 'undefined') {
fieldLink.set_required(xrdr.attributes['Required'].value == 'TRUE' || xrdr.attributes['Required'].value == '-1');
}
if (typeof (xrdr.attributes['Hidden']) !== 'undefined') {
fieldLink.set_hidden(xrdr.attributes['Hidden'].value == 'TRUE' || xrdr.attributes['Hidden'].value == '-1');
}
resolve(fieldLink);
}, reject);
});
};
this.getField = function (clientContext, xrdr) {
var field = clientContext.get_web().get_availableFields().getById(xrdr["ID"]);
return new Promise(function (resolve, reject) {
clientContext.load(field);
clientContext.executeQueryAsync(function () {
resolve(field);
}, function (sender, args) {
reject({ sender: sender, args: args });
});
});
};
this.getParentContentType = function (clientContext, id) {
return this.getContentTypeById(clientContext, new SP.ContentTypeId(id).get_typeId()); // todo: problem with parent id
};
this.getContentTypeById = function (clientContext, ctId) {
var ct = clientContext.get_web().get_availableContentTypes().getById(ctId);
clientContext.load(ct);
return new Promise(function (resolve, reject) {
clientContext.executeQueryAsync(function () {
resolve(ct);
}, function (sender, args) {
reject({ sender: sender, args: args });
});
});
};
this.getContentTypeByName = function (clientContext, name) {
var cts = clientContext.get_web().get_contentTypes();
clientContext.load(cts);
return new Promise(function (resolve, reject) {
clientContext.executeQueryAsync(function () {
for (var x = 0; x < cts.length; x++) {
if (cts[x].Name == name) {
resolve(cts[x]);
}
}
}, function (sender, args) {
reject({ sender: sender, args: args });
});
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment