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>

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?