Posted by: R Manimaran | August 3, 2010

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

One of friend came up with this error while updating a lookup column in a list in SharePoint event handler.

When updating the look up column, we should not assign the value directly. Instead,we need to assign the Id of the lookup value.

I have a lookup column Country which gets the value from another list named “Country”.

If I update the value as

item[“Country”]=”India”;

results in the above error : Invalid data has been used to update the list item. The field you are trying to update may be read only.

Here I wrote one helper function which will return the SPLookupFieldValueCollection which can be set to the field.

public static SPFieldLookupValueCollection GetLookFieldIDS(string lookupValues,
SPList lookupSourceList)
{
SPFieldLookupValueCollection lookupIds = new SPFieldLookupValueCollection();
string[] lookups = lookupValues.Split(new char[] { ‘,’ },StringSplitOptions.RemoveEmptyEntries);
foreach (string lookupValue in lookups)
{
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);
foreach (Microsoft.SharePoint.SPListItem item in listItems)
{
SPFieldLookupValue value = new SPFieldLookupValue(item.ID.ToString());
lookupIds.Add(value);
break;
}
}
return lookupIds;
}
  • Call this method like this

item["Country"] = GetLookFieldIDS(“Australia”, lookupFieldList);

  • If the LookupField allows multiple values

item["Country"] = GetLookFieldIDS(“Australia,India”, lookupFieldList);


About these ads

Responses

  1. If you come to this error when creating a Custom Field within SharePoint it may be because you have the ParentType set to “Lookup” and the value that is getting saved is a string type. Make sure the value that is getting saved by your custom control is a integer type.

  2. Thank you so much for this. It helped me! :D

  3. Thanks for this article.. you saved my day.. :)

  4. Great.. saved my time.. thanks

  5. Gr8 , Saved my day….Thanks

  6. Really useful… :)

  7. I created a custom user field and encoutnered this issue. Instead of assigning an SPUser object to the field I assigned the integer ID of the SPUser and my field now works. Thanks!

  8. [...] I already write a blog on updating the Lookup field using the Server Object Model.  Here is the blog post for reference. Click here [...]

  9. thank you very much………you really saved my day……..

  10. thanks, you rock


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: