RiksonTool
RiksonTool

Reputation: 167

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?

Upvotes: 0

Views: 2119

Answers (1)

Jaime Macias
Jaime Macias

Reputation: 847

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

Upvotes: 1

Related Questions