none
Get value from a lookup column. RRS feed

  • Question

  • Hello,

    I use this code to get value from a custom column :

    	string strDashListRoot = "http://serveur/HRD/Lists/";
    int teamLeaderId = 0;

    SPSite oSiteCollection = new SPSite(strDashListRoot);
    SPWeb oWebsiteRoot = oSiteCollection.OpenWeb();
    oWebsiteRoot.AllowUnsafeUpdates = true;
    SPList oList = oWebsiteRoot.Lists["teams"];
    SPListItemCollection listItemCOll;

    SPQuery query = new SPQuery();

    query.Query="<Where><Eq><FieldRef Name='employeeid'/><Value Type='Text'>" + userID + "</Value></Eq></Where>";

    listItemCOll = oList.GetItems(query);
    if (listItemCOll.Count > 0)
    {
    for(int icount=0; icount< listItemCOll.Count;icount++)
    {
    //Take the reference of that item
    SPListItem listItem = listItemCOll[icount];
    teamLeaderId = Convert.ToInt32(listItem["teamleader"]);
    }
    }

    return teamLeaderId;

    But how to get a value from a lookup column ?

    Thanks in advance !! :-)
    Tuesday, January 26, 2010 9:58 AM

Answers

  • SPFieldLookupValue lookupValue = new SPFieldLookupValue(listItem["columnName"].ToString());
    int id = lookupValue.LookupId;
    string value = lookupValue.LookupValue;

    you should check that listItem["columnName"] is not null before using it
    • Marked as answer by Nekrofage Tuesday, January 26, 2010 2:06 PM
    Tuesday, January 26, 2010 10:16 AM

All replies

  • SPFieldLookupValue lookupValue = new SPFieldLookupValue(listItem["columnName"].ToString());
    int id = lookupValue.LookupId;
    string value = lookupValue.LookupValue;

    you should check that listItem["columnName"] is not null before using it
    • Marked as answer by Nekrofage Tuesday, January 26, 2010 2:06 PM
    Tuesday, January 26, 2010 10:16 AM
  • Thanks Jevgeni !! :-)

    Your code runs perfectly !! :-)
    Tuesday, January 26, 2010 2:06 PM