Learning SharePoint   Enhance Your SharePoint Knowledge

Subscribe to Learning SharePoint Subscribe
Twitter Learning SharePointFollow Us
in Learning SharePoint
Check Out Our New SharePoint 2013 series!

How to Get\Set Lookup Field using ECMAScript SharePoint 2010

06/15/2011  
Category: SharePoint 2010


In this post of LearningSharePoint.com we will explain how to get and set a lookup field using Ecmascript use -

Firstly, to get or retrieve a lookup field value see the below example -

function GetLookupValue()
{
var context = new SP.ClientContext.get_current();
var web = context.get_web();
var list = web.get_lists().getByTitle(‘Testlist’);

var query = SP.CamlQuery.createAllItemsQuery();
allItems = list.getItems(query);
context.load(allItems, ‘Include(Title,Department)’);

context.executeQueryAsync(Function.createDelegate(this, this.success), Function.createDelegate(this, this.failed));
}

function success() {

var TextFiled = “”;
var ListEnumerator = this.allItems.getEnumerator();

while(ListEnumerator.moveNext())
{
var currentItem = ListEnumerator.get_current();
TextFiled += currentItem.get_item(‘Title’) + ‘-’ +currentItem.get_item(‘Department’).get_lookupValue() + ‘\n’;
}
alert(TextFiled);
}

function failed(sender, args) {
alert(“failed. Message:” + args.get_message());
}

In the above ECMAScript Department is the Lookup and currentItem.get_item(‘Department’).get_lookupValue() will get you the value of the Lookup item selected.

Next Lets look at how to set a Lookup Field. >>

Subscribe to Learning SharePoint Subscribe
Twitter Learning SharePointFollow Us
in Learning SharePoint
Advertisement

Random Posts

SharePoint 2010 Developer FAQs
SharePoint 2010 Developer FAQs

This is a summary of our most common asked Forum Questions. Q. How do I deploy my custom CSS file»

Adding\Updating\Retriving\SPquery Lookup fields in Sharepoint
Adding\Updating\Retriving\SPquery Lookup fields in»

I have a Lookup field “Departments” for the following operations – Adding a Lookup Item – SPListItem listItem = myList.Items[0];»

Set People and Group Field with Current User’s Login Name using Ecmascript\Javascript using Client Object Model – SharePoint 2010
Set People and Group»

Here is a quick example of  how to set the People and Group (People Picker) Field with the Login Name»

Recent Forum Posts

Linq to Sharepoint

by RS on May 18, 2012

comments on the code

by spbsmile on May 18, 2012

connection(add) EditControlBlock and my program

by spbsmile on May 18, 2012

Custom master pages used in multiple SharePoint 2010 sites

by ubersteve on May 18, 2012

upload file and click on button but cant upload file into list by using below code

by santhoshreddy on May 18, 2012