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.
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.
- <script src="/<siteurl>/jquery-1.12.4.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(document).ready(function() {
- var otherSiteUrl = "<sitecollectionurl>";
- var listName = "TestList";
- var itemType = GetItemTypeForListName(listName);
- var item = {
- "__metadata": {
- "type": itemType
- },
- "Title": "updated title"
- };
- // Get form digest value of the other site collection
- $.ajax({
- url: otherSiteUrl + "/_api/contextinfo",
- 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
- headers: {
- "Accept": "application/json;odata=verbose"
- },
- success: function(contextData) {
- alert(contextData.d.GetContextWebInformation.FormDigestValue);
- $.ajax({
- url: otherSiteUrl + "/_api/web/lists/getbytitle('" + listName + "')/items?@target='" + otherSiteUrl + "'",
- method: "POST",
- contentType: "application/json;odata=verbose",
- data: JSON.stringify(item),
- async: false,
- headers: {
- "Accept": "application/json;odata=verbose",
- "X-RequestDigest": contextData.d.GetContextWebInformation.FormDigestValue
- },
- success: function(data) {
- alert('success');
- },
- error: function(jqXHR, textStatus, errorThrown) {
- alert('error');
- }
- });
- },
- error: function(jqXHR, textStatus, errorThrown) {
- alert('error');
- }
- });
- });
- // Get List Item Type metadata
- function GetItemTypeForListName(name) {
- return "SP.Data." + name.charAt(0).toUpperCase() + name.split(" ").join("").slice(1) + "ListItem";
- }
- </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.
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.
- <script src="/<siteurl>/jquery-1.12.4.min.js" type="text/javascript"></script>
- <script src="/<siteurl>/jquery.SPServices-2014.02.min.js" type="text/javascript"></script>
- <script type="text/javascript">
- $(document).ready(function() {});
- function PreSaveAction() {
- $().SPServices({
- operation: "CopyIntoItemsLocal",
- async: false,
- SourceUrl: "<sourcepdfurl>",
- DestinationUrls: ["<destinationurl>"],
- completefunc: function(xData, Status) {
- alert("Status=" + Status + " XML=" + xData.responseXML.xml);
- }
- });
- return true;
- }
- </script>
Note
While copying file from one source to another, we can change the name of the file but not the extension.
While copying file from one source to another, we can change the name of the file but not the extension.

OzzbornPosted Jul 9, 2020, 1:18 AM
In addition to 'Title', how can I update other fields? I need to copy all items from one list to another and match columns between those lists. Thanks.
Al RoomePosted Nov 30, 2017, 12:19 PM
This is Great , how do you actually get the title of the item to update to a different site collection instead of it being called updated title?