Update List Item Across Site Collection Using Rest Services And Copy Files From One Library To Another Using SP Services

In this blog, I am going to explain how to update items in different site collections and copy files from one library to another in different site collections using SP Services.

For the below operations, user must have "contribute rights" on the other site collections and this code is tested via Windows User in SharePoint 2013.

While trying to update List item in different site collections, we must get request digest for that site collection as shown below.
  1. <script src="/<siteurl>/jquery-1.12.4.min.js" type="text/javascript"></script>  
  2. <script type="text/javascript">  
  3.     $(document).ready(function() {  
  4.         var otherSiteUrl = "<sitecollectionurl>";  
  5.         var listName = "TestList";  
  6.         var itemType = GetItemTypeForListName(listName);  
  7.         var item = {  
  8.             "__metadata": {  
  9.                 "type": itemType  
  10.             },  
  11.             "Title""updated title"  
  12.         };  
  13.         // Get form digest value of the other site collection  
  14.         $.ajax({  
  15.             url: otherSiteUrl + "/_api/contextinfo",  
  16.             type: "POST"//While trying to get Context info for user POST is used as type instead of method because body is empty while //request is sent   
  17.             headers: {  
  18.                 "Accept""application/json;odata=verbose"  
  19.             },  
  20.             success: function(contextData) {  
  21.                 alert(contextData.d.GetContextWebInformation.FormDigestValue);  
  22.                 $.ajax({  
  23.                     url: otherSiteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?@target='" + otherSiteUrl + "'",  
  24.                     method: "POST",  
  25.                     contentType: "application/json;odata=verbose",  
  26.                     data: JSON.stringify(item),  
  27.                     async: false,  
  28.                     headers: {  
  29.                         "Accept""application/json;odata=verbose",  
  30.                         "X-RequestDigest": contextData.d.GetContextWebInformation.FormDigestValue  
  31.                     },  
  32.                     success: function(data) {  
  33.                         alert('success');  
  34.                     },  
  35.                     error: function(jqXHR, textStatus, errorThrown) {  
  36.                         alert('error');  
  37.                     }  
  38.                 });  
  39.             },  
  40.             error: function(jqXHR, textStatus, errorThrown) {  
  41.                 alert('error');  
  42.             }  
  43.         });  
  44.     });  
  45.     // Get List Item Type metadata  
  46.     function GetItemTypeForListName(name) {  
  47.         return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";  
  48.     }  
  49. </script>  
The second task is to copy files from one library to another library in different site collections.

I have used SP Service for copying files rather than Rest Service because I was getting error "The length of the URL for this request exceeds the configured maxUrlLength value" when I used Rest service. So, I decided to use SP service. Given below is the code for that.
  1. <script src="/<siteurl>/jquery-1.12.4.min.js" type="text/javascript"></script>  
  2. <script src="/<siteurl>/jquery.SPServices-2014.02.min.js" type="text/javascript"></script>  
  3. <script type="text/javascript">  
  4.     $(document).ready(function() {});  
  5.   
  6.     function PreSaveAction() {  
  7.         $().SPServices({  
  8.             operation: "CopyIntoItemsLocal",  
  9.             async: false,  
  10.             SourceUrl: "<sourcepdfurl>",  
  11.             DestinationUrls: ["<destinationurl>"],  
  12.             completefunc: function(xData, Status) {  
  13.                 alert("Status=" + Status + " XML=" + xData.responseXML.xml);  
  14.             }  
  15.         });  
  16.         return true;  
  17.     }  
  18. </script>   
Note

While copying file from one source to another, we can change the name of the file but not the extension.