PowerAutomate and SharePoint OData filter queries

PowerAutomate has excellent actions related to SharePoint lists operations like getting, update, and create list items. The PowerAutomate guys introduced 'Filter Query' along with the 'Order by' and 'Top Count' as advanced features to these actions. The 'Filter Query' feature comes handy and can save me overcomplicating the PowerAutomate flow. By overcomplicating, I mean the possibility to pull all the list items and do the filtering inside the flow, which will introduce unnecessary data operations inside the flow, making it slow and complicated.

PowerAutomate SharePoint get list item filter by user

What is OData?

This question could be relevant for users that do not have a development background and have no clue what OData is. OData or the full name The Open Data Protocol is an HTTP Rest services specification made by Microsoft a long time ago, and the OData is built-in in many of the Office 365 Web services, Microsoft Graph, which is a web service by itself and other.

The problem with OData?

The problem with the OData specification is that it is cumbersome, contains lots of features, and different web services implement it partially. So, one feature of the filters like "substringof('..')" will work for one web service like SharePoint lists and will not work for other services. Because of the different implementation of the standard across Microsoft services, the different Microsoft engineering teams have different manuals on how to work with OData queries. Here is a link to the SharePoint that is not quite rich, and here is the Microsoft Graph team document on the same topic.

Handy queries when working with SharePoint lists

I have some experience with using Odata queries with SharePoint lists, and I would try to cover in this blog. First, it is not a trivial task to filter a SharePoint list or PowerAutomate flow by the Person or Group field. This field is an object containing multiple entities inside, not primitive like string, number, etc. so here are a few recipes on how to filter using SharePoint REST services, then in PowerAutomate flow.

Filter a SharePoint list by Person or Group field using SharePoint rest services

A few examples can come handy. A SharePoint list has a built-in field to track the creator of the item and who last edited the item as well as there might be a custom Person of Group fields. I will use the $select and $expand operators to show how I can get to these fields without any filtering initially.

https://mytenant.sharepoint.com/sites/site1/_api/web/lists/getbytitle('MyList')/items?$select=PersonOrGroupField/Name,PersonOrGroupField/Title&$expand=PersonOrGroupField
https://mytenant.sharepoint.com/sites/site1/_api/web/lists/getbytitle('MyList')/items?$select=ID,Author/Id,Author/Name,Author/Title&$expand=Author
https://mytenant.sharepoint.com/sites/site1/_api/web/lists/getbytitle('MyList')/items?$select=ID,Editor/Id,Editor/Name,Editor/Title&$expand=Editor

In the above examples, the PersonOrGroupField stands for a custom Person or Group field created in the list. The Author and Editor fields, as I mentioned, are system fields that come with every SharePoint list. Still, they are handy in filtering data since. Usually, I am interested in creators or editors in multiple business scenarios. Because the PersonOrGroupField is an object, I can access the different entities of the object by specifying PersonOrGroupField/Id, PersonOrGroupField/Name, or PersonOrGroupField/Title. The /Id stands for the SharePoint site user id, and it is number. The /Title stands for the user display name, ex. 'Velin Georgiev' and the /Name stands for I guess the user principal name, which looks like 'i:0#.f|membership|velin.georgiev@email.com' in SharePoint Online.

Filter Person or group field to equal a display name

Here is how I can filter a SharePoint list by user name using the $filter variable and the equal operator. Such a query can be used even from just a browser to pull data. More information on the $filter system query options can be found on the OData official site. Still, as I said before, it is no guarantee that any of the options are implemented for a specific Microsoft 365 or Microsoft Graph service.

https://mytenant.sharepoint.com/sites/mysite/_api/web/lists/getbytitle('MyList')/items?$select=ID,PersonOrGroupField/Id,PersonOrGroupField/Name,PersonOrGroupField/Title&$expand=PersonOrGroupField&$filter=PersonOrGroupField/Title eq 'Velin Georgiev'

Filter Person or group field that contains specific email

Another common scenario is to filter the list where I have only the user email. So then the "substringof('velin.georgiev@email.com',PersonOrGroupField/Name)" comes into play.

https://mytenant.sharepoint.com/sites/mysite/_api/web/lists/getbytitle('MyList')/items?$select=ID,PersonOrGroupField/Id,PersonOrGroupField/Name,PersonOrGroupField/Title&$expand=PersonOrGroupField&$filter=substringof('velin.georgiev@email.com',PersonOrGroupField/Name)

Filter a SharePoint list by Person or group field in PowerAutomate

I can apply the same technique in PowerAutomate flow, as can be seen in the picture below. The difference with SharePoint is that in SharePoint, the $expand=PersonOrGroupField operation is must, but somehow the flow handles this internally, and I specify just the filter query.

substringof('velin.georgiev@email.ie',PersonOrGroupField/Name)
PersonOrGroupField/Title eq 'Velin Georgiev'

Here is a picture of PowerAutomate. The flow is moving responses from Microsoft Form into a SharePoint list, and it is using the user email that submitted the form to filter the list items by the user.

PowerAutomate Microsoft Form Update SharePoint List Item

Filter list by date field form PowerAutomate and SharePoint REST

Another common scenario is to use filtering over dates in a SharePoint list, and again, the $filter operator can come in handy. The DateTime field also requires a specific format to be passed, so it still might not be trivial to guess the query. Here is how to do it in PowerAutomate first this time.

PowerAutomate SharePoint List Item Date check
DateTimeField eq null or DateTimeField le '@{addDays(utcNow(), 0, 'yyyy-MM-dd')}')

The SharePoint REST services will be similar, and again, two system DateTime fields are handy for filtering based on business requirements. The Created and Modified field are always present in every SharePoint list. The Created refers to when the list item hm as been created, the Modified date field is there to record the date of the last list item modification.

https://mytenant.sharepoint.com/sites/mysite/_api/web/lists/getbytitle('mysite')/items?$select=ID,DateTimeField&$filter=DateTimeField le '2020-02-03' or DateTimeField eq null

https://mytenant.sharepoint.com/sites/mysite/_api/web/lists/getbytitle('mysite')/items?$select=ID,Created&$filter=Created le '2020-02-03' or Created eq null

https://mytenant.sharepoint.com/sites/mysite/_api/web/lists/getbytitle('mysite')/items?$select=ID,Modified&$filter=Modified le '2020-02-03' or Modified eq null

I assume that the date format of the custom date fields could differ depending on the regional settings of the site so for example US format could be 'yyyy-MM-dd', but Brazil date format might be 'yyyy-dd-MM'

Conclusion

OData filtering is not trivial if the user does not have a programming background, and I experienced it multiple times, working with managers on PowerAutomate flows. SharePoint list is quite a convenient way to store data for business users and perform business automation using PowerAutomate. However, using Odata queries can simplify the PowerAutomate flow, and this is a huge benefit when it comes to troubleshooting and maintenance. Therefore, we as a community have to write down more PowerAutomate OData recipes :)

Comments

Comments are disabled.