Wednesday, 23 August 2017

Create SharePoint Page and redirect to the page with edit mode


I recently found one of our clients struggle in creating a page in pages library. The process follows like this:

  1. Go to Pages library
  2. New Document
  3. Enter page name, description, select layout
  4. Click create. ->creates the page and redirects to the default view of the library.
  5. Search for the page you have just created, open the page
  6. Edit the page using the ribbon.
So if you could see the struggle to create a page and reach to the edit page. So client asked me to simplify this.
I found a stack-overflow post very useful and this blog post is inspired by that stack-overflow post.

Solution:
Put this code in a js file and refer this file in your master page.
$(document).ready(function () {
 var currentAddress = window.location.pathname;
    if (currentAddress.toLowerCase().indexOf('/createpage.aspx') == -1) // not the creation page page
        return;
    $("input[id$='buttonCreatePage']").each(function () {
        // The OK button
        var $OKButton = $(this);
        var previousClickHandler = this.onclick;
        this.onclick = null;
        // New click handler
        $OKButton.click(function () {
            var currentAction = $("form").attr('action');
            var newPageUrl = _spPageContextInfo.webServerRelativeUrl;
            if (!newPageUrl.endsWith('/'))
                newPageUrl = newPageUrl + "/";
            newPageUrl = newPageUrl + $("[id$='parentUrlLabel']").text();
            newPageUrl = newPageUrl + $("input[id$='urlNameTextBox']").val() + ".aspx?ControlMode=Edit";
            var newAction = UpdateQueryString("Source", newPageUrl, currentAction);
            $("form").attr('action', newAction);
        });
        $OKButton.click(previousClickHandler);
    });
}
function UpdateQueryString(key, value, url) {
    if (!url) url = window.location.href;
    var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"),
        hash;
    if (re.test(url)) {
        if (typeof value !== 'undefined' && value !== null)
            return url.replace(re, '$1' + key + "=" + value + '$2$3');
        else {
            hash = url.split('#');
            url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
            if (typeof hash[1] !== 'undefined' && hash[1] !== null)
                url += '#' + hash[1];
            return url;
        }
    }
    else {
        if (typeof value !== 'undefined' && value !== null) {
            var separator = url.indexOf('?') !== -1 ? '&' : '?';
            hash = url.split('#');
            url = hash[0] + separator + key + '=' + value;
            if (typeof hash[1] !== 'undefined' && hash[1] !== null)
                url += '#' + hash[1];
            return url;
        }
        else
            return url;
    }
}


Remember, this works only for the publishing Pages Library of your site.
Also check my post, SharePoint page layout with default html in RichHtmlField
Hope it helped you too. Thank you :)