SPLookupField Helper function

written by admin on Thursday, March 20 2008

If you are programmatically adding list entries to a SharePoint 2007 list which contains a lookup field, you have to supply the ID of the source list's ListItem. If you fail to supply the ID you might see error messages such as this:

"Invalid data has been used to update the list item. The field you are trying to update may be read only."

You can easily see how this could happen - look at this code which attempts to create a new list item using easily imaginable strings:

SPListItemCollection listItems = _DestinationList.Items;
SPListItem newListEntry = listItems.Add();
newListEntry ["Group"] = groupName; // string value of desired list setting
newListEntry ["ItemName"] = itemName; // string value of desired list setting
newListEntry ["ItemQuantity"] = newItemQty;  
newListEntry ["Name"] = name;
newListEntry ["Email Address"] = email;
// persist changes
newListEntry .Update(); 

To help me with this all too common problem, I've dashed off this simple little helper function

/// <summary>
/// This static class exposes some helper methods
/// </summary>
static class Helper
{ 

    /// <summary>
    /// When updating a list, you can't insert a string value into a lookup field.  You must insert the ID of the list item in the source list
    /// </summary>
    /// <param name="lookupValue">String you need to find the ID for</param>
    /// <param name="lookupSourceList">Lookup source list</param>
    /// <returns>returns the ID of the lookup string in the source list</returns>
    public static int GetLookupFieldID(string lookupValue, SPList lookupSourceList)
    {
        int LookupFieldID = -1; 

        // get the item in the list
        SPQuery query = new Microsoft.SharePoint.SPQuery();
        query.Query = String.Format("<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>{0}</Value></Eq></Where>", lookupValue);
        SPListItemCollection listItems = lookupSourceList.GetItems(query); 

        // get the item ID
        foreach (Microsoft.SharePoint.SPListItem item in listItems)
        {
            // get the e-mail address and user ID from the XML
            LookupFieldID = item.ID;
        }
        return LookupFieldID;
    }   
}

Using the above function to elegantly resolve those IDs.  In the code below, I've also pointed out some other errors to watch out for.  The list of links at the bottom of this post points to some blog entries where other folks have posted solutions of their own to the same Read Only error message.

SPListItemCollection listItems = _DestinationList.Items;
SPListItem newListEntry = listItems.Add();
newListEntry ["Title"] = Guid.NewGuid().ToString(); // leaving out required fields may result in the same Read Only error
newListEntry ["Group"] = Helper.GetLookupFieldID(groupName, _MasterGroupsList);
newListEntry ["ItemName"] = Helper.GetLookupFieldID(itemName, _MasterItemsList);
double newItemQty;
double.TryParse(enteredValue, out newItemQty);
newListEntry ["ItemQuantity"] = newItemQty;  // supplying the wrong data type to a Number FieldType may also result in the same Read Only error
newListEntry ["Name"] = name;
newListEntry ["Email_x0020_Address"] = email; // supplying the wrong internal field name may also result in the same Read Only error
// persist changes
newListEntry .Update(); 
 

Similar Posts

  1. Strange Problems with a mysteriously corrupted list
  2. When deploying code, please use Solution Packages for both our sakes
  3. Collaborate over the Internet with OneNote and Office Live

Comments are closed

Options:

Size

Colors