尝试使用JSOM Sharepoint 2013创建发布页面并更改页面布局

时间:2015-03-19 20:16:20

标签: sharepoint

您好我正在尝试使用JSOM Sharepoint 2013创建发布页面并更改页面布局。我的代码运行没有错误,但不会更改页面布局或任何其他页面字段,例如'页面标题'

这是我的代码

'use strict';

var context = new SP.ClientContext('/sites/poc');
//var user = context.get_web().get_currentUser();

// Variables used in various callbacks 

var web;
var pubWeb;
var pageInfo;
var newPage;
var listItem;
//var hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
//var appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));

// This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
$(document).ready(function () {

    web = context.get_web();
    var user = context.get_web().get_currentUser();
    $('#CreatePage').click(function () { createPage(); });
});

function createPage() {
    context.load(web);
    context.executeQueryAsync(

        // Success callback after loading the App Web. 
        // We first want to get the host web, and cast it as a PublishingWeb. 
        function () {
            var hostUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
            var hostcontext = new SP.AppContextSite(context, hostUrl);
            web = hostcontext.get_web();

            pubWeb = SP.Publishing.PublishingWeb.getPublishingWeb(context, web);
            context.load(web);
            context.load(pubWeb);
            context.executeQueryAsync(

                // Success callback after getting the host Web as a PublishingWeb. 
                // We now want to add a new Publishing Page. 
                function () {
                    pageInfo = new SP.Publishing.PublishingPageInformation();
                    newPage = pubWeb.addPublishingPage(pageInfo);
                    context.load(newPage);
                    context.executeQueryAsync(
                        function () {

                            // Success callback after adding a new Publishing Page. 
                            // We want to get the actual list item that is represented by the Publishing Page. 
                            listItem = newPage.get_listItem();
                            context.load(listItem);

                            context.executeQueryAsync(



                                                                function () {
                                                                                          var x = web.get_url() + "/Pages/" + listItem.get_fieldValues().FileLeafRef;
                                          alert(x);
                                    var relativeUrl = "/sites/poc/Pages/" + listItem.get_fieldValues().FileLeafRef;
                                    var pageFile = web.getFileByServerRelativeUrl(relativeUrl);
                                    context.load(pageFile);
                                    var pageItem = pageFile.get_listItemAllFields();
                                    context.load(pageItem);

                                    context.executeQueryAsync(function () {

                                        //change page layout
                                        var itemValue = pageItem.get_item('PublishingPageLayout');
                                        itemValue.set_url(web.get_url() + '/_catalogs/masterpage/' + 'Yaroslav1.aspx');
                                        itemValue.set_description('Some description');

                                        //change title and url of newly created publishing page
                                        pageItem.set_item('PublishingPageLayout', itemValue);
                                        pageItem.set_item('Title', 'NewPage');
                                        pageItem.set_item('FileLeafRef', 'NewPage' + '.aspx');

                                        pageItem.update();
                                        alert('success');
                                    }, function (sender, args) {
                                        alert('Failed to get the PublishingWeba: ' + args.get_message());
                                    });
                                },


                                // Failure callback after getting the actual list item that is  
                                // represented by the Publishing Page. 
                                function (sender, args) {
                                    alert('Failed to get new page: ' + args.get_message());
                                }
                                );
                        },
                        // Failure callback after trying to add a new Publishing Page. 
                        function (sender, args) {
                            alert('Failed to Add Page: ' + args.get_message());
                        }
                        );
                },
                // Failure callback after trying to get the host Web as a PublishingWeb. 
                function (sender, args) {
                    alert('Failed to get the PublishingWeb: ' + args.get_message());
                }
                );
        },
        // Failure callback after trying to load the App Web. 
        function (sender, args) {
            alert('Failed to get the hosting Web: ' + args.get_message());
        });
}




function getQueryStringParameter(urlParameterKey) {

    var params = document.URL.split('?')[1].split('&');

    var strParams = '';

    for (var i = 0; i < params.length; i = i + 1) {

        var singleParam = params[i].split('=');

        if (singleParam[0] == urlParameterKey)

            return decodeURIComponent(singleParam[1]);

    }

}

0 个答案:

没有答案