My answer might be late but I was ableam advised to getprovide detailed answer, sorry about it working usingas it was my first post on SharePoint Stack Exchange. The following is my blog link where I added step by step approach defined in how to perform CRUD operations on a List that resides on another on premises farm. Recently working on an assignment I had faced this issue and could not find any end to end solution hence I documented it at on my blog at following url:
http://zenyscorp.blogspot.ca/2017/01/end-to-end-scenario-for-using.html. Its applicable if you have both
In nutshell, following are the farmssteps required to get a list item added, updated or deleted from one farm to another:
Its applicable if you have both the farms in same premises.
If Using CSOM:
- Get the context for the target site.
- Use CSOM to perform CRUD on the target.
function PerformListOperations() {
var clientContext = new SP.ClientContext( "http://sharepointtesting.xxxx.com/teams/SharePoint" );
var oList = clientContext.get_web().get_lists().getByTitle('OAuthTest');
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem( itemCreateInfo );
oListItem.set_item('Title',$('#Title').val());
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(onPerformListOperationsSuccess, onPerformListOperationsFail);
}
function onPerformListOperationsSuccess () {
alert("Successfully created an item using CSOM...");
}
function onPerformListOperationsFail (sender, args) {
alert('Failed to create an item using CSOM... Error:' + args.get_message());
}
$(document).ready(function () {
$("#CreateButton").click(function () {
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', PerformListOperations);
});
});
If Using REST API:
Call ContextInfo REST API call and retrieve FormDigestValue value.
Pass the FormDigestValue to subsequent calls to perform CRUD.
Making sure that metadata type for ListItem is correct, in my case my List Name is "OAuthTestList", the SharePoint adds "ListItem" as suffix to the list name, please refer following code line:
var jsonMetaData = { __metadata: { type: 'SP.Data.OAuthTestListListItem' }, Title: newTitle };
How to get Form Digest: I am calling contextinfo REST api call as shown in same environmentsfollowing function and retrieving value FormDigestValue from GetContextWebInformation object of response. Please pay attention how I am using "Target SharePoint Farm" URLs in hostUrl and how I am using it to construct the appURL value.
function CreateAListItem(listName, newTitle) {
var formDigest;
var hostUrl = "http://sharepointtesting.xxxx.com/xxxx/SharePoint/oAuth";
var appUrl = hostUrl + "/_api/web/lists/getbytitle('" + _options.listName + "')/items";
$.support.cors = true;
$.ajax(
{
url: hostUrl + "/_api/contextinfo",
type: 'POST',
xhrFields: { withCredentials: true },
dataType: "json",
contentType: "application/x-www-url-encoded",
headers: {
"accept": "application/json;odata=verbose"
},
success: function (data) {
if (data.d) {
formDigest = data.d.GetContextWebInformation.FormDigestValue;
// add your logic to add an item in the list example:
AddListItem(formDigest, appUrl, newTitle);
}
},
error: function (err) {
alert(xhr.status + ': ' + xhr.statusText);
}
});
}
Once the FormDigestValue is retrieved then we can save that in a variable and pass it in subsequent REST API Calls. A possible implemention of AddListItem could be as following:
function AddListItem(formDigestValue,appUrl, newTitle)
{
var jsonMetaData = { __metadata: { type: 'SP.Data.OAuthTestListListItem' }, Title: newTitle };
$.support.cors = true;
$.ajax({
url: appUrl,
type: "POST",
contentType: "application/json;odata=verbose",
headers: { "accept": "application/json;odata=verbose", "x-requestdigest": formDigestValue, "If-Match": "*" },
data: JSON.stringify(jsonMetaData),
xhrFields: { withCredentials: true },
success: function (data) {
if (data.d) {
//do something here;
}
},
error: function (xhr) {
alert(xhr.status + ': ' + xhr.statusText);
}
});
}