0

How can I filter the API call dynamically passing through variable?

I am getting value in this variable through another API call:

var userid;

 this.props.context.msGraphClientFactory.getClient().then((client: MSGraphClient): void => {
  client
    .api('users')
    .filter('identities/any(c:c/id eq '+ userid)

In the console I am getting this value in the filter query, instead I am not getting value of the variable "userid".

enter image description here

In the render function, storing the value of variable userid.

Making variable userid global by writing simply var userid; at the top where we import the library.

public render(): React.ReactElement<IReactTaskProps> {

 return (
  <div className="cal" >
    
        {this.state.events.map((item, key) =>
       {if(item.percentComplete != 100){
        var tablerow = $("<tr>");

        tablerow.append($('<td>').append("<a href=" +"https://tasks.office.com/credentinfotech.com/en-GB/Home/Planner#/plantaskboard?planId=" + item.planId + "&taskId=" + item.id + " target='_blank' >" + item.title ))
        tablerow.append($('<td>').append((() => {
          switch (item.percentComplete) {
            case 0: return "Not-Started"
            case 50: return "In-Progress";
          }
        })()))
        tablerow.append($('<td>').append(moment(item.startDateTime).format('MM/DD/YYYY ')));
        tablerow.append($('<td>').append(moment(item.dueDateTime).format('MM/DD/YYYY')));
       

 //here I am storing the value in the variable userid

      userid = item.createdBy.user.id;
        console.log(userid);
          
        tablerow.append($('<td>').append(profilename))
       
      $("#tbody").append(tablerow);
    
          
       }}
       )
      }      
  </table>  
        
  </div>
  </div>
    ); 
}
1
  • Try below solution. let me know if it works for you. Commented Feb 24, 2021 at 14:33

2 Answers 2

1

Use like this:

.filter(`identities/any(c:c/id eq ${userid}`)

Make sure first part is correct.

Reference: Consume the Microsoft Graph in the SharePoint Framework - Using MSGraphClient


You can also use it like:

let filterString = "identities/any(c:c/id eq " + userid;

this.props.context.msGraphClientFactory.getClient().then((client: MSGraphClient): void => {
  client
    .api('users')
    .filter(filterString)
5
  • @abhishek Have you tried this? is it working for you? Commented Feb 24, 2021 at 15:18
  • I have used both the way but i am not getting the varaiable userid value in the filter query Commented Feb 25, 2021 at 5:09
  • 1
    You have defined var userid; just before API call. I can see that you are not assigning any value to this variable....try adding some static value first and then check value of filterString before API call using console.log(filterString); Commented Feb 25, 2021 at 5:24
  • filter query is working by adding static value to the variable userid , but why i am not getting dynamic value to global variable userid Commented Feb 25, 2021 at 6:23
  • 1
    So, that is different issue. it is based on how/when you are setting value of userid variable. If you are calling API before setting the value of userid, you will not be able to access its value. Make sure first you are setting the variable value and then you are accessing it (by calling API). Also, if you are creating a global variable, you should not write var userid; again before API call. Commented Feb 25, 2021 at 7:14
0

Instead of filtering the api i have used api like that

.api('users/'+`${userid}`)

Declaring userid as a global variable with dynamic id

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.