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

Currently, I am using an ajax request to access the list items. The fields that are not lookups come as expected but the lookup fields come across as an ID of the object that corresponds to its original list. How can I get the title of the lookup? I have been looking at a lot of post but I cannot figure out how to adapt the things I have found to my code.

My code posted below is just the request and the saving of the data. My ajax code looks like:

 jQuery.ajax({

        url: siteUrl + "/_api/lists/getbytitle('" + listName + "')/items", 
        type: "GET",
        headers: {
            "accept": "application/json;odata=verbose",
        },
        success: function (data) {
            jQuery.each(data.d.results, function (index, item) {
                listItem =  item;
                  //creates the new object
                var taskHolder = new Task(
                    item.ID,
                    item.Title,
                    item.Name,
                    item.Client,
                    item.Assigned_x0020_To,
                    item.Status
                );    
share|improve this question

1 Answer 1

up vote 4 down vote accepted

You need to use the $expand option in your query string.

siteUrl + "/_api/lists/getbytitle('" + listName + "')/items?$select=NameOfLookup/FieldYouWant&$expand=NameOfLookup/NameOfFieldYouWant"

So yours would be:

"/_api/lists/getbytitle('" + listName + "')/items?$select=$expand=status_x0028_test_x0029/Title&$expand=status_x0028_test_x0029/Title

This will bring back all of the associated data. Here I have a list called Test List and a lookup field called Status. The 2013 REST API does not support bulk loading the data like the ListData.svc REST service did/does.

Bulk expansion and selection of related items is not supported. --MSDN

So you have to specify the field you want in both the select and expand. Here is example output from my list in O365.

enter image description here

{
    "d": {
        "results": [
            {
                "__metadata": {
                    "id": "26f2b95d-ae86-44b7-bc69-dda79c88d742",
                    "uri": "https://domain.com/_api/Web/Lists(guid'efd16a43-aaf4-44fd-a9a1-4634afe328ac')/Items(2)",
                    "etag": "\"1\"",
                    "type": "SP.Data.Test_x0020_ListListItem"
                },
                "Status": {
                    "__metadata": {
                        "id": "86127a54-7c19-49c6-8e48-e4f56919433e",
                        "type": "SP.Data.StatusesListItem"
                    },
                    "Title": "Development"
                },
                "Title": "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dol",
                "StatusId": 3
            }
        ]
    }
}

Notice the JSON contain the Status field shown in the image witht he value of the Title field in the source list which is "Development"

"Status": {
    "__metadata": {
        "id": "86127a54-7c19-49c6-8e48-e4f56919433e",
        "type": "SP.Data.StatusesListItem"
    },
    "Title": "Development"
}
share|improve this answer
    
So, should I use the name of the look up as SP has it "status_x0028_test_x0029_Id"(this only holds the ID of the item from the other list) or as it is printed on the list page? It is printed as "status(test)". Currently, I have tried both but I keep getting a "value": "The field or property 'status_x0028_test_x0029' does not exist." or "value": "The expression \"status(test)\" is not valid." –  user24005 Feb 27 at 15:36
    
Thought it might be useful to see my actual queries: siteUrl + "/_api/lists/getbytitle('" + listName + "')/items?$expand=status(test) and siteUrl + "/_api/lists/getbytitle('" + listName + "')/items?$expand=status_x0028_test_x0029 –  user24005 Feb 27 at 15:41
    
I'm sorry, I was confusing the 2010 REST with 2013. I have corrected/updated my answer with more detail –  Robert Kaucher Feb 27 at 19:01
    
Thanks finally got it. It seems that the $select portion is required to use the $expand query. Thanks again. –  user24005 Feb 28 at 5:36

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.