I am trying to create a small manual backup function for my libraries in SharePoint and tried to use the following code that copy data from one Library to another(or an url) but without success. edit: this is not planned as an automatic backup. Someone will be weekly tasked to click the button basically. Full automation may follow but this is not my goal here.

Would it be possible to adapt the code to copy to a defined location on the user computer?

Code from:

how to copy files from one document library to another in sharepoint using jsom

Copy of my code:

<script src="/sites/myDomain/mySite/SiteAssets/nSPTiles.js" type="text/javascript"></script>
<script src="/sites/myDomain/mySite/SiteAssets/jquery-3.1.1.min.js" type="text/javascript"></script>
<script src="/sites/myDomain/mySite/SiteAssets/jquery-ui-1.12.1.custom/jquery-ui.min.js" type="text/javascript"></script>

<input id="clickMe" type="button" value="clickme" onclick="copyFiles('SiteAssets','C:\\Users\\myUserName\\Desktop');" />

<script type="text/javascript"> 

function copyFiles(sourceLib, destLib){

console.log("1_" + sourceLib);
console.log("2_" + destLib);

var context = new SP.ClientContext.get_current();
var web = context.get_web();
var folderSrc = web.getFolderByServerRelativeUrl(sourceLib);

console.log("3_" + folderSrc);

    function() {
        console.log("4_" + "Got the source folder right here!");
        var files = folderSrc.get_files();
        var e = files.getEnumerator();
        var dest = []; 
        while (e.moveNext()) {
            var file = e.get_current();
            var destLibUrl = destLib + "/" + file.get_name();

            console.log("5_" + destLibUrl);

            dest.push(destLibUrl); //delete this when we're happy we got the file paths right
        console.log("6_" + dest); //delete this when we're happy we got the file paths right
        context.executeQueryAsync(function() { console.log("Files moved successfully!");}, function(sender, args) {console.log("error: ") + args.get_message()});
    function(sender, args){console.log("Sorry, something messed up: " + args.get_message());}


Thanks for reading.

PS: I know that there shall be no need to backup items since they are on the server but it did happen that the sub site got deleted and restored but with empty libraries (SiteAssets in particular)

2 Answers 2


seems to be more a question of consistent SharePoint Server and backup strategy. If you got the effect that backup and restore does not work the usual way the first thing is to monitor your installation. To my experience this effect takes place if SharePoint Foundation is used or your server installation is crap. Furthermore it's a question on how do your backup strategy looks like and whether the possibilities of backup and restore always fail. What happens if you create another site-collection with the database a.s.o? So this could point at a fundamental crack in the sql server installation or configuration.

To my opinion to use javascript as backup procedure isn't safer. You always need a client for. And what about if your permission structures change? You will have to grant that the code is running after any change etc.

  • Well, I have no control or influence on the back end of the huge company I work for. Hence, we do with what we have available at our disposal. And for the backup, it will be weekly done by someone. I don't plan to have it automatically triggered. If I can, why not, but first no.
    – MüdeFuchs
    May 28, 2018 at 9:55
  • iff you are working for a huge company there must be a process defined by the IT for this case. So the only way is to open a ticket and ask for a recovery test. Everything else is creating islands and develop processes that exists in parallel. And the next one who is trapped in the same case will do another workaround. Sorry to veto but your argument conc. huge company is the biggest one against your workaround!
    – Celophysis
    May 28, 2018 at 14:14
  • Yes, the same process that took us 2,5 month to get the SharePoint back till they finally find the right people to do it. So before I get a real fix I can certainly wait one year.
    – MüdeFuchs
    May 28, 2018 at 14:22
  • I see. So if there is no other way than backup manually what about a powershell solution? This could run on a scheduled task on server copying library content to a share. No client is needed and it's faster.
    – Celophysis
    May 29, 2018 at 7:11
  • Thanks for the idea but I am not competent in PowerShell and I have only access to the client side. I just want to make it easier than going to each library, open then in explorer, copy into a folder on my laptop.
    – MüdeFuchs
    May 29, 2018 at 14:57

So I understood the issue that scripts in browsers can not simply push files to a computer since it would be a major security issue.

I find a workaround via an excel macro.

I hosted a xlsm on my SharePoint that opens on the click of a button on one of my SP page. Once the excel is opened and the user clic 'enable content' the macro fires and download the files froms the hard coded library to a temp folder on the users computer (mapping the libraries as a network drive, then using webDav link to the libraries).

Thanks for those who commented!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.