Monday, 26 May 2014

Elevate your search experience: an Auto-complete search box

In this post I will explain how to elevate the search experience for your intranet or public faced website by replacing the Out of the box search control. 
The main component of the new autocomplete search  control are:  
  1. Search scope definition List: a custom list definition represents the search scope custom definition and a list instance of this list definition. 
  1. Empty element.xml file contains the necessary code to replace the  search delegate control replacement. 
  1. Custom user control holds the search autocomplete  logic , the control utilizes the Search RESTful APIs and execute an XHR calls to the search WCF service. 

The site owner will deploy the solution and will start populate the search definition by adding new items to the search definition list instance , the list definition will contain label (definition Title) and search definition. The search definition in fact is the RESTful call query string parameters. 
First let's create an empty farm solution 


Then add new List definition to the empty solution, name the list Search Definitions
Add new site column element to your empty solution to add your custom columns (Search definition and ScopeOrder)

Add the new field in the custom column elements.xml as below 
    Add the newly created fields to your Custom List definition as below:

after finalizing the custom list definition let's add the second main component which is the empty element file contains the search delegate control replacement.
Then add a new custom user control 

The user control consists of two components  dropdown list and a textbox  when the page loaded for the first time the control loads the predefined search scopes in a dropdown list and as the user start types a JavaScript code utilizing the client object model getting the scope definition and inject the user input string and execute an ajax call to the RESTful api at the end point
http://sitecollecitonUrl/_api/search/query

The control append the definition (which is the query string parameters ) to the above URL and render the search results
Example of the search definition


as you can see the first search definition search without limiting the results to any scope or content type, however the second one is limiting the results only to people using the SourceID parameter.
The third is trying to find the query only in the pages with type "NewsPage" which is a custom content type I've already created before

here is how the control looks like 


For complete source code please download the code at http://code.msdn.microsoft.com/Elevate-your-search-02fc9030




Friday, 16 May 2014

Promote your rating actions :Like a Page

In this post I will explain in details how to enable the like functionality for every page in your SharePoint site, and promote the like actions to the PromotedActions Delegate Control. 

In SharePoint 2013 a new DelegateControls are newly introduced like Sitesuitebar and promoted actions for a full list of the newly introduced place holders please check this blog post: 

First I will start with how to enable the ratings settings for your page library 
Navigate to your page library and via the ribbon click on the library settings , under the rating settings click on enable rating and choose likes. 




Now your site pages is available for rating , the out of the box rating actions can be found in the page library default view located in allitems.aspx page in our example we will try to provide an easier way to see the current pages like count and perform like and unlike actions from the page itself 

Create an empty SharePoint 2013 farm solution 


add new elements.xml file to it 

In the newly created elements.xml paste the below code which reference the LikePage user control
  
LikePage user control,will utilize the JavaScript object model and get he current page like count, also it detects whether the user already liked the page or not and it shows and hide the like actions based on that. 

This is how the control looks like  





 For complete source code please go to http://code.msdn.microsoft.com/Promote-your-rating-35fda760

Tuesday, 13 May 2014

A Content Enrichment Example: Search Refiner by WebApplication Display Name

In this post I will describe in details how to refine SharePoint search results by Web application Display name. In SharePoint as OOTB there is no managed property stores the web application name in SharePoint 2013.

Firstly, in order to filter the search results using web application display name we need to create new managed property of type text and make it refinable and active.


Secondly, to get the web application name and store it in the new refinable managed property we just created. We need to  utilize the Content Enrichment callout architecture and create new WCF service to process the search items and write the appropriate value to managed property “WebApplicationDisplayName

I followed this MSDN article with little changes, the input property in my example will be the siteID which represents the site collection Guid. In the ProcessItem method I create new instance of SPSite object using this unique Guid and then get the web application display name and dispose of the SPSite object.


After wards, I created a new IIS application and deployed the WCF service to it and run the below shell script to configure the search content enrichment service.



To run this script you need to stop all crawling if you have continuous crawling enabled you need to disable it 


Important: FileNotFound Exception

I notice that the service causes FileNotFound exception in the line responsible of the creation of the new SPSite instance. The reason was the default application pool identity need to be configured as the SharePoint application pool identity so it will be granted access to Content Database.
After a successful run I opened my search center, edit the refiner Webpart in the search result page and added the WebapplicaitonDisplayName to the top of my refiners.


and at the end the new search refiner will appear as below in the search page:


Full code is available in MSDN Code gallery here