List Aggregator App (SPFx)

Add value to a lookup field using JavaScript Object model in SharePoint 2013

Here we will discuss how we can add value to a lookup field using JavaScript client object model code in SharePoint 2013. 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 item to the list using JavaScript object model. So if your column allows only single value selection then you can add the lookup value like below:

Before that you can check out below useful articles:
Lotus notes to SharePoint 2013 Migration

SharePoint deveopment training course

Get logged in username using SharePoint 2013 client object model

Enable Sign in as Different User Option in SharePoint 2013

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>

If your lookup column allows multiple selection 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 webpart.

<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>

Hope this will be helpful to you.

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 →