Lookup Helper Methods
Sometimes when writing custom code for SharePoint, you want to open the list to which another list field is referring (when it uses a lookup field). I had to do this to put the lookup field values in a custom menu item. Other times, you just want the SP List Item that another list item is referring to. You can do this using the SPFieldLookup and SPFieldLookupValue classes. Below are two helper functions that I wrote to do those two tasks. They may be useful to others working with lookup lists in SharePoint.
SPList GetLookupList (SPList, string)
This helper function takes the referring list and the lookup field name as parameters and returns the SPList object. If it can't find the field name or the lookup list it will throw an error with the text indicating what went wrong.
SPListItem GetLookupListItem (SPListItem, string)
This helper function takes the referring list item and the lookup field name as parameters and returns the SPListItem object the item is referring to. If it can't find the name, the lookup list or the list item, it will throw an error with the text indicating what went wrong.
Code Listing
The following is the code listing for these two methods. They're static methods in a public class. Just add this to a new file in your SharePoint project.
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
namespace AdventuresInConsulting
{
public class LookupHelper
{
/// <summary>
/// Get the list ID used by a lookup field for values
/// </summary>
/// <param name="referringListName">The SPList containing the lookup field</param>
/// <param name="lookupFieldName">The name of the lookup field</param>
/// <returns>The list used by the lookup field</returns>
static public SPList GetLookupList(SPList referringList, string lookupFieldName)
{
SPList lookupList = null;
string errMsg = string.Empty;
try {
errMsg = "ERROR: Could not find field named " + lookupFieldName + " in list " + referringList.Title;
SPFieldLookup editionsField = referringList.Fields[lookupFieldName] as SPFieldLookup;
errMsg = "ERROR: Expected " + lookupFieldName + " to be a lookup field in list " + referringList.Title;
Guid lookupWebGuid = editionsField.LookupWebId;
string lookupListId = editionsField.LookupList;
// The using statement prevents a leak of SPWeb (thanks for the catch Bob!)
using (SPWeb lookupWeb = SPContext.Current.Site.OpenWeb(lookupWebGuid))
lookupList = lookupWeb.Lists[new Guid(lookupListId)];
} catch (Exception e) {
throw new Exception(errMsg, e);
}
return lookupList;
}
/// <summary>
/// Get the SPListItem the lookup field is pointing to
/// </summary>
/// <param name="referringListItem">The SPListItem containing the lookup field</param>
/// <param name="lookupFieldName">The name of the lookup field in referringListItem</param>
/// <returns>The SPListItem where the value for the lookup field is stored</returns>
static public SPListItem GetLookupListItem(SPListItem referringListItem, string lookupFieldName)
{
SPListItem lookupListItem = null;
string errMsg = string.Empty;
try {
errMsg = "ERROR: Could not find field named " + lookupFieldName + " that is of type lookup";
SPFieldLookup lookupField = referringListItem.ParentList.Fields[lookupFieldName] as SPFieldLookup;
if (lookupField == null) throw new Exception(errMsg);
SPFieldLookupValue lookupValue = lookupField.GetFieldValue(referringListItem[lookupFieldName].ToString()) as SPFieldLookupValue;
if (lookupValue == null) throw new Exception(errMsg);
errMsg = "ERROR: Could not open lookup list";
SPList lookupList = LookupHelper.GetLookupList(referringListItem.ParentList, lookupFieldName);
errMsg = "ERROR: Could not find lookup item in list";
lookupListItem = lookupList.GetItemById(lookupValue.LookupId);
} catch (Exception e) {
throw new Exception(errMsg, e);
}
return lookupListItem;
}
}
}
Comments
Anonymous
June 16, 2008
PingBack from http://blog.a-foton.ru/2008/06/16/lookup-helper-methods/Anonymous
February 06, 2009
won't this leak memory call SPContext.Current.Site.OpenWeb(lookupWebGuid).Anonymous
February 07, 2009
Good catch Bob. That will leave an SPWeb object hanging around. Luckily getting SPContext.Current.Site does not leak. I've fixed the code up in the latest update to this post. Thanks for the pointer.