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

Add value to a lookup field using JavaScript
SharePoint deveopment training course

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.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →