0

I'm creating new publishing Pages via JSOM and now i need to get the listitem to change some fieldvalues but i struggle to get the ID of the publishing page that i just created in Pages library.

    var publishingWeb = new SP.Publishing.PublishingWeb.getPublishingWeb(ctx, web);
    var pageInfo = new SP.Publishing.PublishingPageInformation();
    pageInfo.set_name("New Publishing Page.aspx");
    pageInfo.set_pageLayoutListItem(pageLayoutItem);
    var newPage = publishingWeb.addPublishingPage(pageInfo);
    ctx.load(newPage);
    ctx.executeQueryAsync(SecondQuerySuccess, SecondQueryFailure);
    var itemID = newPage.get_listItem().get_Id();

But this throws an Uncaught Error: The property or field 'Id' has not been initialized.

So i tried to load the item itself.

    var item = newPage.get_listItem();
    ctx.load(item);
    ctx.executeQueryAsync(ThirdQuerySuccess, ThirdQueryFailure);
    var itemID = item.get_id();

But now i get a Uncaught TypeError: Cannot read property 'toString' of null in executeQueryAsync. The Object item is not null I checked that.

Am I missing something? Or am I on the wrong track?

0

I found the solution myself. I was confussed by how to use async functions. My goal was to create a publishing page for wich I need to use a predefined content type and page layout. Then i need to get the listitem for that page and populate some of the fields. Finally update the listitem get the coresponding file check it in and publish it. Here is the Code I am using.

function initiatePageCreation(){
    var scriptbase = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/";
    $.getScript(scriptbase + "SP.Runtime.js", function(){
        $.getScript(scriptbase + "SP.js", function(){
            $.getScript(scriptbase + "SP.Publishing.js", createPublishingPage);
        });
    });
}
var ctx, web, pageLayoutItem, newPage, pageItem, pageName, file;
function createPublishingPage(){
    ctx = new SP.ClientContext.get_current();
    web = ctx.get_web();
    var masterPageGallery = web.get_lists().getByTitle("Master Page Gallery");

    pageLayoutItem = masterPageGallery.getItemById(355);

    ctx.load(web);
    ctx.load(pageLayoutItem);
    ctx.executeQueryAsync(on_LoadLayout_success, on_LoadLayout_failure);
}

function on_LoadLayout_success(){
    var publishingWeb = new SP.Publishing.PublishingWeb.getPublishingWeb(ctx, web);
    var pageInfo = new SP.Publishing.PublishingPageInformation();
    pageName = "New Publishing Page.aspx";
    pageInfo.set_name(pageName);
    pageInfo.set_pageLayoutListItem(pageLayoutItem);
    newPage = publishingWeb.addPublishingPage(pageInfo);
    ctx.load(newPage);
    ctx.executeQueryAsync(on_PageAdded_success, on_PageAdded_failure);
}

function on_PageAdded_success(){
    pageItem = newPage.get_listItem();
    ctx.load(pageItem);
    ctx.executeQueryAsync(on_ItemLoaded_success, on_ItemLoaded_failure);
}

function on_ItemLoaded_success(){
    pageItem.set_item('Title', 'Test123');
    //set some more Fieldvalues
    pageItem.update();
    ctx.load(pageItem);
    ctx.executeQueryAsync(on_AttributesAdded_success, on_AttributesAdded_failure);
}

function on_AttributesAdded_success(){
    file = pageItem.get_file();
    file.checkIn();
    file.publish();
    ctx.load(file);
    ctx.executeQueryAsync(on_FileCheckedIn_success, on_FileCheckedIn_failure);
}

function on_FileCheckedIn_success(){
    console.log("done");
}

function on_FileCheckedIn_failure(sender,args){
    console.log('Request failed' + args.get_message());
}

function on_AttributesAdded_failure(sender, args){
    console.log('Request failed' + args.get_message());
}

function on_ItemLoaded_failure(sender, args){
    console.log('Request failed' + args.get_message());
}
function on_LoadLayout_failure(sender, args){
    console.log('Request failed' + args.get_message());
}

function on_PageAdded_failure(sender, args){
    console.log('Request failed' + args.get_message());
}

I hope someone may find this usefull.

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.