none
How to retrieve SharePoint online look up coulmn RRS feed

  • Question

  • Hi,

     I m developing an add inMVC ,I</g></g> want to get SP list values, but the column is attached to another list as a lookupcolumn</g>. So I have done the coding as follows.

    Once I run the add in</g> I m ending up getting this Microsoft.SharePoint.Client.FieldLookupValue as the column value. Any help please. 

     public static List<Models.Leave> GetLeaves(SharePointContext spContext, CamlQuery camlQuery)
            {
                string departmentval;
                List<Models.Leave> leaves = new List<Models.Leave>();
                using (var clientContext = spContext.CreateUserClientContextForSPHost())
                {
                    if (clientContext != null)
                    {
                        List lstLeaves = clientContext.Web.Lists.GetByTitle("Leave Requests");
                        ListItemCollection lstLeaveItems = lstLeaves.GetItems(camlQuery);
                        clientContext.Load(lstLeaveItems);
                        clientContext.ExecuteQuery();
                        if (lstLeaveItems != null)
                        {
                            foreach (var lstLeaveItem in lstLeaveItems)
                            {
                                departmentval = (lstLeaveItem["Department"]).ToString();
                                FieldLookupValue DeptfieldValue = lstLeaveItem["Department"] as FieldLookupValue;
                              //  DeptfieldValue.LookupValue = departmentval;
                                leaves.Add(
                                new Models.Leave
                                {
                                    Description = lstLeaveItem["Title"].ToString(),
                                    StartDate = (DateTime)lstLeaveItem["StartDate"],
                                    EndDate = (DateTime)lstLeaveItem["EndDate"],
                                    Reason = lstLeaveItem["Reason"].ToString(),
                                    Manager = lstLeaveItem["Manager"].ToString(),
                                    Status = lstLeaveItem["Status"].ToString(),
                                    Department = DeptfieldValue.ToString()

                                });
                            }
                        }
                    }
                }
                return leaves;
            }

    The view as follows 

     @foreach (Leave objLeave in @Model)                    
                        {
                            <tr>
                                <td>@objLeave.Description</td>
                                <td>@objLeave.StartDate.ToString("MM/dd/yyy")</td>
                                <td>@objLeave.EndDate.ToString("MM/dd/yyy")</td>
                                <td>@objLeave.Reason</td>
                                <td>@objLeave.Manager</td>
                                <td>@objLeave.Status</td>
                                <td>@objLeave.Department.ToString()</td>
                            </tr>
                        }


    d.n weerasinghe





    • Edited by newnw Wednesday, February 22, 2017 5:56 AM
    Wednesday, February 22, 2017 5:54 AM

Answers

  • Hi,

    You can get the string value of a lookup column by accessing the FieldLookupValue.LookupValue property:

    https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.fieldlookupvalue.lookupvalue.aspx

    the code is similar like this:

    FieldLookupValue lookup = item["LK1"] as FieldLookupValue;
    string value = lookup.LookupValue;
    int id = lookup.LookupId;

    Best regards,

    Patrick

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Proposed as answer by Maruthachalam Thursday, February 23, 2017 9:45 AM
    • Marked as answer by newnw Monday, February 27, 2017 1:22 AM
    Thursday, February 23, 2017 8:59 AM
    Moderator
  • Hi Patrick,

    Thank you very much for your help, I have achieved this as follows.

     Department = (lstLeaveItem.FieldValues["Department"] as FieldLookupValue).LookupValue.ToString()


    d.n weerasinghe

    • Marked as answer by newnw Monday, February 27, 2017 1:22 AM
    Monday, February 27, 2017 1:22 AM

All replies

  • Hi,

    You can get the string value of a lookup column by accessing the FieldLookupValue.LookupValue property:

    https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.fieldlookupvalue.lookupvalue.aspx

    the code is similar like this:

    FieldLookupValue lookup = item["LK1"] as FieldLookupValue;
    string value = lookup.LookupValue;
    int id = lookup.LookupId;

    Best regards,

    Patrick

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Proposed as answer by Maruthachalam Thursday, February 23, 2017 9:45 AM
    • Marked as answer by newnw Monday, February 27, 2017 1:22 AM
    Thursday, February 23, 2017 8:59 AM
    Moderator
  • Hi newnw,

    Any progress now?

    Best regards,

    Patrick


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, February 24, 2017 9:38 AM
    Moderator
  • Hi Patrick,

    Thank you very much for your help, I have achieved this as follows.

     Department = (lstLeaveItem.FieldValues["Department"] as FieldLookupValue).LookupValue.ToString()


    d.n weerasinghe

    • Marked as answer by newnw Monday, February 27, 2017 1:22 AM
    Monday, February 27, 2017 1:22 AM