Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

Can anyone use the Javascript CSOM SP.Publishing.PublishingWeb in non-app code?

$
0
0

I have been trying to write some javascript which can be used to create publishing pages in the current site (this is called in an application page opened via a custom action on the Pages library).

I am trying to use code similar to the example on the SP Typescript demos page.  The only real difference between their and my implementation which I can see is that they are running in an app (hence checking SPHostUrl is is non-existent for me as I am on premise and non-app).

My code is as follows:

SP.SOD.executeFunc('mQuery.js', 'm$', function () {
    m$.ready(function () {
        m$('#CreatePage').click(function () {
            SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function () {
                SP.SOD.executeOrDelayUntilScriptLoaded(function () {
                    SP.SOD.executeFunc('sp.publishing.js', 'SP.Publishing', function () {
                        createPage();
                    });
                }, 'SP.Runtime.js');
            });
        });
    });
});

function createPage() {
    var context = SP.ClientContext.get_current();
    var web = context.get_web();
    context.load(web);
    context.executeQueryAsync(function () {
        var pubWeb = SP.Publishing.PublishingWeb.getPublishingWeb(context, web);
        context.load(pubWeb);
        context.executeQueryAsync(function () {
            var pageInfo = new SP.Publishing.PublishingPageInformation();
            var newPage = pubWeb.addPublishingPage(pageInfo);
            context.load(newPage);
            context.executeQueryAsync(function () {
                var listItem = newPage.get_listItem();
                context.load(listItem);
                context.executeQueryAsync(function () {
                    var link = document.getElementById("linkToPage");
                    link.setAttribute("href", web.get_url() + "/Pages/" + listItem.get_fieldValues().FileLeafRef);
                    link.innerText = "Go to new page!";
                }, function (sender, args) {
                    alert('Failed to get new page: ' + args.get_message());
                });
            }, function (sender, args) {
                alert('Failed to Add Page: ' + args.get_message());
            });
        }, function (sender, args) {
            alert('Failed to get the PublishingWeb: ' + args.get_message());
        });
    }, function (sender, args) {
        alert('Failed to get the Web: ' + args.get_message());
    });
}


However I cannot for the life of me figure out how to get the PublishingWeb object.  Everytime my code hits the "context.load(pubWeb)" I get a javascript error thrown in sp.runtime.js:

"Object doesn't support property or method 'get_$y_0'"

I've tried a number of alternatives such as hard coding the url for the context, but nothing seems to work.  Is this just me?  Or just non-app code?


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>