0

In js, i retrieve some items and inside there are lookup column and people column. When i print them, it's [object Object] Are there a way to determine if it's a lookup column?

This one doesn't work

alert(lookup instanceof FieldLookupValue);

Finally how can i value and id? I tried this but doesn't work

var lookup = items[cpt].get_item("userCol");
alert(lookup);
item_requester_username = lookup.get_fieldValues().Requester.get_lookupValue(); 
var item_requester_id = lookup.get_fieldValues().Requester.get_lookupId();
alert('The Requester Username and ID : ' + item_requester_username + ' ; ' +    item_requester_id);

3 Answers 3

1

Here is an example to get lookupValue and lookupId

var lookup = item.get_item('userCol');
var value= lookup.get_lookupValue();
var id= lookup.get_lookupId();
0
1

User field will be instance of SP.FieldUserValue.
I don't think that inherits from SP.FieldLookupValue in contrast to say CSOM, ref SP.FieldUserValue.prototype.constructor is function and same with SP.FieldUserValue.constructor.

So your check would be:

var conditional = lookup instanceof SP.FieldUserValue;

Another simple hack is to simply check if the function get_lookupId exists as a property on the object:

var conditional = typeof lookup.get_lookupId === 'function';
0
0

You can get the type directly from the field. Try something like this:

fieldCollection = list.get_fields();
field = fieldCollection.getByTitle("Your field title");
clientContext.load(field);

Then, on the success handler you could alert the type with SP.Field.typeAsString:

alert(field.get_typeAsString());

(notice: I suppose that the field variable will be in scope, but this is outside the question).

Once you know the string associated to the type you want to handle, you can just test for it.


EDIT: Since you seem to actually need the field type (as per question title) and not the field type (as the question test suggested - Are there a way to determine if it's a lookup column), please use the solution with

var value= lookup.get_lookupValue();
var id= lookup.get_lookupId();

already proposed by Nandeem. I am leaving this reply only in case someone needs info on the typeAsString method.

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.