Here in this blog, i am taking the general scenario of "updating/creating" a list item from one site collection to another site collection.
The Statement:
"I have a master list in one site collection and copy of the same list in another site collection. I would like to make sure that whenever there is an update to an item on the master list, the same item in the copy list in the other site collection gets updated ."
Implementation :
Steps in brief :
- We will use the rest API in the SPD workflow for the cross site collection call.
- On the source master list, we will write two workflows (2013 template) one on item added and the other one on item updated.
- The item added workflow will create the copy of list item in the destination list using the rest API.
- The item updated workflow will update that copy in the destination list, whenever the master list entry will update.
- For allowing the master list workflow to create/update entry in other site collection, we need to provide the workflow a app permission on the target site.
- List Schema :
Master List : List Name "Employee" , Columns : {"EmpName", "CTC"}
Destination List :
List Name "Employee Backup", Columns {"EmpName", "CTC","MasterListItemID"}
MasterListItemID : will hold the item id of the master list item.
Let's create first workflow (on Item Added) :
3 dictionary variables will require for the post request :
- header : which will contain the accept/Content-type keys with the same value "application/json;odata=verbose"
Header Dictionary contains accept & content-type keys
2. metadata : the metadata dictionary will contain only one key "type" and the value will be SP.Data.[title of target list]ListItem
3. parameters : this dictionary will contain the key __metadata & the columns values.
the "MasterListItemId" column will hold a current item ID.
Parameter dictionary..(the __metadata will be of type dictionary)
Will associate all these dictionary variables with the "call a web-service" action. The method will be a "Post" as we are creating a item.
Request URL will be : https://targetsitecollection/_api/web/lists/getbytitle('Target_List_Name')/Items
Save this workflow and update the settings to trigger it on item added only.
Now, to allow this workflow to create item in the target site collection, we need to provide a permission to this workflow.
Go to site settings of the source site(holding master list) -> site app permissions
Copy the App identifier (ID between last | and @) for next steps (n my example this was 8f20f240-ddde-45dc-a08a-66834769220d)
Now, manually add this app identifier on the target site collection (site holding the target list). To do this follow the below steps :
a. Open the appinv.aspx page :
http://{the Site Collection}/{target-site}/_layouts/15/appinv.aspx.
b. Paste your App identifier of the source site, lookup the rest of the information and use the following XML to the App’s Permission Request XML.
And now the first part has been done, now lets publish the workflow and create a new item in the master list and check the workflow status, if it is completed than the item will be successfully created in the list.
Note : Make sure that the user who is creating the item in master list, also have a permission on the target site collection/site. Or else, we can write the actions under "APP STEP" in the workflow.
-----------------------------------------------------------------------
Now, lets write a second workflow, which will trigger when the master list item has been updated.
In the item updated workflow will have two stages :
- Fetch the item ID of the "copy of the master list item/target list item". Because for updating the target list item using rest api, we will require it item id.
- Updating the target list item.
As we have stored the master list item ID in the target list item field (MasterListItemID). So, whenever the master list item will update, we will take the current item ID and will query the target list for fetching the item whose "MasterListItemID" is equal to the current item Id.
So, we will write a first request as :
https://target_site_collection/_api/web/lists/getbytitle('target_list')/Items?$filter=MasterListItemID eq [%Current Item:ID%]
which will in turn return the Item ID of the target list item Id and then we will use that ID to update the list item.
As for now, we are expecting only one result in response. So, for now i am directly using "d/results(0)/ID" for getting the item ID. Later on we can check the response Item count and do some validation.
header dictionary will just hold two keys (Accept/Content-Type) and the request will a "GET" request.
Now, as the first stage is completed, will write a second stage for updating the item.
This stage will also contain 3 dictionaries object (header,metadata,parameters).
metadata & parameters dictionary will be same as the first workflow. So, create the dictionary same as mentioned in the "Item Added Workflow"
header dictionary will contain two additional keys as we are updating the existing item.
X-HTTP-Method and If-Match are the two additional keys.
In the "call a web service" action we will point to the target list item and will use the item id that we have stored in the previous stage.
https://target_site_collection/_api/web/lists/getbytitle('target_list')/items([%Variable:SecondaryListItemID%])
And now, just save and publish the workflow and try updating the item.
Note : Only once we need to provide a permission. And we have done it while writing the first workflow.
References :
http://blog.portiva.nl/2016/11/03/sharepoint-designer-call-http-web-service-to-create-item-in-other-site-collection/