Alec Tang

Professional Web Developer/ Web Designer

I build websites based on the latest web standards providing the best possible solution to your company

  • Home
  • About
  • Portfolio
  • Contact
  • Blog
Subscribe Feed

Reading and Writing to Lookup Field in Sharepoint

Posted by Alec on Mon, 18 Jan 2010, in ASP.NET C#   Sharepoint   

Lookup field is a field which links a child class to a parent class, it is an association kind, which allows a list item to reference another item from another list. However, reading and writing to a lookup field is different from a normal field. We need to use the class SPFieldLookupValue to archieve this.

First, it is important to understand that, whenever we create a Lookup field on a list, the value it stores for its reference item is not what we thought it was, ID. It is actually ID;#VALUE, where ID is the foreign key of the parent list item and VALUE is the title or whatever column we set of the parent list item.

So, to read a Lookup field in C#:

if (listItem["Material"] != null){
     SPFieldLookupValue spv = new SPFieldLookupValue(listItem["LOOKUP_NAME"].ToString());
     ddl.SelectedValue = spv.LookupValue.ToString();
}

What we have done here is simply get an instance of an SPFieldLookupValue by passing it the lookup field name and then aget its lookupvalue property. That will return the title of the parent list item.

To write to a Lookup field in C#:

SPList spList = web.Lists["ListName"];
int intSelectedId = Common.getItemId(ddl.SelectedValue, spList);
SPFieldLookupValue spv;
if (intSelectedId > 0)
{
    spv = new SPFieldLookupValue(intSelectedId, ddlM.SelectedValue);
    document["LOOKUP_NAME"] = spv;
}

Notice that we needed to get the parent item's ID by passing it the title value. I created my own lookup method for this:

public static int getItemId(string strValue, SPList spList){
  SPQuery query = new SPQuery();
  query.Query = "<Where><Eq><FieldRef Name='Title' />
                  <Value Type='TEXT'>" + strValue + "</Value></Eq></Where>";
  if (spList.GetItems(query) != null){
       SPListItem result = spList.GetItems(query)[0];
       return result.ID;
   }
  else
       return 0;
}
Comments Be the first to write a comment. Comment gets approved before publishing.

Post Your Comment

 
 
   

Search

 

Latest Posts

  • Website finally back up from Google's block
  • Sys.WebForms.PageRequestManagerParserErrorException
  • How Google treats Content Duplication
  • How to create HTML column in Sharepoint List View
  • How Sharepoint stores User Data
  • How to send email via Sharepoint
  • Malaysia Airlines launched iPhone Application: MHMobile
  • Intranet, the next big market
  • How to retrieve and update from a multi choice Checkboxlist ...
  • Auto Complete returns giant list of undefined

Categories

  • Browsers (1)
  • Projects (1)
  • Web Design (7)
  • Sharepoint (20)
  • Telerik (5)
  • Wordpress (1)
  • Internet (2)
  • SQL (5)
  • LINQ (3)
  • ASP.NET C# (33)
  • JavaScripts (3)
  • IIS (0)
  • Industry (1)
  • Tools (8)
  • SEO (5)

Archives

  • August 2010 (1)
  • May 2010 (1)
  • April 2010 (3)
  • March 2010 (5)
  • February 2010 (4)
  • January 2010 (11)
  • November 2009 (3)
  • October 2009 (1)
  • September 2009 (9)
  • August 2009 (3)
  • July 2009 (4)
  • June 2009 (1)
  • May 2009 (2)
  • April 2009 (8)
  • March 2009 (6)
  • February 2009 (2)
© Copyright 2009 Alec Tang. All Rights Reserved.
This site is conform to W3C Standard XHTML & CSS