RiksonTool RiksonTool - 17 days ago 9
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>
<Query>
<Where>
<Eq>
<FieldRef Name='PONo' />
<Value Type='Lookup'>" + poNo + @"</Value>
</Eq>
</Where>
</Query>
</View>";

ListItemCollection items = list.GetItems(caml);
clientContext.Load<ListItemCollection>(items);
clientContext.ExecuteQuery();


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

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;
}