0

I've written an autohosted-app to show a few data that are in a list.

To get the data in the list, I use this piece of code with a CAML Query.

using (var clientContext = TokenHelper.GetClientContextWithContextToken(hostWeb, contextToken, Request.Url.Authority))
{
    var web = clientContext.Web;
    var list = web.Lists.GetByTitle("Calendrier");

    string camlString =
        "<View><ViewFields>" +
            "<FieldRef Name='Title' />" +
            "<FieldRef Name='EventDate' />" +
            "<FieldRef Name='Category' />" +
            "<FieldRef Name='Location' />" +
            "<FieldRef Name='Eleve' />" +
        "</ViewFields></View>";

    var camlQuery = new Microsoft.SharePoint.Client.CamlQuery() { ViewXml = camlString };

    // allItems contains the data
    var allItems = list.GetItems(camlQuery);

    clientContext.Load(allItems);
    clientContext.ExecuteQuery();
    clientContext.Dispose();

    return View(allItems.ToList());
}

And here is the code in the view to print the data retrieved :

    @foreach (var item in Model)
    {

        <tr>
            <td>@item["EventDate"]</td>
            <td>@item["Location"]</td>
            <td>@item["Eleve"]</td>
            <td>@item["Category"]</td>
        </tr>
    }

But when I try to print the Eleve field in my View, everything went fine except for printing the "Eleve" column which is a lookup.

How can I get the lookup value of Eleve.Title instead of it to print 'Microsoft.SharePoint.Client.FieldLookupValue' in the column where I try to print @item["Eleve"]?

And if somebody have a good link to learn CAML for sharepoint 2013 thanks to share it :)

1 Answer 1

1

Try this in the view on the Eleve row:

<td>@(new SPFieldLookupValue(item["Eleve"].ToString()).LookupValue</td>

This should cast the item to a LookupValue, from witch you extract the value.

Or if you can not access SPFieldLookupValue class, you could make .ToString() on the item column and then parse out the value from the resulting string.

2
  • 1
    Thanks a lot :) Actually I just added .LookupValue to @item["Eleve"] as it was already a object of type FieldLookupValue. And it works fine ! Thanks again !!
    – ggobbe
    Apr 25, 2013 at 7:57
  • Glad I could point you in the right direction! Apr 25, 2013 at 7:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.