Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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 :)

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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 '13 at 7:57
    
Glad I could point you in the right direction! –  Robert Lindgren Apr 25 '13 at 7:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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