SharePoint Search – Querying Private Groups and Documents Within Private Groups

Since private Groups were provided in Office 365, we have had a need to surface private groups and private group content in SharePoint Search. A while back many quickly found that the “WebTemplate” for Groups was simply “Group“, thus we could add the following to our search query to limit results to Groups. “WebTemplate:Group”, such as /search.aspx?k=WebTemplate:Group.

The major issue with this search query was that it only returned public Groups, as well as documents only within public Groups. No worries, User Voice to the rescue. A request, “Allow members to search private groups” was submitted and even marked as “Boom! It’s Done” April 27, 2017. As many noticed, this does not necessarily appear to be true.

I have a test tenant in Office 365 that has a few test public groups and a few test private groups. Each of these groups have unique content. When I create a simple SharePoint Site with a page that includes a search results webpart, no matter the query I create, I am only able to return public group data.

SharePoint Rest API to the rescue

Mikael Svenson figured out a while ago that we can use the SharePoint Rest API to query private Group content. I have found his finding to be very useful, so let me continue along his thread and provide a few more endpoints and suggestions when replacing OOTB SharePoint Search Results Web Part with a JavaScript based solution that uses the SharePoint Rest API to retrieve Groups as well as private Groups, and/or documents within both public and private Groups.

SharePoint Search Rest API and Private Group Data

As Mikael points out, first and foremost, we need to add &Properties=’EnableDynamicGroups:true’ to the end of our query.

Search for Groups, both public AND private, that contain a keyword value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
var query = "*"; //replace with your keyword string, possibly from query string or text box
var searchEndpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + query + " WebTemplate%3AGroup'&Properties='EnableDynamicGroups:true'";

$.ajax
({
   type: "GET",
   url: searchEndpoint,
   headers: {
      "Accept": "application/json;odata=verbose"
   }
}).done(function (response) {
   console.log("Group list returned");
   console.log(response);
   
   //add your custom code here to work with response.
}).fail(function () {
   console.log("failed to get group search results");
});

I readily admit that the results found in my “response” variable above is rather entertaining to work with, but you will find important result data in the json returned, such as:

d.query.PrimaryQueryResult.RelevantResults.RowCount

and

d.query.PrimaryQueryResult.RelevantResults.Table

Search for general results in Groups, both public AND private, that contain a keyword value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var query = "*"; //replace with your keyword string, possibly from query string or text box
var searchEndpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + query + "'&Properties='EnableDynamicGroups:true'";
$.ajax
({
type: "GET",
   url: searchEndpoint,
   headers: {
      "Accept": "application/json;odata=verbose"
   }
}).done(function (response) {
   console.log("Group list returned");
   console.log(response);
   //add your custom code here to work with response.

}).fail(function () {
   console.log("failed to get group search results");
});

Here, all I did was remove the Managed Property requirement of WebTemplate in the query string, but left &Properties=’EnableDynamicGroups:true’ to continue looking for results in Private Groups as well.

Paging, Sorting, Selecting Properties

Of course since these examples are still using the general SharePoint Search Rest API, we can use additional query parameters such as “startrow” and “rowlimit” to page through our results. Further we can specify specific Managed Properties to return using “selectproperties“. Sorting? No problem, “sortlist“.

Examples:

startrow=10 (0 based)
rowlimit=10 (default is 10)
selectproperties=’Path,URL,Author’
sortlist=’Author:descending’

More complex SharePoint Search Rest API Endpoint

1
2
var query = "*"; //replace with your keyword string, possibly from query string or text box
var searchEndpoint = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + query + "startrow=10&rowlimit=10&selectproperties='Path,URL,Author'&sortlist='Author:descending'&Properties='EnableDynamicGroups:true'";

Thus, return all items in the index based on the keyword value in “query”, starting at the 11th result, show get up to 10 results, select only the Managed properties, Path, Url and Author, sort by Author, in a Descending order, and finally search in private groups as well.

Conclusion

Until SharePoint Online’s default Search Results Web part is updated to also search private groups as well, a custom solution using JavaScript is possible if you work with the SharePoint Search Rest API.

Comments

  1. Thanks, this is helpful, although I’m not exactly sure how to make the javascript search box. If I could just pass the query text from the classic SharePoint page to the modern search page (replacing k={querystring} with q={querystring}, I’d be all set.

    • Too true.

      The box itself, you could create am input type=”text” field, with a input type=”submit” button. Then bind onClick on the submit button to trigger your javascript. The script would only need to take the value of the input text box, package up the request and send off to SharePoint.

Speak Your Mind

*


4 + seven =