RiksonTool RiksonTool - 1 year ago 148
C# Question

SharePoint lookup field returning null value

I am using CSOM to retrieve data from sharePoint online.
I need to get data from a document lib. here is syntax which I used to retrieve data.

List list = clientContext.Web.Lists.GetByTitle("Required Documents");
if (list != null)
CamlQuery caml = new CamlQuery();
caml.ViewXml = @"<View>
<FieldRef Name='PONo' />
<Value Type='Lookup'>" + poNo + @"</Value>

ListItemCollection items = list.GetItems(caml);

here PONo is a lookup for another list item.
So I tried to get value as below, but it returns null.

var itm = item.FieldValues["PONo"] as FieldUserValue;

when try like this,

var itm = item.FieldValues["PONo"];

It returns needful value. what would be the problem?

Answer Source

Try it like this, FieldUserValue is useful when working with Users but in this case you need FieldLookupValue.

var PONo = item["PONo"] as FieldLookupValue;

if (PONo!= null)
    var PONo_Value = PONo.LookupValue;
    var PONo_Id = PONo.LookupId;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download