Locking Down Search for a Site (Part 2 – SP2013, SP2016, SP2019)

If you want to achieve the functionality I outlined in “Part 1” using SharePoint 2013 UI, or higher (SP 2016, 2019), you are going to need to go through a different process, as Microsoft deprecated Search Scopes in the 2013 UI.  Fortunately, the work around is actually a bit less work, as you only need to modify your search results page.

The first step is to create a new results page.  This can be done by navigating to the “Pages” area of your search site. (ie. click the gear icon, and then “Site Contents”).  From there, click the Documents drop down, and choose a new “Welcome Page”.

It should default to a “Search Results” page template, but if it does not, be sure you choose that type.  Give it a title relating to the search you want to have the page limited to.

Once you have the page created, click “Edit” from the toolbar, and scroll down to the search results area.  Choose “Edit Web Part” from the Search Results web part.

From there, click the “Change Query” button from the web part editor’s “Properties for Search Results” section.

Here is where you are going to restrict the results for the page. Under the “Basics” tab, you will have several property filters to choose from.  If you do not see the one you want, you can load all the properties by clicking the “Show all managed properties” option at the bottom.  For this example, I want to restrict the results to a particular site, so I chose the “Site” property, and then filled in the URL for the site.  You can use the “Test Query” button to confirm there are results coming through, provided your site already has some content.

Click OK to save the updated settings.

 

Next you will likely want to do some tidying up, and possibly even include a new tab for your new search results page.

Choose “Edit Web Part” for the Search Box further up at the top of the page, and click the checkbox for “Use this site’s Search Settings”.

Also click the link where it is hyperlinked to “Search Settings”.  This is where you can add a new tab to the search navigation for the page.  Give it a title, and then put in the link to the custom results page you just created.   Click OK for the Search Settings, and also OK for the Search Box settings.

Save and publish the page.  Run a few queries to make sure you didn’t miss anything.

That’s it! You are done.

You can also reference search boxes on your other sites to use this custom results page.  Just point them there.

 

Locking Down Search for a Site (Part 1 – SP2010, SP2013*)

One of the SharePoint features that does not always work as expected is the “Search This Site” component.  If one is doing a search using this option, the results page can sometimes get confusing very quickly for unexperienced SharePoint users.  Results are typically presented for the site in question, however the user is then presented with too many options for making ongoing searches.  What is worse, is that if the user alters their search, they may lose the context of searching just that one site, and in turn get results for the entire enterprise instead.

Fortunately, SharePoint has some quite simple options for remedying this.  For SharePoint 2010 & 2013 (in the 2010 UI), there is the Search Scopes option, and in 2013 (UI) onwards there is an option for the customization of the results page, which I will be documenting in a follow-up post to this one.

The first step is it to set up a new scope for the site you wish to limit the results to.  To do so, navigate to the root of the site collection, and click “Site Actions”, then “Site Settings”.  Find the link labeled “Search Scopes”.

Once on the Scopes page, click the “New Scope” link.

Give the new scope a title.  Preferably something short, and with no spaces.  The reason for this is you will need to use this exact title later on.  Unless you need the other options changed for some reason, you can feel free to leave them as is.  The scope will still be available for use, even if the check boxes are not selected.

The next step is to set up some rules for the scope you just created, which will tell the scope which content it should hold.  Look through the scopes on the View Scopes page, until you locate the scope you just created.  Click the “Add Rules” link (you can also click “Edit Properties and Rules” from the drop down if you are going back to change or add more rules).

From here, you will want to set the scope to be just to the site.  Set the URL to the domain, and click OK.

From here, you will want to set up the search results page that you want to use for the site where you will have the limited results.  You have a couple of options here for placement.  You can either add the results page to the root search site, or create a new Search Center and add it as a subsite under the site you are working on.  The reason you may want to set up a new Search Center is for when you want to maintain the same branding presence as the site you are providing results for.

You will want to create a new “results page” in the “Pages” area of the search site. For example, you could add the page to https://company.com/search/Pages/Forms/AllItems.aspx  (Click the “Documents” tab, Click the New Documents drop down, and select “Welcome Page”, then “Search Results Page”)

If using a new search center as a subsite (Site Actions -> Site Settings -> Sites and Workspaces -> Create -> Under the Enterprise tab choose Enterprise Search Center), you could just modify the existing results page: https://company.com/SpringDemo/search/Pages/results.aspx

Once on the results page, click the “Page” tab, and then “Edit”


Now you are ready to plug in the custom scope you created in the prior step.  Scroll down to the Search Core Results web part, and click the “Edit Web Part” option from the drop down.

In the settings area, expand the “Location Properties”, and in the textbox labeled “Scope”, add in the scope name of the scope you created earlier.

Click OK, and then check in and publish the updated results page.

The last step is to wire up a search box to the updated results page.

For this example, we will be wiring up a search box on the home page of the site.

The first thing you may want to do is hide the search bar in the masterpage.

The reason for this is that this search bar is not directly customizable.  The only way around that is to use some custom JavaScript to override the default search destination.  If you are using the default SharePoint masterpage, you can hide it by using a bit of custom CSS ( #s4-searcharea {display:none} ).  You can put that in a .css file, and if you have publishing turned on, add it as a css file to be used for the site.

After completing that optional step, you are finally ready to complete the final step.  Add a “Search Box” web part to where you would like it placed. Then choose “Edit Web Part”

If you want to keep searches limited to just this site, first hide the scopes drop down (default to target results page).

Under the Miscellaneous section, add put in the link to the results page you modified earlier (ie. /SpringDemo/search/Pages/results.aspx)

Last you will want to change the chrome type and title so that people will know what it is for.

Click OK, and stop editing (publish if necessary) the page.  You should be ready to test/go now.

 

As a last optional step, if you want to put a link on the search results page so that the user can get easily back to the home page, you do that by adding a content editor web part to add a link.

Remove a SharePoint Search Server from the Search Service Application

Occasionally there will be instances where you need to remove a server from the Search Service Application, if you have multiple search servers set up in the farm. To do so, especially if the server is unavailable, or offline, follow the steps below:

$ssa = Get-SPEnterpriseSearchServiceApplication
$active = Get-SPEnterpriseSearchTopology -SearchApplication $ssa -Active
$clone = New-SPEnterpriseSearchTopology -SearchApplication $ssa -Clone -SearchTopology $active
$clone.GetComponents()

//use the output from above to grab the names of each component from the server you wish to remove.  remove each component as necessary.
Remove-SPEnterpriseSearchComponent -SearchTopology $clone -Identity ($clone.GetComponents() | Where-Object {$_.Name -eq "ContentProcessingComponent3"}) -Confirm:$false

//check the new components topology
$clone.GetComponents()

//set the topology
Set-SPEnterpriseSearchTopology -Identity $clone

SharePoint Search Box Not Firing When Enter Pressed in IE

Ran into an issue in SP2013/SP2010 where the Enter/Return key would default to other page elements instead of taking the keywords and conducting the search if the Enter key was pressed. So for example, if there was a button on the page, it would fire that element, instead of the search box. Here is a simple bit of jQuery which will ensure that the user’s search gets completed:

//fix for IE 11 Enter/Return key not firing for Search box
jQuery("input[id^=ctl00_PlaceHolderSearchArea]").keypress(function(event){
  if(event.which == 13) 
  {
    window.location = jQuery("a[id^=ctl00_PlaceHolderSearchArea]").attr('href');
   }		        
});

Filtering SharePoint Search by Calendar Dates

SharePoint, out of the box does not support filtering calendar dates.  The reason for this is that to search, they are set up as strings, thus running a comparision is not achievable.  Example: Let’s say you are using the Search API, and only want to show events that have not taken place yet.  There is nothing within search that will allow you to do this, without doing some tweaks.

In order to accomplish this, you will first need to set up a custom mapping for search to pick up the calendar date as a DateTime datatype.

1. In Central Admin, click on the Search Service Application

2. Click on Search Schema

3. Click “New Managed Property” and fill it out like the following.  Be sure to set the type as “Date and Time”. Then click OK.

Be sure to map the “ows_q_Date_EndDate” and “ows_q_Date__EndDate” properties.

4. Once the property has been mapped, you will need to run a Full Crawl in order for search to pick up the new property.

5. Then after the crawl has finished, and the new mapped datatype is available, you can run a query for new training events that have not yet taken place:

https://sharepoint.company.com/_api/search/query?querytext='*training* -path:"https://sharepoint.company.com/management/"'&refinementfilters='and(ContentTypeID:0x0102*,CalendarEndDate:range([Today],max,from="ge"))'&clienttype='AllResultsQuery'&rowlimit=10&sortlist='CalendarEndDate:descending'

A few things to note in the query:

  • ContentTypeID:0x0102* is the SharePoint content type for calendar events
  • [Today] will need to be replaced in your code with today’s actual date and time in string format, so: ‘2017-01-01’ (or DateTime.Now.ToString(“yyyy-MM-dd”))
  • This query also includes an optional exclusion (-path:”https://sharepoint.company.com/management/”), if you do not want a particular calendar or site included in the results.

This will return you calendar events matching the keyword, that have also not taken place yet.