Question

I have two simple lists. one is called employee which has 1 column called Title. Another is called Company which has 1 column called Title. I added a lookup column in the Employee List and pointed it to the ID of the Company List.

Now I wrote this code using CSOM (no javascript and no server object model)

    ClientContext c = new ClientContext(url);
    List el = c.Web.Lists.GetByTitle("Employee");
    CamlQuery query = new CamlQuery();
    query.ViewXml = @"<View>
                        <ViewFields>
                            <FieldRef Name='ID'/>
                            <FieldRef Name='Title'/>
                            <FieldRef Name='CompanyTitle'/>
                        </ViewFields>
                        <Joins>
                            <Join Type='LEFT' ListAlias='Company'>
                                <Eq>
                                    <FieldRef Name='Company' RefType='Id'/>
                                    <FieldRef List='Company' Name='ID'/>
                                </Eq>
                            </Join>
                        </Joins>
                        <ProjectedFields>
                            <Field Name='CompanyTitle' Type='Lookup' List='Company' ShowField='Title'/>
                        </ProjectedFields>
                        <Query>
                            <Where>
                                <Eq>
                                    <FieldRef Name='Company' LookupId='TRUE' />
                                    <Value Type='Lookup'>10</Value>
                                </Eq>
                            </Where>
                        </Query>
                    </View>";

    ListItemCollection items = el.GetItems(query);
    c.Load(items);
    c.ExecuteQuery();
    foreach (ListItem i in items) {
        Console.WriteLine(i["Title"]);
        Console.WriteLine(i["CompanyTitle"]);
    }

It works... but it prints

E01 Microsoft.SharePoint.Client.FieldLookupValue E11 Microsoft.SharePoint.Client.FieldLookupValue E21

But instead of seeing "Microsoft.SharePoint.Client.FieldLookupValue" I wanted to see the actual content of the column from the parent list.

what is wrong with my code?

Was it helpful?

Solution

Got it

Console.WriteLine(((FieldLookupValue)i["CompanyTitle"]).LookupValue);

putting it here so that it may help someone

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top