Thursday, January 24, 2013

SHAREPOINT CUSTOM SEARCH WITH KENDOUI


As discussed in the previous post I had planned to incorporate Kendo UI with the SharePoint 2013 Custom Search Results page, I am going to develop a Web Part which will use the Kendo Grid.

For those who are still wondering what this kendo UI is all about.. It really is a cool; HTML5, Javascript etc based Framework which provides several widgets for more details visit <a href="http://www.kendoui.com/">http://www.kendoui.com/</a>

INTEGRATING KENDO UI
In the previous section we made a user control now user control has both .ascx page and the code behind file. Add the reference to the kendo UI style sheets but before doing that we need to place the files in the 15hive in the Layouts folder so that these style sheets are available for our user control. Place them in the STYLES folder in the Layouts folder.


Add Script tag in the file to use kendoUI:
  <link href="/_layouts/15/kendoFiles/Content/kendo/2012.2.710/kendo.common.min.css" rel="stylesheet" type="text/css"></link>
    <link href="/_layouts/15/kendoFiles/Content/kendo/2012.2.710/kendo.blueopal.min.css" rel="stylesheet" type="text/css"></link>
  <script src="/_layouts/15/kendoFiles/Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
    <script src="/_layouts/15/kendoFiles/Scripts/jquery-ui-1.8.11.js" type="text/javascript"></script>
<script src="/_layouts/15/kendoFiles/Scripts/kendo/2012.2.710/kendo.web.min.js" type="text/javascript"></script>
    <script src="/_layouts/15/kendoFiles/Scripts/kendoui-Custom.js"></script>
   
   <script type="text/javascript">
       function getParameterByName(name) {
           var match = RegExp('[?&]' + name + '=([^&]*)')
                           .exec(window.location.search);
           return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
       }
       var queryTerms = getParameterByName('k');//for the case if there is query string search request
       function PerformSearch() {
           queryTerms = document.getElementById('query').value;
           Results.init($('#resultsDiv'));
           Results.load();
         
       }      
       Results = {
           element: '', url: '',
           init: function (element) {
               Results.element = element;
               Results.url = _spPageContextInfo.webAbsoluteUrl + "/_api/search/query?querytext='" + queryTerms + "'";
           },
           load: function () {
               $.ajax({
                   url: Results.url,
                   method: "GET",
                   headers: { "Accept": "application/json; odata=verbose" },
                   success: Results.onSuccess, error: Results.onError
               });
           },     onSuccess: function (data) {
               var results = data.d.query.PrimaryQueryResult.RelevantResults.Table.Rows.results;
               var dSource = new Array();            
               var html = "<table>";
               for (var i = 0; i < results.length; i++) {
                   dSource[i] = new Object();
                   dSource[i].Title = results[i].Cells.results[3].Value;
                   dSource[i].Path = results[i].Cells.results[6].Value;
               }
               $("#products").kendoGrid({
                   dataSource: dSource                
               });

           },
           onError: function (err) { alert(JSON.stringify(err)); }
       }

   </script>

In this JavaScript snippet we are creating a KendoUI Grid in the div that we have allocated for it in the .ascx file(as shown below). We will use the OData Services exposed by the SharePoint 2013 to call the search service and use the data returned by the service to create a data source for our KendoGrid control. And this is how we will show the results.

Now the Kendo UI and the Custom Search is all setup, enter any search query on our visual webpart and you will get the result, currently it is showing only two search result fields which we will alter using the column model of KendoGrid.






With the help of the latest REST based services provided by the SharePoint using kendo Grid or any other ajax supported grid has become real easy.

I will share further posts related to SharePoint 2013 development or ASP.Net which could be of any help to the developers who are seeking help in this regard.

Wednesday, January 16, 2013

SharePoint 2013 Custom Search Result using Visual Studio 2012


We will have our own custom Search results page which will consist of a visual web part containing a User Control.

A.1.        Search Mechanism

Once the SharePoint Search Service is running and has something to crawl, the user will be able to search through the content of the SharePoint website, lists, document libraries and pages. Our search Web part will be shown instead of the OOTB search bar of SharePoint. Upon clicking the search button user will be redirected to the custom search results page. Following query string values are also sent while redirecting to this page which we will use these query strings to call the search on our custom search page.

A.2.        Custom Search Page

This page will have a Visual Web Part containing a User Control containing the Grid view to show the results of the search. The User control will use the REST based service exposed by the SharePoint. The details of the API are as follows:

A.2.a.     Location of the Search Rest service

                The Search REST service is located at the following URI: http://host/site/_api/search


A.3.        Visual Web Part Implementation

 

Go to the file-> Add new project and then Select Empty SharePoint Project. A new solution will be created now right click the project file and add new Visual Web Part to the project.

Your project will look like just as shown in the picture above, now add the following code to the .ascx file in your project.



<%@ Assembly Name="$SharePoint.Project.AssemblyFullNamequot; %>


<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>


<%@ Import Namespace="Microsoft.SharePoint" %>


<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1.ascx.cs" Inherits="searchWebPart.VisualWebPart1.VisualWebPart1" %>


Custom Result Page


Search: <input id="query" type="text" /> <input id="searchBtn" type="button" onclick="PerformSearch()" value="button" />


<%-- <asp:Button ID="searchBtn" runat="server" Text="Button" OnClientClick="PerformSearch()" CommandName="test" />--%>


<div id="resultsDiv" style="display:none;"></div>


<div id="products"></div>



Once you have setup the files as mentioned above, deploy the project using visual studio 2012 and it will place the User control in the Visual Web Part in the ControlTemplates folder of the 15 hive. So your web part will appear in the Custom Web Parts category and can be used on any page.

Once the Web Part has been deployed successfully, create a new site page in the site library and add this WebPart to the page. Stay tuned for the next version of this post where I shall show how to use Kendo UI to display the Search Results.

Tuesday, January 15, 2013

Welcome Developers

Impressed by the numerous blogs present on the web to help us (the developers) on day to day issues that we face while developing various applications, I have decided to create a blog. So far I have decided to start helping people and for that reason I am going to kick off with this Blog.