0

What can I do to see if the current User ID already exists in the column ("QuemLeuId") of type "Person Group" for particular Item in the list?

Can someone help me? Thank you.

window.onload = function(){
    console.log("Dentro da PageLayout");
    var userId = _spPageContextInfo.userId;
    console.log(userId);

    var urlParams = new URLSearchParams(window.location.search);
    var myParam = urlParams.get('M_ID');
    console.log(myParam);

    jQuery.ajax({
        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Páginas')/items('" + myParam +"')",
        method: "GET",
        headers: {
            "Accept": "application/json; odata=verbose" 
        },
        success: function (data) {
            console.log(data);
        },
        error: function (data) {
            console.log(data);
        }
    });

}
  • Where do you want to check this? On display/edit form of list? And what is the name of your person and group column? – Ganesh Sanap Dec 12 '18 at 16:42
  • The name is "QuemLeuId". – KmDroid Dec 13 '18 at 11:38
  • Is multiple selection allowed on this column? – Ganesh Sanap Dec 13 '18 at 12:13
  • @GaneshSanap Yes – KmDroid Dec 13 '18 at 12:23
0

Try Using below code:

jQuery.ajax({
    url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('Páginas')/items('" + myParam +"')?$select=ID,Title,QuemLeuId/Id&$expand=QuemLeuId",
    method: "GET",
    headers: {
        "Accept": "application/json; odata=verbose" 
    },
    success: function (data) {
        if(data.d){
            if(data.d.QuemLeuId.Id == userId){
                console.log("User Found.");
            }
        }
    },
    error: function (error) {
        console.log(error);
    }
});

If Multiple Selection is allowed on Person or Group Field then you need to traverse the data.d.QuemLeuId.results array and check if the user is found or not(Use below code inside success function):

if(data.d){
    for (var i = 0; i < data.d.QuemLeuId.results.length; i++){
        if(data.d.QuemLeuId.results[i].Id == userId){
            console.log("User Found.");
            break;
        }
    }
}
  • It's working! Is it normal display this " 'length' of undefined" when it doesnt found any userId in the column? Thank you. – KmDroid Dec 13 '18 at 12:40
  • To avoid that error, you can add one more check like if(data.d.QuemLeuId.results){} before for loop. please upvote answer if it helped you. – Ganesh Sanap Dec 13 '18 at 13:18
  • Thank you! I also have this ticket, could you take a look? Link: sharepoint.stackexchange.com/questions/254202/…, I think you can help me to solve this problem. – KmDroid Dec 13 '18 at 13:35
1

If your ajax request returns the item successfully you should be able to check the person/group column with data.d.QuemLeuId == userId in case of single user column or data.d.QuemLeuId.indexOf(userId) > -1 if it's a multi-user column.

  • I used this code inside success: if(data.d.QuemLeu.indexOf(userId) > -1){ console.log("OK!"); } And display an error "Cannot read property 'indexOf' of undefined". What should I do to solve this? Thank you. – KmDroid Dec 13 '18 at 12:00
  • Then your user column is probably single-user/group and you can check directly using data.d.QuemLeuId == userId. I'd recommend calling console.log(data); first thing in your success function so you can easily see the structure of the web service response – Steve T Dec 13 '18 at 14:53
0

Use REST api to filter based on user filed, check if data length returned >0.

19 is user ID.

/_api/web/lists/getbytitle('mylist3')/items?$select=Title,QuemLeuId/Id&$expand=QuemLeuId&$filter=QuemLeuId/Id eq 19
  • In your example is searching for the UserID in the all list. What I want is search in column ('QuemLeuId') but for a specific item. Should I do like .../items('" + itemid +"')/?$select=Title...?. Thank you. – KmDroid Dec 13 '18 at 11:37

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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