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.