Create a copy of document in SharePoint 2010 document library using JavaScript

Sreedhar K Source

I want to create a copy of document in the SharePoint document library.
Basically let us assume there is a template and every user will open the document by clicking on it. I want to create a copy of file user has clicked and open that file for editting.
I have tried using JavaScript Client Object model of SharePoint. But the examples are for manipulating list items but not for document library.
Can any one please point to any sources that I can use to manipulate the files in document library

One restriction being I need to use JavaScript object model or web services to achive this functionality. i.e., NO server side code

Following is the code I got till now

  1. The approach I am planning to use is copy the existing file object
  2. Rename it and
  3. Save it to other document library

Please ignore formatting as I am not able to do it properly and this is under development code

    <script type="text/javascript">
 var clientContext = null;
        var web = null;
        var meetingItems = null;
        var filePath = null;
        var file = null;
        ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");

        function Initialize() {
            clientContext = new SP.ClientContext.get_current();
            web = clientContext.get_web();

            this.list = web.get_lists().getByTitle("Documents");

            clientContext.load(list, 'Title', 'Id');
            var queryStart = "<View>"+ "<Query>"+ "<Where>"+ "<Eq>"+ "<FieldRef Name='Title'/>" + "<Value Type='Text'>"; 
                    var queryEnd = "</Value>"+ "</Eq>"+ "</Where>"+ "</Query>"+ "</View>";

camlQuery = new SP.CamlQuery(); 

queryMeeting = queryStart + 'DevCookbook'+ queryEnd;


meetingItems = list.getItems(camlQuery);

            clientContext.executeQueryAsync(Function.createDelegate(this, this.onListLoadSuccess), Function.createDelegate(this, this.onQueryFailed));

        function onListLoadSuccess(sender, args) {
        filePath = meetingItems.get_item(0).get_path();
        file = meetingItems.get_item(0);

            clientContext.executeQueryAsync(Function.createDelegate(this, this.onFileLoadSuccess), Function.createDelegate(this, this.onFileFailed));

           // alert("List title : " + this.list.get_title() + "; List ID : " + this.list.get_id());

           // doclist();        
function doclist()
var path = file.get_title();
path = meetingItems.get_item(0).get_file().get_title();
        function onQueryFailed(sender, args) {
            alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());

        function onFileLoadSuccess(sender, args) {
      alert("List title : " + this.list.get_title() + "; List ID : " + this.list.get_id());

        function onFileFailed(sender, args) {
            alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());



answered 7 years ago Sreedhar K #1

I used copy webservice to do the functionality.
Approach is combination of Object Model and JavaScript functions

  1. Copy the file from templates library.
  2. Check out file using "CheckoutDocument" function
  3. Add metadata in background
  4. Show edits metadata pop up to user using

    var oDialog = { url: "../Library/Forms/Edit.aspx?ID=" + itemID, title: "Create a new document" }; SP.UI.ModalDialog.showModalDialog(oDialog)

  5. After user input check in the document

comments powered by Disqus