Wednesday, February 7, 2018

Display all documents a user can access in SharePoint

Summary


This post walks you through the steps of creating a page that will display every shared document, folder and site that's been shared with a user, avoiding the reliance on email links when items are shared.  The end result looks something like below.



Solution


Search can bring back everything a user has access to.  So by modifying the Search Content Web Part, I brought back all files, folders and sites.  I updated the Display Templates to show the sites that the documents are in, and added a hover panel that has a document preview and shows the path where the document lives.

I added a Search Refinement Web Part, that displays all of the sites along with the item count under each site.  This way a user can easily drill down into different areas.

And lastly I added a search box so that users can perform a search on the results to aid in finding specific items.

Setting up the  web parts


Search Content Web Part


First I created a page and added a Search Content Web Part.  In order to bring back all files/folders/sites, so I changed the Query text to be:

Path:"https://vivity.sharepoint.com" -Path:"https://vivity.sharepoint.com/SiteAssets/" ContentTypeId:0x0120* -ProgID:OneNote.Notebook    -ContentTypeId:0x012002* -ContentTypeId:0x012000C0* -ContentTypeId:0x0120001928* ContentTypeId:0x010100* {QueryString.searchTerm}

Which searches my site, leaves out SiteAssets and OneNote Notebooks, and brings back derivatives of the contenttypes I want (files, folders and sites).  I am also searching for the querystring parameter "searchTerm", so a wildcard is thrown in the url as a default, which is replaced if a user submits a search.


I also mapped the ows_SiteName crawled property to RefineableString02 and am sorting by that.

Next, I wanted to update my Display Templates (/_catalogs/masterpage/Display Templates/Content Web Parts).  I started out with the Control_ListWithPaging.html and the Item_TwoLines.html and modified those.  

Control_ListWithPaging became Control_GroupedSites_WithHover, and I added some paging logic from here.  I also added a javascript file that sets some variables I'll be using, and adds some styling to my display template.

Item_TwoLines became Item_GroupedSites_WithHover, and my goal here was to display the documents, and added some code to make the Site Title be the header for every set of documents.

I also wanted more contextual information about each document, so I added a hover panel, which I updated to display the folder path and a document preview.

To show the hover panel, I started with the /_catalogs/masterpage/Display Templates/Item_Default_HoverPanel.html file, and merged it with the Item_Word_HoverPanel.html file, making some slight modifications to make it more generic and show the folder path so we have some context of where the document lives.  I named it Item_Default_HoverPanel_SharedDocuments.html.

Search Refiner Web Part


Additionally, users usually have a sense of where a document lives that they're searching for, so I added a Search Refinement web part that lists all of the sites that are returned an displays them so you can drill down easily.



and when you select a site, it will only bring back those items:


I updated the refiner templates so that they display in a table format, and you can find these updates in  /_catalogs/masterpage/Display Templates/Filters/Control_Refinement_SharedDocuments.html and Control_Refinement_SharedDocuments.html.

Search


Lastly I wanted a way for users to be able to search at any point, so I added a Content Query Web Part with some html/javascript that simply adds a querystring parameter and resubmits the page, and the the Content Search Web Part uses as a search term.

Code


All the code, templates and web parts are up on my github:  https://github.com/sregan1/SharePoint-Office365