lundi 29 juin 2015

Copy version history of Sharepoint 2010 documents using Javascript

So I am have an html file which I am linking a content editor on a document library in SharePoint. So far it works to do what I was intending for it to do which is copy a file from one sharepoint doc library to another one somewhere else on the site. (The JSfiddle won't work because the links are obviously broken to my sharepoint but I put it in there to organize it a bit better.) Please note I am pretty noob at web languages but I work for the government so I have been waiting weeks for a Visual Studios license for my work computer so I can write this in C# but... Its the government.. It'll be a few more weeks before that's an option.

When its in Sharepoint and the links are correct, it loads the files from source doc library and creates checkbox HTML tags with the file name, the user then selects the files they want by checking the appropriate boxes, then it copies each selected file over to the target doc library. Simple stuff.

HOWEVER, I am running into an issue when I was told just this morning I need to move SP version history and any other file metadata along with the file. From the research I've done on the subject I have sort of figured out the moveTo() method can move metadata but it won't leave a copy of the file in the source doc library which won't work for my purposes. And the copyTo() method simply starts the new copy of the file at version 1.0 or 0.1.

So my question is.. Is there a simple way to move metadata of a file with out significantly altering my code? I have noticed there is a property in SP.File called versions.. I haven't figured out how to use it but I was hoping some one would know how to move that property along with the file.

Thanks in advance for any help you can provide and here's my code.

http://ift.tt/1C1Rirs

Here is the ENTIRE html file I'm linking to in the content editor incase you wanna test it out. You just need to edit the trgURL and srcURL to doc library relative URLs on your SharePoint site.

<!DOCTYPE html>
<html>
<Head>
    <title>Copy Function</title>
    <!--Programmer:James Harrington. Intern, SGT inc. 2015-->
    <script src="http://ift.tt/1rfghyb">
</script>
<style>
h1 { color: #223355; font-family: 'Open Sans', sans-serif; font-size: 24px;}
p { color: #223355; font-family: 'Open Sans', sans-serif; font-size: 12px;}
    .b
    {
        padding-left:10px;
        padding-right:10px;
        color: white;
        border-radius: 4px;
        text-shadow: 0 1px 1px rgba(0,0,0,0.2);
        background-color: #223355;
    }

    #FormContainer
    {
    background-color:#eeeeee;

    -webkit-box-shadow:0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.2) inset;
       -moz-box-shadow:0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.2) inset;
            box-shadow:0 1px 4px rgba(0, 0, 0, 0.3), 0 0 40px rgba(0, 0, 0, 0.2) inset;
            margin:10px;
            padding-left:20px;
            padding-top:20px;
            padding-bottom:20px;
            width: 400px;
    }

    #FileContainer
    {
        padding-bottom:20px;
    }

    #brand
    {
        width:70px;
        height:70px;
    }

</style>
</Head>
<body>

<!--Button to load checkbox list-->
<div id="FormContainer">
    <!--EDIT IMAGE URL IN LINE BELOW!!!-->
    <img id="brand" style="display:block; position: absolute; margin-left:280px;" src="meatball.png" />
    <h1 id="fromto">Copy Files: </h1>
    <input id="FileGetter" class="b" type="button" onclick="getFilesInFolder()" value="Load Files"></input><br>

    <!--Contains checkboxes--->
    <div id="FileContainer">

    </div>

    <!--Performs file fetch-->
    <input id="FileLoader"  class="b" type="button" onclick="ProcessFiles()" value="Copy"></input><br>
</div>


<script type="text/javascript">

//EDIT TARGET/SOURCE URLS BELOW
    var trgURL = "PUT YOUR TARGET DOC LIBRARY RELATIVE URL HERE";
    var srcURL = "PUT YOUR SOURCE DOC LIBRARY RELATIVE URL HERE";
//EDIT TARGET/SOURCE URLS ABOVE

    //On Document Load
    $(document).ready(function()
    {
        //Hide the load button until files are listed
        $('#FileLoader').hide();
        setToFromTitle(trgURL, srcURL);
    });

    //set From/To Title
    function setToFromTitle(trg, src)
    {
        var trgFolderName = trgURL.substr(0, trgURL.length-1);
        trgFolderName = trgFolderName.split("/").pop();
        var srcFolderName = srcURL.substr(0, srcURL.length-1);
        srcFolderName = srcFolderName.split("/").pop();
        $('#fromto').html("<p style='font-size:100%'>Copy Files: </p><p>" + srcFolderName + "  >>>  " + trgFolderName + "</p>");
    }

    var files;//list of files
    //Attempt to find files in input URL.
    function getFilesInFolder()
    {
        var context = SP.ClientContext.get_current();
        var web = context.get_web();
        var folder = web.getFolderByServerRelativeUrl(srcURL);
        files = folder.get_files();
        context.load(files);
        context.executeQueryAsync(Function.createDelegate(this, this.OnSuccess), Function.createDelegate(this, this.OnFailure));
    }
    //If file list is loaded correctly
    function OnSuccess()
    {
        var listItemEnumerator = files.getEnumerator();
            //Loops through every file URL in folder
            while (listItemEnumerator.moveNext())
            {   
                //gets URL of current URL
                var fileUrl = listItemEnumerator.get_current().get_serverRelativeUrl();
                //splices file name off of end of URL
                var fileName = fileUrl.split("/").pop();
                //adds a check box with proper info
                var htmlToAdd = "<input class='docbox' class='check' type='checkbox' name='"+fileUrl+"'>"+fileName+"</input><br>"
                $('.check').css("margin-bottom","5px");
                $('#FileContainer').append(htmlToAdd);
            }
        //show file checkboxes and copy button
        $('#FileContainer').hide();
        $('#FileContainer').slideDown("slow");
        $('#FileGetter').hide();
        $('#FileLoader').show();
    }
    //If file list is loaded incorrectly
    function OnFailure(sender, args) {
        alert("Failed. Message:" + args.get_message());
    }

    //file fetch on selected check boxes
    function ProcessFiles()
    {
        $('.docbox').each(function()
        {
            var check = $(this);
            var url = this.name
            var file = url.split("/").pop();
            if(check.is(':checked'))
            {
                copy(file, url);
            }
        });

        $('#FileGetter').show();
        $('#FileContainer').empty()
        $('#FileLoader').hide();
    }

//uses SP.File.copyTo() funtion to move individual file from source doc lib to target doc lib
    function copy(filename, exacturl)
    {
        try
        {
            var context = SP.ClientContext.get_current();
            var web = context.get_web();
            var file = web.getFileByServerRelativeUrl(exacturl);//target file to look at
            var target = trgURL+filename;//build target location url
            file.copyTo(target, true);//copy set 2nd parameter: true=overwrite - false=do not overwrite
            context.executeQueryAsync(Function.createDelegate(this, this.successHandler), Function.createDelegate(this, this.errorHandler));
        }
        catch(err)
        {
            alert(err.message);
        }
    }

    function successHandler() {
        alert("success!");
        location.reload(true);
    };

    function errorHandler() {
        alert("The file didn't copy. Please Try again or contact sharepoint admin if the problem persists.");
    };


</script>

</body>
</html>

Aucun commentaire:

Enregistrer un commentaire