0

I have a field called Monitoring System that has a type of lookup in my Sharepoint 2010 list. I want to use C# windows form to return the data for this field, and I tried

foreach (ListItem listItem in items)
{
    var ms = listItem["MonitorSys"];
}

However, when I look in the immediate window this is what I see

Microsoft.Sharepoint.Client.FieldLookupValue
LookupID: 138
LookupValue: "Manderin Villa Services"
TypeId: "{xkllkjasdkfjlasfd}"

it looks like what I want is the LookupValue but how do I extract that? using CSOM

I tried this syntax, but when it hits my variable assignment it immediately loads the win form and stepping through the code with F11 it does the same...

FieldUserValue monsys = (FieldUserValue)listItem["MonitoringSystem"];
string msys = monsys.LookupValue;

Full code is:

Public static void GetSPData()
{
  using (var ctx = new ClientContext(SPURL))
  {
    List list = web.Lists.GetByTitle(SPSite);
    CamlQuery camlQuery = new CamlQuery();
    camlQuery.ViewXml= strong.Format();

    ListItemCollection items = list.GetItems(camlQuery);
    ctx.Load(list);
    ctx.Load(items);
    ctx.ExecuteQuery();

    foreach (ListItem listItem in items)
    {
      //this is where I am having the issues
      FieldUserValue monsys = (FieldUserValue)listItem["MonitoringSystem"];
      string msys = monsys.LookupValue;
    }
  }
}
  • "loads the win form" - You have not posted the full code... Is there a try catch that's letting the error slip by? – Mike Smith - MCT - MVP May 2 '18 at 0:16
0

Try either of these:

FieldLookupValue monsys = (FieldLookupValue)listItem["MonitoringSystem"];
string msys = monsys.LookupValue;

var monsys = listItem["MonitoringSystem"];
string msys = monsys.LookupValue;

You may need to explicitly load the properties. There are several ways to do this. Not having seen the rest of your code, maybe like this:

context.Load(list.Fields); 

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code#sharepoint-field-tasks

or

https://docs.microsoft.com/en-us/sharepoint/dev/sp-add-ins/complete-basic-operations-using-sharepoint-client-library-code#retrieve-a-specific-field-from-the-list

  • Using your first example of FieldLookupValue I get the same result as my OP using FieldUserValue, it hits the assignment line and immediately launches the form. If I try the second example, when I type monsys. I do not have LookupValue as an option, only Equals, GetHashCode, GetType, and ToString – user2932408 May 1 '18 at 21:14
  • I have updated the answer with notes about loading the field. – Mike Smith - MCT - MVP May 2 '18 at 0:21
  • My apology see my edit for further syntax. I appreciate the help! – user2932408 May 2 '18 at 13:32

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.