September 25, 2022

Power Automate: How to get a list of document libraries? Or lists?

So let’s say you want to do an inventory of a SharePoint site and get the list of all document libraries. We can use an easy strategy to get them as well as lists, posts, comments, categories and more. All with the same strategy? Let’s take a look at how to do it.

The strategy and concepts

We’ll only use one action to do this, but it’s quite a versatile action. The “Send an HTTP request to SharePoint” action. I have a complete reference to it here , but the gist is that we can send dynamic URLs to SharePoint, which will return with raw data we can then parse. The data has a defined structure that is quite complex, but that’s to be expected since SharePoint itself provides us with a lot of customization and datatypes.

What’s important to know is that a document library in SharePoint is a list behind the scenes. I know it’s strange, but it’s essential to know so you can understand the strategy.

So if document libraries, posts, agendas, etc., are all lists, why do they look so different? It’s because SharePoint applies “templates” to them, as we’ll see in a bit below. Templates indicate that we should see a list of files (document libraries) instead of a list of agenda posts (posts on a page), for example. They are the same in the backend but end up presented differently.

So let’s use this templating mechanism to search for document libraries.

The request

Now that we know what we’re looking for let’s make the request. As mentioned, we will use the “Send an HTTP request to SharePoint” action. This action will allow us to build a URL, send it to SharePoint , and get the information back. So let’s see what we can do:

The URL has a structure defined by Microsoft, but we can understand what it’s being done. We’re calling the lists section and filtering by the “Base Template” (remember in the previous section that everything is a list but with a different template) with the ID 101.

Now we have the first challenge. If we check the dynamic values, using a Compose Action , for example, we’ll get:

There are no fields. This is because we’re sending a dynamic request to SharePoint , so Power Automate doesn’t know what to expect before it runs the Flow. We need to generate a definition that we can use, so we need to use the “Parse JSON” action that will get any JSON returned and create a schema that we can use. If you don’t know what a schema is, don’t worry, I have you covered in this article.

Usually, to generate a schema, we can generate it from a sample, but if you do, you may have errors while running the Flow. See the details here, but if you don’t want to worry about that I’ve done that work for you, so you can simply copy and paste the following schema:

{
    "type": "object",
    "properties": {
        "d": {
            "type": "object",
            "properties": {
                "results": {
                    "type": "array",
                    "items": {
                        "type": "object",
                        "properties": {
                            "__metadata": {
                                "type": "object",
                                "properties": {
                                    "id": {
                                        "type": "string"
                                    },
                                    "uri": {
                                        "type": "string"
                                    },
                                    "etag": {
                                        "type": "string"
                                    },
                                    "type": {
                                        "type": "string"
                                    }
                                }
                            },
                            "FirstUniqueAncestorSecurableObject": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "RoleAssignments": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "Author": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "ContentTypes": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "CreatablesInfo": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "DefaultView": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "DescriptionResource": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "EventReceivers": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "Fields": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "Forms": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "InformationRightsManagementSettings": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "Items": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "ParentWeb": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "RootFolder": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "Subscriptions": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "TitleResource": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "UserCustomActions": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "Views": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "WorkflowAssociations": {
                                "type": "object",
                                "properties": {
                                    "__deferred": {
                                        "type": "object",
                                        "properties": {
                                            "uri": {
                                                "type": "string"
                                            }
                                        }
                                    }
                                }
                            },
                            "AllowContentTypes": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "BaseTemplate": {
                                "type": [
                                    "integer",
                                    "null"
                                ]
                            },
                            "BaseType": {
                                "type": [
                                    "integer",
                                    "null"
                                ]
                            },
                            "ContentTypesEnabled": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "CrawlNonDefaultViews": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "Created": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            },
                            "CurrentChangeToken": {
                                "type": "object",
                                "properties": {
                                    "__metadata": {
                                        "type": "object",
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        }
                                    },
                                    "StringValue": {
                                        "type": "string"
                                    }
                                }
                            },
                            "DefaultContentApprovalWorkflowId": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            },
                            "DefaultItemOpenUseListSetting": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "Description": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            },
                            "Direction": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            },
                            "DisableCommenting": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "DisableGridEditing": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "DocumentTemplateUrl": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            },
                            "DraftVersionVisibility": {
                                "type": [
                                    "integer",
                                    "null"
                                ]
                            },
                            "EnableAttachments": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "EnableFolderCreation": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "EnableMinorVersions": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "EnableModeration": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "EnableRequestSignOff": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "EnableVersioning": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "EntityTypeName": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            },
                            "ExemptFromBlockDownloadOfNonViewableFiles": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "FileSavePostProcessingEnabled": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "ForceCheckout": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "HasExternalDataSource": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "Hidden": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "Id": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            },
                            "ImagePath": {
                                "type": "object",
                                "properties": {
                                    "__metadata": {
                                        "type": "object",
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        }
                                    },
                                    "DecodedUrl": {
                                        "type": "string"
                                    }
                                }
                            },
                            "ImageUrl": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            },
                            "DefaultSensitivityLabelForLibrary": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            },
                            "IrmEnabled": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "IrmExpire": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "IrmReject": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "IsApplicationList": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "IsCatalog": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "IsPrivate": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "ItemCount": {
                                "type": [
                                    "integer",
                                    "null"
                                ]
                            },
                            "LastItemDeletedDate": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            },
                            "LastItemModifiedDate": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            },
                            "LastItemUserModifiedDate": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            },
                            "ListExperienceOptions": {
                                "type": [
                                    "integer",
                                    "null"
                                ]
                            },
                            "ListItemEntityTypeFullName": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            },
                            "MajorVersionLimit": {
                                "type": [
                                    "integer",
                                    "null"
                                ]
                            },
                            "MajorWithMinorVersionsLimit": {
                                "type": [
                                    "integer",
                                    "null"
                                ]
                            },
                            "MultipleDataList": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "NoCrawl": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "ParentWebPath": {
                                "type": "object",
                                "properties": {
                                    "__metadata": {
                                        "type": "object",
                                        "properties": {
                                            "type": {
                                                "type": "string"
                                            }
                                        }
                                    },
                                    "DecodedUrl": {
                                        "type": "string"
                                    }
                                }
                            },
                            "ParentWebUrl": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            },
                            "ParserDisabled": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "ServerTemplateCanCreateFolders": {
                                "type": [
                                    "boolean",
                                    "null"
                                ]
                            },
                            "TemplateFeatureId": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            },
                            "Title": {
                                "type": [
                                    "string",
                                    "null"
                                ]
                            }
                        },
                        "required": [
                            "__metadata",
                            "FirstUniqueAncestorSecurableObject",
                            "RoleAssignments",
                            "Author",
                            "ContentTypes",
                            "CreatablesInfo",
                            "DefaultView",
                            "DescriptionResource",
                            "EventReceivers",
                            "Fields",
                            "Forms",
                            "InformationRightsManagementSettings",
                            "Items",
                            "ParentWeb",
                            "RootFolder",
                            "Subscriptions",
                            "TitleResource",
                            "UserCustomActions",
                            "Views",
                            "WorkflowAssociations",
                            "AllowContentTypes",
                            "BaseTemplate",
                            "BaseType",
                            "ContentTypesEnabled",
                            "CrawlNonDefaultViews",
                            "Created",
                            "CurrentChangeToken",
                            "DefaultContentApprovalWorkflowId",
                            "DefaultItemOpenUseListSetting",
                            "Description",
                            "Direction",
                            "DisableCommenting",
                            "DisableGridEditing",
                            "DocumentTemplateUrl",
                            "DraftVersionVisibility",
                            "EnableAttachments",
                            "EnableFolderCreation",
                            "EnableMinorVersions",
                            "EnableModeration",
                            "EnableRequestSignOff",
                            "EnableVersioning",
                            "EntityTypeName",
                            "ExemptFromBlockDownloadOfNonViewableFiles",
                            "FileSavePostProcessingEnabled",
                            "ForceCheckout",
                            "HasExternalDataSource",
                            "Hidden",
                            "Id",
                            "ImagePath",
                            "ImageUrl",
                            "DefaultSensitivityLabelForLibrary",
                            "IrmEnabled",
                            "IrmExpire",
                            "IrmReject",
                            "IsApplicationList",
                            "IsCatalog",
                            "IsPrivate",
                            "ItemCount",
                            "LastItemDeletedDate",
                            "LastItemModifiedDate",
                            "LastItemUserModifiedDate",
                            "ListExperienceOptions",
                            "ListItemEntityTypeFullName",
                            "MajorVersionLimit",
                            "MajorWithMinorVersionsLimit",
                            "MultipleDataList",
                            "NoCrawl",
                            "ParentWebPath",
                            "ParentWebUrl",
                            "ParserDisabled",
                            "ServerTemplateCanCreateFolders",
                            "TemplateFeatureId",
                            "Title"
                        ]
                    }
                }
            }
        }
    }
}

It’s a lot, but you don’t need to understand the structure since Microsoft defines it.

Getting the information

Now let’s look at the things we need. First, we need the list of items containing the “document libraries,” so we need the “results” field. You can search for it as follows:

Now that we have an “Apply to each” action, we can display all document libraries names using a “compose” action , for example, by searching for “EntityTypeName” as follows:

Here’s the result:

It’s important to know that we always get at least 4 document libraries since SharePoint creates some by default. Here’s an example of a SharePoint site (the “Test Document Library” was created by me).

SharePoint will always create and return:

  1. “Documents” as “Shared_x0020_Documents”
  2. “Form Templates” as “FormServerTemplates”
  3. “Site Assets” as “SiteAssets”
  4. “Style Library” as “Style_x0020_Library”

Notice as well that the “Site Pages” is a “Page library” and not a “Document Library,” so if you want to get it, you need to use the template identifier 119.

Want more types?

You can find the list of all types in Microsoft’s official reference , but here are the most common ones:

  1. “List” is 100
  2. “Page Library” is 119
  3. “Posts” is 301

There are a lot more, so if you’re curious or need another, please check Microsoft’s official documentation. I won’t replicate it since it’s quite an extensive list.

Final thoughts

It’s easy to get a list of document libraries or lists if we know where to look. SharePoint has quite a robust API that we can query and get information, but it’s not always simple to parse the results; I preferred to include the schema you can copy and use directly in your Flows.

Have a suggestion of your own or disagree with something I said? Leave a comment or interact on Twitter and check out other Power Automate-related articles here.

Photo by Volodymyr Hryshchenko on Unsplash

 

Manuel Gomes

I'm a previous Project Manager, and Developer now focused on delivering quality articles and projects here on the site. I've worked in the past for companies like Bayer, Sybase (now SAP), and Pestana Hotel Group and using that knowledge to help you automate your daily tasks

View all posts by Manuel Gomes →

Leave a Reply

Your email address will not be published.