2

I'm trying to fetch some data from the hostweb from my SharePoint hosted app. This is all going well, however I am having a problem when I try to fetch a multi lookup fields value. Below is my request:

        url: appweburl + "/_api/SP.AppContextSite(@target)/web/lists/getbytitle('Aktiviteter')/items?&select=Title,Location/Id&expand=Location&@target='" + hostweburl + "'"

above request is working, and is fetching the list items, however not the lookup field. The lookup field is a multi lookup field to another custom list on the hostweb. Is there a way to expand when fetching multi lookups, or am I fooling myself?

2 Answers 2

2

Multiple-Column Lookup value is represented as an array of SP.FieldLookupValue objects.

How to read multi-valued lookup field using SharePoint 2013 REST

Assume Employee list with a multi-valued lookup field Departments

Then the query: /_api/web/lists/getbytitle('<list title>')/items returns an array of lookup ids, for example:

getListItems(_spPageContextInfo.webAbsoluteUrl,'','Employees',
  function(items){
     if(items.length > 0)
        console.log(items[0].DepartmentsId);   //returns an array [LookupId1, LookupId1 .. LookupIdN]
  },
  function(error){
     console.log(error.responseText);     
  });

The query:

/_api/web/lists/getbytitle('Employees')/items?$select=Departments/Title,Departments/Id&$expand=Departments

returns projected Departments array of objects as shown below:

getListItems(_spPageContextInfo.webAbsoluteUrl,'?$select=Departments/Title,Departments/Id&$expand=Departments','Employees',
  function(items){
     if(items.length > 0)
        if(items[0].Departments.results.length > 0) {
           console.log(items[0].Departments.results[0].Title);   
           console.log(items[0].Departments.results[0].Id);   
        }   
  },
  function(error){
     console.log(error.responseText);     
  });

SharePoint 2013 REST read list items function:

function getListItems(url, query, listName, success, failure) {
    $.ajax({
        url: url + "/_api/web/lists/getbytitle('" + listName + "')/items" + query,
        method: "GET",
        headers: { "Accept": "application/json; odata=verbose" },
        success: function (data) {
            success(data.d.results);
        },
        error: function (data) {
            failure(data);
        }
    });
}
0

Looking your uri, it's look fine, I believe that your problem is how parse de data. I set an uri and this return this to me:

<entry m:etag="&quot;9&quot;">
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Familias_Controles" type="application/atom+xml;type=feed" title="Familias_Controles" href="Web/Lists(guid'028c99e1-d98c-4206-a248-1ac4e3c64381')/Items(296)/Familias_Controles">
    <m:inline>
        <feed>
            <id>1cd20d60-cb23-4578-9582-a0159aaa165e</id>
            <title />
            <updated>2015-09-09T12:40:40Z</updated>
            <entry>
                <id>32cda293-4079-432e-8481-9f32d62e8bce</id>
                <category term="SP.Data.LM_x005f_Familia_x005f_ControlListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
                <title />
                <updated>2015-09-09T12:40:40Z</updated>
                <author>
                <name />
                </author>
                <content type="application/xml">
                <m:properties>
                <d:Id m:type="Edm.Int32">2</d:Id>
                </m:properties>
                </content>
            </entry>
            <entry>
                <id>ffcc5b9f-e08f-44ad-a9c7-6bd27fa1acfc</id>
                <category term="SP.Data.LM_x005f_Familia_x005f_ControlListItem" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
                <title />
                <updated>2015-09-09T12:40:40Z</updated>
                <author>
                <name />
                </author>
                <content type="application/xml">
                <m:properties>
                <d:Id m:type="Edm.Int32">4</d:Id>
                </m:properties>
                </content>
            </entry>
        </feed>
    </m:inline>
</link>

And JavaScript parse this data like this:

{
"d":
    {"results":[
        {
        "__metadata":{"id":"d6662447-1630-4e77-9ec8-20b601a7f091","uri":"http://molymetsig.colabra.cl/sites/sig/molynor/_api/Web/Lists(guid'028c99e1-d98c-4206-a248-1ac4e3c64381')/Items(296)","etag":"\"9\"","type":"SP.Data.Gestor_x005f_DocumentalItem"},
        "Familias_Controles":
            {"results":[
                    {
                        "__metadata":
                        {
                            "id":"f80ddc7f-f5c4-47df-be85-3e86292f431d",
                            "type":"SP.Data.LM_x005f_Familia_x005f_ControlListItem"
                        }
                        ,
                        "Id":2
                    },
                    {   
                        "__metadata":
                        {
                            "id":"b3c5e814-7e6a-47c0-993d-5dbe17deaf91",
                            "type":"SP.Data.LM_x005f_Familia_x005f_ControlListItem"
                        },
                        "Id":4
                    }
                ]
            }
        }]
    }
}

You could get the data like this

data data.d.results[0].Location.results

Wich one is an array of id's. I hope I was not too complicated.

Greetings :)

1
  • I will give that a go a bit later and get back to you :)
    – MazZ
    Commented Sep 10, 2015 at 9:03

Your Answer

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

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