Showing posts with label Copy. Show all posts
Showing posts with label Copy. Show all posts

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