Saturday, November 2, 2013

Create publishing pages in apps for SharePoint 2013

http://code.msdn.microsoft.com/office/SharePoint-2013-Create-9bfa6f5c

vs2012->file->new->project c# ->office/SharePoint->apps->apps for SharePoint 2013-> "SP_Publishing_js"
Click on 'Ok' button
Click on 'Validate' button
Click on 'Sign in'
Now click on 'Finish' button

SP_Publishing_js -> Pages -> open 'Default.aspx' 

Add below 'sp.publishing.js' javascript reference in 'PlaceHolderAdditionalPageHead' 
The SP.Publishing.js file. This file is provided by SharePoint Server, 
And has been added as a script link in the Default.aspx page, 
Because that enables the script in App.js to use SP.Publishing classes.
<script type="text/javascript" src="/_layouts/15/sp.publishing.js"></script>

And add below div in 'PlaceHolderMain' 
<asp:Content ContentPlaceHolderID="PlaceHolderMain" runat="server">
    <div>
        <p id="message">
            initializing...
        </p>
    </div>
    <div>
        <input id="CreatePage" type="button" value="Create New Publishing Page" />
        <a id="linkToPage"></a>
    </div>
</asp:Content>
SP_Publishing_js -> Scripts -> open 'App.js' 
And add below javascript 
'use strict';

var context;
var web;
var pubWeb;
var pageInfo;
var newPage;
var listItem;
var user;

context = SP.ClientContext.get_current();
user = context.get_web().get_currentUser();

$(document).ready(function () {
    web = context.get_web();
    $('#CreatePage').click(function () { createPage(); });
    getUserName();
});

function getUserName() {
    context.load(user);
    context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
}

function onGetUserNameSuccess() {
    $('#message').text('Hello ' + user.get_title());
}

function onGetUserNameFail(sender, args) {
    alert('Failed to get user name. Error:' + args.get_message());
}

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

        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(

                function () {
                    pageInfo = new SP.Publishing.PublishingPageInformation();
                    newPage = pubWeb.addPublishingPage(pageInfo);
                    context.load(newPage);
                    context.executeQueryAsync(
                        function () {
                            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 hosting Web: ' + args.get_message());
        });
}

function getQueryStringParameter(paramToRetrieve) {
    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] == paramToRetrieve)
            return singleParam[1];
    }
}
SP_Publishing_js -> AppManifest.xml -> select 'Permission' Tab
Set web, site collection to 'FullControl' permission
SP_Publishing_js -> Deploy
Now it is asking 'Do you trust SP_Publishing_js?'
Click on 'Trust It'
Click on 'Create New Publishing Page' button
Now a page has been created in 'Pages' Library (if 'Page' Library is not their we need to activate publishing feature)
Thank You

1 comment:

Featured Post

Mention a Channel or Team – Power Automate

Mention a Channel or Team – Power Automate graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages Channel: ---------- {   ...

Popular posts