Category Archives: Office 365 – Groups

Searching documents across multiple Office 365 groups with REST API

If you work on Office 365, you probably know what are groups, one of the latest features launched by Microsoft on its collaborative platform.

For those who don’t know what’s it, it’s a new way and mostly a simplest way, to create sites that allow users to collaborate with a minimal set of collaborative features such as storing documents, creating conversations, taking notes in a notebook and sharing events on a calendar.

Below you can see an example of a group, especially the document storage.

Office365 - Groups

For now, if you want to find a document in a given group or a set of documents across all groups, there’s no possibility to search what you’re looking for.

Indeed, when you search for documents using the search area available on the top-left (cf. screenshot), you will be redirected to the global search center of your company available in Office 365.

Fortunately, we can use the search engine with REST API to implement that missing feature.

As a reminder (we described it many times in articles on this blog), to submit a search query in Office 365 using REST API, you can send a POST request to the following URL with a well-formatted HTTP body.

https://tenant.sharepoint.com/_api/Search/PostQuery

When you want to retrieve only documents stored in SharePoint libraries, you can filter items based on the ContentClass property which exists for each items (pages, documents, tasks, events…) that were created in your sites.

In the same way, if you want to search only in a particular kind of site (e.g. Office 365 groups in our case), you can filter results based on the SiteTemplate property.

So what looks like the request to send to the REST API ?

{
  request = {
    Querytext = "ContentClass:STS_ListItem_DocumentLibrary AND SiteTemplate:GROUP",
    RowLimit = 100,
    SelectProperties = (
            Title,
            DefaultEncodingURL,
            SiteTitle,
            SPSiteURL
        ),
        StartRow = 0,
        Timeout = 30000,
        TrimDuplicates = True
    }
}

In the request above, we return the first 100 documents (excluding duplicates) stored in all Office 365 groups and we also retrieve some additional information such as the title of the documents (Title), the URL of the file (DefaultEncodingURL), the name of the group where it’s stored (SiteTitle) and the URL of the group (SPSiteURL).

If you want to be more precise on your search (e.g. search for documents that have a title which start with the word “Program“), then you can add new filters to your query such as the example below.

Title:Program* AND ContentClass:STS_ListItem_DocumentLibrary AND SiteTemplate:GROUP

As we have seen in this article, it’s pretty simple to search documents across all Office 365 groups and it’s just up to you to integrate and to use REST API in your different applications (mobile, Office add-ins, WebParts…).

Advertisements