Add value to a lookup field using JavaScript Object Model in SharePoint 2013/2016/Online

This SharePoint tutorial explains, how to add value to a lookup field using JavaScript client object model code in SharePoint 2013/2016/Online.

Here I have a SharePoint list that has a lookup column. That lookup column is pulling data from a SharePoint list. And I want to add an item to the list using the JavaScript object model (jsom).

So if your list lookup column allows only single value selection then you can add the lookup value like below:

Add value to a lookup field using JSOM in SharePoint (single value selection)

var countryvalue = new SP.FieldLookupValue();
countryvalue.set_lookupId(1);
listItem.set_item('MyCountry', countryvalue);

Here 1 is the id of the source list and MyCounty is the column name of my parent list which is the lookup column name.

Below is the full code which you can put inside a Script editor web part.

<input type='button' id='1234' value='Click Me' onclick="additemstolist();"/>

<script language="javascript" type="text/javascript">

function additemstolist()
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('MyTest');
var listItemInfo = new SP.ListItemCreationInformation();
var listItem = list.addItem(listItemInfo);
listItem.set_item('Title', 'My Test Title');
var countryvalue = new SP.FieldLookupValue();
countryvalue.set_lookupId(1);
listItem.set_item('MyCountry', countryvalue);
listItem.update();
context.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailure));
}

function onSuccess() {
alert(“Item Added Successfully.");
}

function onFailure() {
alert(“Error Occured!!!");
}
</script>

Add value to a lookup field using JSOM in SharePoint (multiple selections)

If your list lookup column allows multiple selections then you can add multiple values like below:

var lookupsIds = [1,2,3];
var lookups = [];
for (var ii in lookupsIds) {
var lookupValue = new SP.FieldLookupValue();
lookupValue.set_lookupId(lookupsIds[ii]);
lookups.push(lookupValue);
}
listItem.set_item('MyCountry', lookups);

Here in this line var lookupsIds = [1,2,3]; 1,2,3 are the id of the source list. Here suppose you want to select 2 values you can like var lookupsIds = [2,3];

Below is the full code which you can put inside a script editor web part in the web part page in SharePoint.

<input type='button' id='1234' value='Click Me' onclick="additemstolist();"/>

<script language="javascript" type="text/javascript">

function additemstolist()
{

var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle('MyTest');

var listItemInfo = new SP.ListItemCreationInformation();
var listItem = list.addItem(listItemInfo);

listItem.set_item('Title', 'My Test Title');

var lookupsIds = [1,2,3];
var lookups = [];
for (var ii in lookupsIds) {
var lookupValue = new SP.FieldLookupValue();
lookupValue.set_lookupId(lookupsIds[ii]);
lookups.push(lookupValue);
}
listItem.set_item('MyCountry', lookups);
listItem.update();
context.executeQueryAsync(Function.createDelegate(this, this.onSuccess), Function.createDelegate(this, this.onFailure));
}

function onSuccess() {
alert(“Item Added Successfully.");
}

function onFailure() {
alert(“Error Occured!!!");
}
</script>

You may like following SharePoint lookup column tutorials:

Through this SharePoint tutorial, we learned how to add value to a lookup field using JavaScript Object Model in SharePoint 2013/2016/Online.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>