Filtering Files and Folders in SharePoint with Power Automate

When working with Power Automate and SharePoint, one common task is retrieving items from a document library. However, document libraries contain both files and folders, and often, you may need to work exclusively with files or folders. Understanding how to filter these items efficiently can streamline your flows and make your automation processes more effective.

Understanding the isFolder Property

SharePoint items in a document library can be differentiated into files and folders through the isFolder property. This property is boolean, where True indicates a folder, and False indicates a file. However, when using Power Automate's "Get files (properties only)" action to retrieve items from SharePoint, you cannot directly use the {IsFolder} property in the Filter Query option due to syntax limitations.

The following is the result of Get files (properties only)

[
  {
    "@odata.etag": "\"1\"",
    "ItemInternalId": "2",
    "ID": 2,
    "Title": "B60885",
    "Modified": "2024-04-03T21:29:01Z",
    "Editor": {
      "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
      "Claims": "i:0#.f|membership|crmdev@whsc.on.ca",
      "DisplayName": "CRM Developer",
      "Email": "crmdev@whsc.on.ca",
      "Picture": "https://workershealthandsafety.sharepoint.com/sites/Dynamics365/_layouts/15/UserPhoto.aspx?Size=L&AccountName=crmdev@whsc.on.ca",
      "Department": "",
      "JobTitle": ""
    },
    "Editor#Claims": "i:0#.f|membership|crmdev@whsc.on.ca",
    "Created": "2024-04-03T21:29:01Z",
    "Author": {
      "@odata.type": "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser",
      "Claims": "i:0#.f|membership|crmdev@whsc.on.ca",
      "DisplayName": "CRM Developer",
      "Email": "crmdev@whsc.on.ca",
      "Picture": "https://workershealthandsafety.sharepoint.com/sites/Dynamics365/_layouts/15/UserPhoto.aspx?Size=L&AccountName=crmdev@whsc.on.ca",
      "Department": null,
      "JobTitle": null
    },
    "Author#Claims": "i:0#.f|membership|crmdev@whsc.on.ca",
    "OData__DisplayName": "",
    "{Identifier}": "ClassDocuments%252fB60885",
    "{IsFolder}": true,
    "{Thumbnail}": {
      "Large": null,
      "Medium": null,
      "Small": null
    },
    "{Link}": "https://workershealthandsafety.sharepoint.com/sites/Dynamics365/ClassDocuments/B60885",
    "{Name}": "B60885",
    "{FilenameWithExtension}": "B60885",
    "{Path}": "ClassDocuments/",
    "{FullPath}": "ClassDocuments/B60885",
    "{IsCheckedOut}": false,
    "{VersionNumber}": "1.0"
  },

...

]

Utilizing the FSObjType Property

Fortunately, there's a workaround that involves using the FSObjType property. FSObjType is a property that functions similarly to {IsFolder}, but it's compatible with the Filter Query syntax in Power Automate. The FSObjType property utilizes numerical values to differentiate between files and folders:

  • FSObjType eq 0: This filter query returns only files, excluding any folders from the results.
  • FSObjType eq 1: Conversely, this query filters out files and returns only folders.




This distinction is crucial for creating more focused and efficient workflows, especially when you need to manipulate only files or folders within a SharePoint document library.

Practical Application

When setting up the "Get files (properties only)" action in Power Automate, you can specify your desired filter in the Filter Query field to either work with files or folders. Here are the steps for filtering to retrieve only files:

  1. Add the "Get files (properties only)" action to your flow.
  2. In the action settings, locate the Filter Query option.
  3. Enter FSObjType eq 0 to ensure your query returns only files.
  4. Proceed with your flow's logic, now assured that only files will be processed.

Conclusion

Leveraging the FSObjType property in Power Automate to filter items in SharePoint document libraries is a powerful technique. It allows you to create more targeted and efficient flows by ensuring that you only work with the specific type of items you need, be they files or folders. Whether you're automating document processing, organizing files, or managing library structures, understanding and applying this filtering method can significantly enhance your workflow's effectiveness and efficiency.


Reference: 1


No comments:

Post a Comment