6

I have a List (Topics) which has a MultiLookupField to a List named (Speakers)

Speakers has a User Field (UserName).

What I'm trying to do is to read some of the User Fields Properties. Which works when I'm querying the List with the User Field directly:

_api/web/getbytitle('Speaker')/items(1)?$select=UserName/Id&$expand=UserName

However I'm not able to get the User Field through the Topics list:

These are my desperate attempts which are all not working:

_api/web/lists/getbytitle('Topics')/items(1)?$select=Speakers/ID,Speakers/UserName&$expand=Speakers

_api/web/lists/getbytitle('Topics')/items(1)?$select=Speakers/ID,Speakers/UserNameId&$expand=Speakers

_api/web/lists/getbytitle('Topics')/items(1)?$select=Speakers/ID,Speakers/UserName/Id&$expand=Speakers,Speakers/UserName/Id

Edit:

I'm also facing the same behaviour when I'm trying to select Boolean or Choice Fields in the Lookup List.

Providing a little Image which should show my situation:

enter image description here

so for clarification my Topics List has a MultiLookup Column Speakers which is bound to the List Speakers. The List Speakers has some other fields and a User Field.

And I'm trying to expand some of the User's Field Properties through Rest.

7
  • 1
    Can you elaborate on 'not working'? Are you getting an error? If so, what error? Are you getting unexpected results?
    – wjervis
    Dec 16, 2014 at 13:21
  • 1
    I only get 400 Bad Request. The query to field 'xxx' is not valid. When trying to get one of that fields, tested now with Boolean, Choice and User Fields in a Lookup List.
    – Mark
    Dec 16, 2014 at 13:29
  • 1
    Have you tested the url to make sure it works in either a browser or a REST app?
    – wjervis
    Dec 16, 2014 at 13:30
  • By the way, the first url is valid, the others are not.
    – wjervis
    Dec 16, 2014 at 13:32
  • I have tested it in a Browser and an Rest app. I know that they are wrong. But as I didn't get it to work. I have tried and played around with the Url to get it working.
    – Mark
    Dec 16, 2014 at 13:33

2 Answers 2

3

Isn't possible expand lookup via Rest Api. Only for user(column default) and field with atribute Deferred.

But have solution for this:

loop items and get all items and selecting only the SpeakersID

_api/Web/Lists/GetByTitle('Topics')/items?$select=SpeakersId

will return as show image below: json topics and speakers

but isn't suficient for get user info:

Update(2):

then you will use 2 rest api for your case.

_api/Web/Lists/GetByTitle('Topics')/items?$select=SpeakersId;
_api/Web/Lists/GetByTitle('Speakers')/items(n)?$select=UserName/Id,UserName/Title&$expand=UserName;

logic generic.

url="..../_api/Web/Lists/GetByTitle('Topics')/items?$select=SpeakersId";
call ajax....(sucess:getItems,...)
function getItems(data){
  var items = data.d.results; // array of items
  var item;
  for(var i =0;i<items.length;i++){
    item = items[i];
    speakers = item.SpeakersId.results;
    for(var j =0;j<speakers.length;j++){
      var LookupID_Speaker = speakers[j];
      var url = "_api/Web/Lists/GetByTitle('Speakers')/items("+LookupID_Speaker +")?$select=UserName/Id,UserName/Title&$expand=UserName";
      // set ajax async to false;
      call ajax....(sucess:getProperties,...)
    }
  }
}

function getProperties(data){
  var UserID = data.UserNameId;
  var UserTitle = data.Title;

  // do you code here.

}
3
  • So when i'm facing Boolean,Choice,User and Lookup Fields in a Lookup List this results everytime in 3 calls to get the values? That's really an awful way if there is no other possibility.
    – Mark
    Dec 17, 2014 at 7:12
  • yeap this is terrible to implementation to get user information :/ but not impossible.
    – b1c10
    Dec 17, 2014 at 13:35
  • see my update post, have tested here and work fine.
    – b1c10
    Dec 17, 2014 at 13:36
0

The REST API doesn't seem to want to fetch the UserName field, try Name instead and you should get the value that's actually in UserName even though my User Information List has a field called Name with a different value in it.

I find Id works when proper cased, but not upper (so no ID)

Try this query:

_api/web/lists/getbytitle('Topics')/items(1)?
    $select=Speakers/Id,Speakers/Name&$expand=Speakers

This works with the standard "User Information List" fields for Users. It may not work the same on a customized list holding user information.

1
  • This doesn't work either please see my edit it may clarifies my problem.
    – Mark
    Dec 16, 2014 at 14:22

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.