2

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?

1 Answer 1

3

Came on this old post with no good answer so decided to add my solution:

var currSite = "/";
var ctId = "0x01002F31FB62E029B146B5D9CF65E832561B";//you should get this yourself, see below
var fldInternalName = "Confidentiality";

var ctx = new SP.ClientContext(currSite);
var web = ctx.get_web();
var fld = web.get_fields().getByInternalNameOrTitle(fldInternalName);

var contentTypeCollection = web.get_contentTypes(); 
var ct = contentTypeCollection.getById(ctId);

var fieldRef = new SP.FieldLinkCreationInformation();
fieldRef.set_field(fld);

ct.get_fieldLinks().add(fieldRef);
ct.update(true);
ctx.executeQueryAsync(onCreationSuccess1, onCreationFail1);

My call to get the contenttypeid (ctId). It is based on REST as this is our preferred approach.

var ctName = "Contacts";    
$.ajax({
        url: currSite+"_api/web/AvailableContentTypes?$select=Name,Id,StringId&$filter=Name eq '" + ctName + "'",
        method: 'GET',
        headers: {
            'accept': 'application/json;odata=verbose'
        }
    }); 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.