Using SharePoint REST API with CAML to return Search Results in a Flow

Flow allows you to easily manipulate your SharePoint content.  However, as I discussed in my previous article Manipulating SharePoint content using Flow, there are times where the basic actions will not suffice.

The Send an HTTP request to SharePoint action enables you to design your specific queries to perform various content management actions.  Some common scenarios include creating, modifying, and viewing items and documents in lists and libraries.  In some instances, however, you may need to leverage the SharePoint REST API to perform a search.  One way to structure the search is by using a Collaborative Application Markup Language (CAML) query. Here's how you do it...

Perform the Search

We start by using the Send an HTTP request to SharePoint action.  There are a couple of elements to pay particular attention to:

  • Site address - the site where your list or library resides
  • Method - make sure it's set to POST
  • Uri - You need to change the items in red for your specific needs
    _api/web/Lists/GetByTitle('[Your list name]')/GetItems(query=@v1)?@v1={"ViewXml":"[Your CAML query]"}

    for example, in my case, I'm searching for items that contain my search string (search) in either the Title and Description fields from a list called FAQ.  @{triggerBody()?['search']} is the search term being passed into the Flow.

    _api/web/Lists/GetByTitle('FAQ')/GetItems(query=@v1)?@v1={"ViewXml":"<View><Query><Where><Or><Contains><FieldRef Name='Title'/><Value Type='Text'>@{triggerBody()?['search']}</Value></Contains><Contains><FieldRef Name='Description'/><Value Type='Text'>@{triggerBody()?['search']}</Value></Contains></Or></Where><RowLimit>500</RowLimit></Query></View>"}

Only keep what you need

The query will return a lot if information in JSON format, some of which you may not even need.  In my example, I'm only interested in two fields - Title and Description.  So, you can use the Select action to only keep those fields you're interested in.  In the Select action, make sure to reference the results element within the JSON structure.  To get to the results themselves, you need to reference the results object, body('Send_an_HTTP_request_to_SharePoint')['d'['results'].  

Once you have the fields captured in the Select action, you can use them in your Flow.

No comments:

Post a Comment

Employee Check-in Simplified using Power Automate

Company employees who move between offices are often faced with a tedious process of having to check-in at the security desk to get a tempor...