Im trying to add a field to a content type through some code on Chris o' Briens blog here. I have modified the code slightly to add multiple fields to a content type. See below:
function SetupServiceCatalog()
{
LinkFieldToContentType("Contacts");
LinkFieldToContentType("Confidentiality");
}
function LinkFieldToContentType(fieldName) {
var clientContext = new SP.ClientContext.get_current();
var web = clientContext.get_web();
// Retrieve all content types
ContentTypeCollection = web.get_contentTypes();
// Retrieve the previously created field
field = web.get_fields().getByTitle(fieldName);
clientContext.load(ContentTypeCollection);
clientContext.load(field);
clientContext.executeQueryAsync(onQuerySuccess, onCreationFail);
}
function onQuerySuccess() {
var contentTypeEnumerator = ContentTypeCollection.getEnumerator();
var myCt;
// Find the previously created content type
while (contentTypeEnumerator.moveNext()) {
var ct = contentTypeEnumerator.get_current();
if (ct.get_name() === "OSD Wiki Template") {
myCt = ct;
break;
}
}
if (myCt != null) {
// Create a field link reference
var fieldLink = new SP.FieldLinkCreationInformation();
fieldLink.set_field(field);
// Add the field link reference to the content type
myCt.get_fieldLinks().add(fieldLink);
myCt.update(true);
var clientContext1 = new SP.ClientContext.get_current();
clientContext1.load(myCt);
clientContext1.executeQueryAsync(onCreationSuccess1, onCreationFail1);
}
}
function onCreationFail() {
}
function onCreationSuccess1() {
}
function onCreationFail1(sender, args) {
}
However the final issue i'm experiencing is the clientContext1.executeQueryAsync which keeps going to the failure callback with the error of "Unknown Error". I'm not sure if its because I have a executeQueryAsync within a onQuerySuccess or? But i have a feeling its the way I'm calling this method twice, the second time within the first calls OnQuerySuccess.
Can anyone suggest what may be wrong?