Tuesday, August 8, 2017

How To CheckIn And CheckOut a SharePoint page using JavaScript(JSOM)

Hi Friends,

Recently I have come across a requirement where I need to update the properties of a Sharepoint page. In this post we are going to see how to checkin and checkout a page in sharepoint using JSOM.
Below are the code snippets.

CheckOut:
function checkOut(){
        var ctx = SP.ClientContext.get_current();
        var page = ctx.get_web().getFileByServerRelativeUrl(window.location.pathname);
        //you can use any path. Here I am using current page.
        page.checkOut();
        ctx.load(page);
        ctx.executeQueryAsync(Function.createDelegate(this, checkOut_Success),
                                            Function.createDelegate(this, checkOut_Fail));
}
function checkOut_Success(sender, args){
 alert("Checked Out");
//do your actions
}
function checkOut_Fail(sender, args){
   alert("Fail checkout");
}

CheckIn:
function checkIn(){
         var ctx = SP.ClientContext.get_current();
        var web = ctx.get_web();
        var page = web.getFileByServerRelativeUrl(window.location.pathname);   
        //you can use any path. Here I am using current page.  
        page.checkIn();
        page.publish();
        ctx.executeQueryAsync(Function.createDelegate(this, checkIn_Success),
                                            Function.createDelegate(this, checkIn_Fail));
}
function checkIn_Success(sender, args){       
    alert(" checked in "); 
}
function checkIn_Fail(sender, args){   
    alert("Fail  checked in "+args.message);
}

Hope this helps...

Monday, August 7, 2017

How to Copy/Move a SharePoint Page/File from one location to another using Javascript

Hi Friends,

In this blog we are going to see How to copy or move a sharepoint Page/File from one location to the other using Javascript.

By default SP.js in SharePoint provides the following functions through which we can perform this activity with the minimal coding effort.

SP.MoveCopyUtil.copyFile
SP.MoveCopyUtil.moveFile

Copy a Sharepoint File/Page:
In the below code snippet we are copying the home.aspx present in pages library to the Folder called "duplicate" under the same pages library.

In order to achieve it, we will be using the function SP.MoveCopyUtil.copyFile which expects the below parameters

1)Current context
2)Source url – Full url of the file/page.
3)Destination url – Full destination url of the file/Page.

Code snippet:
var current_context = SP.ClientContext.get_current();
SP.MoveCopyUtil.copyFile(current_context,"https://siteurl/pages/home.aspx","http://siteurl/Pages/duplicate/home.aspx");
current_context.executeQueryAsync(function(){ PageCopySuccess(); },function(){ PageCopyFailure(); });

function PageCopySuccess(){
alert('Success');
}

function PageCopyFailure(){
alert('Failed');
}


Move a Sharepoint File/Page:
In the below code snippet we are moving the home.aspx present in pages library to the sitepages page library.

In order to achieve it, we will be using the function SP.MoveCopyUtil.moveFile which expects the below parameters

1)Current context
2)Source url – Full url of the file/page.
3)Destination url – Full destination url of the file/Page.

Code snippet:
var current_context = SP.ClientContext.get_current();
SP.MoveCopyUtil.moveFile(current_context,"https://siteurl/pages/home.aspx","http://siteurl/sitepages/home.aspx");
current_context.executeQueryAsync(function(){ PageMoveSuccess(); },function(){ PageMoveFailure(); });

function PageMoveSuccess(){
alert('Success');
}

function PageMoveFailure(){
alert('Failed');
}

Hope this helps...


Popular Posts