1

This should be an easy one but for some reason the answer is escaping me.

I am executing a query with joins that includes projected fields (fields from one of the joined lists) using JSOM.

Using server side code, the type of one of these field values is String and has a value in the format of "ID;#Value".

Using JSOM, how do I get the value of this field? When I call the .toString() method on the field value, I get "[object Object]". If I call typeof() on the field value, I get "string". What am I missing?

* UPDATE *

The following appears in the query XML...

<ProjectedFields>
  <Field Name="LastName" Type="Lookup" List="Employees" ShowField="LAST_NAME" />
</ProjectedFields>
<ViewFields>
  <FieldRef Name="LastName" />
</ViewFields>

The following server-side code...

listItem["LastName"]

...will yield something like "1;#Smith".

The following client-side code...

listItem.get_item('LastName')

...will yield "[object Object]". The type of this field value is indeed string (just as it was for the server-side code) as evidenced by the statement...

typeof(listItem.get_item('LastName')

...which yields "string".

Again, the question is how do I get the value "1;#Smith" using the client-side code?

1

Your field is a lookup field, wich is represented by a complex type SP.FieldLookupValue on the client side. You can retrieve the label and id as shown below:

var lastNameValue = item.get_item('LastName');
var lastNameLabel = lastNameValue.get_lookupValue();
var lastNameId = lastNameValue.get_lookupId();
  • Thank you for the help. The fact that the type of the value was being reported as string was confusing. After thinking about it a bit longer, I guess this is how the server-side code behaves (sort of). – Jason Richmeier Jan 23 '15 at 19:57
3

I suggest you use Fiddler (http://www.telerik.com/fiddler) to inspect the raw JSON being returned from the service call. Seeing what the raw data looks like should help you understand how it's represented in JavaScript.

enter image description here

  • Thank you for the response. I marked the other answer as the solution as it took my issue to completion. I couldn't even up-vote your answer as I do not have enough reputation points. – Jason Richmeier Jan 23 '15 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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