Share via


Error: Unable to cast object of type 'System.String' to type 'Microsoft.SharePoint.SPFieldLookup'.

Question

Tuesday, March 5, 2013 12:32 PM

Hi,

I am trying to fetch the lookup value from the splistitem. I am getting the error as :

Unable to cast object of type 'System.String' to type 'Microsoft.SharePoint.SPFieldLookup'.

I have tried the below code:

SPListItemCollection spListItemCol = destList.GetItems(query);
                    if (spListItemCol != null)
                    {
                        for (int j = 0; j < spListItemCol.Count; j++)
                        {
                            SPListItem archiveIte = spListItemCol[j];
SPFieldLookup field = (SPFieldLookup)archiveIte["mylookupcolumn"];
}
}

How to fix this?

Thanks

All replies (2)

Tuesday, March 5, 2013 12:59 PM ✅Answered

Hi Venkatzeus,

Use the following code to get the lookup value from SPListItem:

SPFieldLookupValue itemValue = new SPFieldLookupValue(archiveIte["mylookupcolumn"] as string??string.Empty);

Hope this helps.

Kind Regards,
Teodora Gancheva

blog: Tips on technology


Tuesday, March 5, 2013 12:59 PM ✅Answered

Hi Venkat,

Lookup field values are returned in the format of ID:#<FieldValue>

So you cannot cast it directly in the above mentioned manner

If you want to extract the Field Value try as below

Please refer to below article

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldlookup.aspx