1

The code below works only to get SharePoint 2013 Foundation list content type and content type url, but I cant get the parent content type.

It returns an object but when I try to access properties ct.get_parent().get_name() I get error:

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.

var listContentTypes;
    var contentTypeName = "";
    var ctTemplateUrl;

    function runCSOMCall() {

        var targetWeb;
        var list;
        var ctx = new SP.ClientContext();
       // var ctObject = new SP.ContentType();
        targetWeb = ctx.get_web();
        list = targetWeb.get_lists(); 
        targetList = list.getByTitle("Project Documents"); //get the list details
        ctx.load(targetWeb);
        ctx.load(targetList);
        listContentTypes = targetList.get_contentTypes();
        ctx.load(listContentTypes);
        ctx.executeQueryAsync(function(){

        var ct_enumerator = listContentTypes.getEnumerator();

        while (ct_enumerator.moveNext()) {
            var ct = ct_enumerator.get_current();
            ctTemplateUrl = ct.get_documentTemplateUrl();
                 //get content type name
                contentTypeName += 'Content types in this list: ' +  ct.get_name() + ' *** Parent: ' + ct.get_parent() + ' ***Template URL' + ctTemplateUrl + '\n';

         var parent=ct.get_parent();                
            ctx.load(parent,'Name', 'Id');//, 'Include(name)');
         }

        console.log(contentTypeName);

        // some other info

         var message = "List info :";
         message += "\n list title: " + targetList.get_title();
              //message += "\n list content types: " + targetList.get_contentTypes();
        $get('outputHtml').innerHTML = message;


        }, function() {
            $get('outputHtml').innerHTML = 'Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace();
        });
    }
    window['runCSOMCall'] = runCSOMCall;

})();



_spBodyOnLoadFunctionNames.push("runCSOMCall");     
2

You need to execute a query again to fetch that, so modify

var parent=ct.get_parent();                
ctx.load(parent,'Name', 'Id');//, 'Include(name)');

to something like

var parent=ct.get_parent();                
ctx.load(parent,'Name', 'Id');

ctx.executeQuery(function(){
     // Here you will have `ct.get_parent().get_name();
}, 
function() {
     $get('outputHtml').innerHTML = 'Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace();
});
| improve this answer | |

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.