SharePoint 2013 Javascript Insert Item with lookup field
16,752
Correct way is:
var lkfieldsomthing = new SP.FieldLookupValue();
lkfieldsomthing.set_lookupId(ID_VALUE); //WHERE ID_VALUE is the unique ID
then while setting you can use
listItem.set_item("LOOKUP_COLUMN_NAME", lkfieldsomthing )
Author by
owenrumney
Updated on June 14, 2022Comments
-
owenrumney almost 2 years
I'm trying to just Javascript to add a list item when in SharePoint 2013 using the following code;
function createListItem() { var clientContext = new SP.ClientContext.get_current(); var oList = clientContext.get_web().get_lists().getByTitle('CrewNoticesAudit'); var itemCreateInfo = new SP.ListItemCreationInformation(); this.oListItem = oList.addItem(itemCreateInfo); oListItem.set_item('Title', 'My New Item!'); oListItem.set_item('NoticeId', 1); // THIS IS A LOOKUP VALUE oListItem.set_item('User', 'administrator'); oListItem.set_item('TimeStamp', new Date()); oListItem.set_item('AuditType', 'Open'); oListItem.update(); clientContext.load(oListItem); clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed)); } function onQuerySucceeded() { alert('Item created: ' + oListItem.get_id()); } function onQueryFailed(sender, args) { alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace()); }
The problem that I'm having is that NoticeId is a lookup value looking at a different list. Do I need to do something different to set a lookup value on an item?
UPDATE: sorry, cracked it. Second google more fruitful.
If anyone else is struggling, you need to create a lookup field.
var noticeIdLookup = new SP.FieldLookupValue(); noticeId.set_lookupId(noticeId);
-
Gennady G almost 7 yearsThank You very much! p.s.: here is exactly "ID", because UniqueId is different thing..