I have one list definition like this:

<ContentType ID="0x01..." Name="CNT_List" Group="List Content" Inherits="TRUE" Version="0">
      <FieldRef ID="{guid}" Name="PRF_Code" DisplayName="Code" />
      <FieldRef ID="{guid}" Name="PRF_ShortDescription" />
      <FieldRef ID="{guid}" Name="PRF_Description" />
      <FieldRef ID="{guid}" Name="PRF_Date" />
      <FieldRef ID="{guid}" Name="PRF_Country_Lookup" />
      <FieldRef ID="{guid}" Name="PRF_Regions_Lookup" />
      <FieldRef ID="{guid}" Name="PRF_StartDate" />
      <FieldRef ID="{guid}" Name="PRF_EndDate" />
      <FieldRef ID="{guid}" Name="PRF_Status_Lookup" />
      <FieldRef ID="{guid}" Name="PRF_Late" />
      <FieldRef ID="{guid}" Name="PRF_PreIntProgress" />

I'm trying retriving data with following code:

List<MyModel> model = new List<MyModel>();
SPList list = web.Lists.TryGetList("CNT_List");

if (list != null)
   SPListItemCollection coll = list .Items;

   foreach (SPListItem icoll in coll)

Now, I'm trying mapping data with following code:

public static MyMode SPListItemToModel(SPListItem listItem)
        if (listItem != null)
            MyModel model = new MyModel();

            model.ID = listItem.ID;
            model.Code = listItem["PRF_Code"].AsString();
            model.ShortDescr = listItem["PRF_ShortDescription"].AsString();
            model.Status = listItem["PRF_Status_Lookup"].AsInt();

If I try to execute my code I obtain one ArgoumentException error at line:

model.Status = listItem["PRF_Status_Lookup"].AsInt();

Which is the problem? Thanks


You need to get the value as below:

model.Status = ((SPFieldLookupValue)listItem["PRF_Status_Lookup"]).LookupValue;

You can refer the members of SPFieldLookupValue from here.

| improve this answer | |
  • Hi Nisarg, I've changed my code with your fix but it not work. "Value does not fall within the expected range.". – ilMattion May 5 '16 at 15:56
  • if the value of listItem["PRF_Status_Lookup"] is empty you will get that error, validate that the data is not empty – Marco May 5 '16 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.