0

What is the best (subjective.. I know. Sorry!) method for prompting a user to copy an instance of a document in my library to a library in his own site (on the same site collection, if that matters)? I am an administrator with a few word documents that I create too frequently to set specific content types for. So I have created a library that contains a few word templates for them to copy over (most importantly: metadata included except for the modified/created fields).

I have tried a few javascript/jquery methods that I'd put on the display form with a text box allowing them to enter their library url and how many copies they'd like to make, but that doesn't seem to work as I'd like. What would be the most efficient way of accomplishing this? Using an event handler? If so, is there any way to associate one of these with a custom button on the ribbon (I've only associated these buttons for js functions)?

Example of the javascript function I was trying to use:

function copyItem() {
    var itemurl = $("#copyFrom").val();
    var dst = $("#copyTo").val();

    $().SPServices({
        operation: "GetItem",
        Url: itemurl,
        async: false,
        completefunc: function (xData, Status) {
            itemstream = $(xData.responseXML).find("Stream").text();
            itemfields = "";
            $(xData.responseXML).find("FieldInformation").each(function(){
                itemfields+=$(this).get(0).xml;
            });;

        }
    });

    $().SPServices({
        operation: "CopyIntoItems",
        SourceUrl: itemurl,
        async: false,
        DestinationUrls: [dst],
        Stream: itemstream,
        Fields:itemfields,
        completefunc: function (xData, Status) {
            var error = $(xData.responseXML).find("CopyResult").first().attr("ErrorCode");
        }
    }
}

called by:

  <label>from:</label><input type="text" value="" id="copyFrom" maxlength="255">
    <label>to:</label><input type="text" value="" id="copyTo" maxlength="255">
    <input type="button" onclick="copyItem();" value="Copy">

note: I am not entering any values into these text boxes right now as I'm manually entering them into itemurl and dst. But the console says:

The value of the property 'copyItem' is null or undefined, not a Function object.

user3299197
  • 115
  • 6

1 Answers1

1

It's not recommended to use "async:false". It's better to do an asynchronous call and insert your second SPServices into the first one.

Also a close parenthesis is missing for your second SPServices.

And finally, the "Fields" must be an array (http://msdn.microsoft.com/en-us/library/copy.copy.copyintoitems.aspx).

I tried the below code and it worked for me:

var srcurl="http://my.sharepoint.com/aymeric_lab/Shared%20Documents/879258.jpeg";
var desturl="http://my.sharepoint.com/aymeric_lab/Shared%20Documents/879258_copy.jpeg";
$().SPServices({
  operation: "GetItem",
  Url: srcurl,
  completefunc: function (xData, Status) {
    var itemstream = $(xData.responseXML).find("Stream").text();
    var itemfields = [];
    $(xData.responseXML).find("FieldInformation").each(function(){
      itemfields.push($(this).get(0).xml);
    });

    $().SPServices({
      operation: "CopyIntoItems",
      SourceUrl: srcurl,
      DestinationUrls: [ desturl ],
      Stream: itemstream,
      Fields:itemfields,
      completefunc: function (xData, Status) {

      }
    })
  }
});
AymKdn
  • 3,327
  • 23
  • 27
  • Hi- Thank you for the clarification and finding the missing parenthesis. After replacing this with the function, the console still says that "copyItem" is null or undefined regardless of the type of file I specify. I have edited my original question to include the button I am using to call it if that will help. – user3299197 Feb 25 '14 at 15:44
  • You didn't read correctly my answer. The main issue is with the Stream: it must be an array. Look at my answer and code again :-) – AymKdn Feb 25 '14 at 21:20
  • Sorry, not "Stream" but "Fields" ! – AymKdn Feb 26 '14 at 08:25
  • I understand that, but I'm still not seeing what the issue is since I'm saying I used your example explicitly and received the same issue. I don't understand how it's working for you, and I've stared at the CopyIntoItems operation method (the original c# methods) to see if I can find what step I'm missing, but I don't see it.. Is there something I should be declaring differently outside of your example (aside from the document URLs)? – user3299197 Feb 27 '14 at 16:14
  • Did you try to look at the request sent to the server and the reply using Firefox + Firebug ? You may learn something there :-) – AymKdn Feb 28 '14 at 08:54
  • 1
    After doing so, I realized that the issue is with "xData.responseXML" - the stream and fields are still blank values because they're not getting any information from responseXML. When trying to write console.log(xData.responseXML);, I get an error saying "unable to get property 'xml' of undefined or null reference so I'm not sure what's going on here. The status is successful and responseText seems to show everything properly – user3299197 Mar 04 '14 at 20:38