Click here to Skip to main content
15,898,036 members
Articles / All Topics
Technical Blog

Create a list item in another site collection

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
28 Sep 2015CPOL1 min read 9.8K   1   2
It is much easier to create or update a list item in another site collection using JSOM. We can easily do it as follows: I recommend using this method instead of the REST API to create a list item in another site collection.

It is much easier to create or update a list item in another site collection using JSOM. We can easily do it as follows:

I recommend using this method instead of the REST API to create a list item in another site collection.

var listName;
var item;
var itemType;

var otherClientContext = new SP.ClientContext("other site url");
var oList = otherClientContext.get_web().get_lists().getByTitle(listName);
otherClientContext.load(oList);

var itemCreateInfo = new SP.ListItemCreationInformation();
var oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', "SharePoint");
oListItem.update();

own1ClientContext.load(oListItem);
own1ClientContext.executeQueryAsync(onQuerySucceeded, onQueryFailed);

function onQuerySucceeded() {
}

function onQueryFailed(sender, args) {
   alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

 

If we try to create a list item in another site collection using REST API call such as

$.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": $("#__REQUESTDIGEST").val()
	},
	success: function (data) {
	},
	error: function (jqXHR, textStatus, errorThrown) {
		alert('error');
	}
});

we get an error

The security validation for this page is invalid and might be corrupted. Please use your web browser’s Back button to try your operation again.
The problem is we are trying to use the form digest $(“#__REQUESTDIGEST”).val() which will give the form digest for the current site collection.

We need to get the other site collection form digest value and provide it to the X-RequestDigest in the headers. In our case, we get this value by making an empty POST request to the
http:///_api/contextinfo and then use the d.GetContextWebInformation.FormDigestValue as the form digest value making the call.

Also, we need to specify method: “POST” instead of type: “POST” for the REST call to create the item.

We can construct the data for the item to be created as:

var itemType = GetItemTypeForListName(listName);
var item = {
	"__metadata": { "type": itemType },
	"Title": "updated title"
};

So, updated code to create a list item in another site collection is:

// Get form digest value of the other site collection
$.ajax({
  url: otherSiteUrl + "/_api/contextinfo",
  type: "POST",
  headers: {
  "Accept": "application/json;odata=verbose"
  },
  success: function (contextData) {                       
    $.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": $("#__REQUESTDIGEST").val(contextData.d.GetContextWebInformation.FormDigestValue)
      },
      success: function (data) {
        alert('success');
      },
      error: function (jqXHR, textStatus, errorThrown) {
        alert('error');
      }
    });
  },
  error: function (jqXHR, textStatus, errorThrown) {
    alert('error');
  }
});

The post Create a list item in another site collection appeared first on SharePoint Guide.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionusing of other site url Pin
Member 1330646412-Jul-17 5:51
Member 1330646412-Jul-17 5:51 
QuestionCan we create an item across web application using post or any other client side technique ? Pin
Member 109783181-Feb-16 19:46
Member 109783181-Feb-16 19:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.