0

I have a SharePoint 2013 on premise environment and I am currently using some JavaScript to query a pages library, however once I have queried the pages library I need to do an if statement based on the content type name. However I am having trouble pulling back the Content Type name, this is what I currently have:

    var web;
var list;
var context;
var mylist;
var collListItem;
var folder;


SP.SOD.executeFunc('sp.js', 'SP.ClientContext', getNews);

function getNews() {
    context = SP.ClientContext.get_current();
    web = context.get_web();
    list = web.get_lists();
    mylist = web.get_lists().getByTitle('Pages');
    var ncamlQuery = new SP.CamlQuery();
    ncamlQuery.set_viewXml('<View Scope=\'Recursive\'><RowLimit>5</RowLimit><ViewFields><FieldRef Name=\'Comments\' /><FieldRef Name=\'ContentType\' /><FieldRef Name=\'FileRef\' /><FieldRef Name=\'Title\' /><FieldRef Name=\'PublishingRollupImage\' /></ViewFields><Query><Where><And><IsNull><FieldRef Name=\'CheckoutUser\' /></IsNull><Or><Eq><FieldRef Name=\'ContentType\' /><Value Type=\'Text\'>ContentType2</Value></Eq><Eq><FieldRef Name=\'ContentType\' /><Value Type=\'Text\'>ContentType1</Value></Eq></Or></And></Where><OrderBy><FieldRef Name=\'Created\' Ascending=\'False\' /></OrderBy></Query></View>');
    ncamlQuery.set_folderServerRelativeUrl('News');
    collListItem = mylist.getItems(ncamlQuery);
    context.load(mylist);
    context.load(collListItem);
    context.executeQueryAsync(
     Function.createDelegate(this, SuccessHandler),
     Function.createDelegate(this, ErrorHandler));
}

function SuccessHandler(result) {
    var newshtml = '';
    listItemEnumerator = collListItem.getEnumerator();

    while (listItemEnumerator.moveNext()) {
        oListItem = listItemEnumerator.get_current();

        var CT = oListItem.get_contentType();
        console.log("content type = " + CT);

    }

}

function ErrorHandler(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Currently the code is returning :

[object object]

In the console however I have tried using the following:

CT.get_name();

CT.get_id();

Both currently return

The property or field 'Name' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested

even though I have explicitly added Content Type as a ViewField in the CAML Query.

Any advice on this would be appreciated.

1

The content type object is not loaded. So you need another round trip to the server to get the details as below

function SuccessHandler(result) {
    var newshtml = '';
    listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        oListItem = listItemEnumerator.get_current();
        var CT = oListItem.get_contentType();
        context.load(CT);
        context.executeQueryAsync(
            (function (contentobject) {
                return function () {
                    var value = contentobject.get_name();
                    alert("VAL: "+value);
                };
            }(contentobject)),
            function () {
                alert("No Success");
            }
        );
    }
}
  • 2
    Thank you very much Amal, this worked with a few tweaks, I changed contectobject.get_name() to be CT.get_name() and this worked great. Thank you! – Adam13Hylo Jul 22 '16 at 13:18
0

For me, Amal Hashim 's solution does not work unless I write "CT" everywhere he wrote "contentobject". then it works.

function SuccessHandler(result) {
    var newshtml = '';
    listItemEnumerator = collListItem.getEnumerator();
    while (listItemEnumerator.moveNext()) {
        oListItem = listItemEnumerator.get_current();
        var CT = oListItem.get_contentType();
        context.load(CT);
        context.executeQueryAsync(
            (function (CT) {
                return function () {
                    var value = CT.get_name();
                    alert("VAL: "+value);
                };
            }(CT)),
            function () {
                alert("No Success");
            }
        );
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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