Thursday, July 9, 2015

How to create a search page using SharePoint search functionality to search data in a particular list

These are the following steps i used to achieve the same in my project.

  1.  I created metadata properties in the search service application to match the fields in the discussion board list and mapped them to crawled properties. Here is a list of properties we are searching on: IssueNumber, Title, Author, SystemImpacted, Status, Priority.
    ---Central Admin
    àManage Service applicationsàSearch Service ApplicationàIn the quick launch section, Click ‘Metadata Properties’ under “Queries & Results”àClick “New Managed Property”àProvide ‘Property name’ and ‘Type’àAdd Mapping in “Mapping to Crawled Properties” sectionàSearch for the field name prefixed with “ows_” , select it and press OK.
  2. Then I wrote a quick user control form to construct the query string based on the values for these          properties entered by the user in the form. I will add the code to source control as soon as I get access  to source safe. The user control itself resides here (Server_name)                                                                    C:\inetpub\wwwroot\wss\VirtualDirectories\80\UserControls. I did not put it in the 14 hive because                  the smarts parts solution looks in this directory.
IssueTrackerListSearch.ascx.cs
using System;

using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IssueTrackerSearch
{
    public partial class IssueTrackerListSearch : System.Web.UI.UserControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            ClearControl(this);
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Response.Redirect(CreateSearchStringQuery());
        }

        private string CreateSearchStringQuery()
        {
            StringBuilder queryString = new StringBuilder();
            queryString.Append("<SiteUrl>/Search/Pages/IssueTrackerResults.aspx?k=");

            if (!String.IsNullOrEmpty(txtIssueNumber.Text))
            {
                queryString.AppendFormat(" {0}:{1}", "IssueNumber", txtIssueNumber.Text.Trim());
            }

            if (!String.IsNullOrEmpty(txtIssue.Text))
            {
                queryString.AppendFormat(" {0}:{1}", "Title", txtIssue.Text.Trim());
            }

            if(!String.IsNullOrEmpty(txtSubmittedBy.Text))
            {
                queryString.AppendFormat("{0}:{1}","Author",txtSubmittedBy.Text.Trim());
            }

            if (!String.IsNullOrEmpty(txtSystemImpacted.Text))
            {
                queryString.AppendFormat(" {0}:{1}", "SystemImpacted", txtSystemImpacted.Text.Trim());
            }

            if (!String.IsNullOrEmpty(ddlStatus.SelectedValue))
            {
                queryString.AppendFormat(" {0}:{1}", "Status", ddlStatus.SelectedValue.Trim());
            }

            if (!String.IsNullOrEmpty(ddlPriority.SelectedValue))
            {
                queryString.AppendFormat(" {0}:{1}", "Priority", ddlPriority.SelectedValue.Trim());
            }

            if (!String.IsNullOrEmpty(txtCustomerName.Text))
            {
                queryString.AppendFormat(" {0}:{1}", "CustomerName", txtCustomerName.Text.Trim());
            }
            if (!String.IsNullOrEmpty(txtItemNumber.Text))
            {
                queryString.AppendFormat(" {0}:{1}", "ItemNumber", txtItemNumber.Text.Trim());
            }
            if (!String.IsNullOrEmpty(txtItemDescription.Text))
            {
                queryString.AppendFormat(" {0}:{1}", "ItemDescription", txtItemDescription.Text.Trim());
            }
            if (!String.IsNullOrEmpty(txtOrderNumber.Text))
            {
                queryString.AppendFormat(" {0}:{1}", "OrderNumber", txtOrderNumber.Text.Trim());
            }
            if (!String.IsNullOrEmpty(ddlBusinessSegment.SelectedValue))
            {
                queryString.AppendFormat(" {0}:{1}", "BusinessSegment", ddlBusinessSegment.SelectedValue.Trim());
            }
            return queryString.ToString();

        }

        private void ClearControl(Control control)
        {
            TextBox textbox = control as TextBox;
            if (textbox != null)
                textbox.Text = string.Empty;

            DropDownList dropDownList = control as DropDownList;
            if (dropDownList != null)
                dropDownList.SelectedIndex = 0;
            foreach (Control childControl in control.Controls)
            {
                ClearControl(childControl);
            }
        }
    }
}


IssueTrackerListSearch.ascx

<%@ Control Language="C#" AutoEventWireup="true" 

CodeFile="IssueTrackerListSearch.ascx.cs" 
Inherits="IssueTrackerSearch.IssueTrackerListSearch" %>

<style type="text/css">
    .style1
    {
        height: 26px;
    }
    .style2
    {
        height: 30px;
    }
</style>

<h3>Search issues based on the following criteria:</h3>
<table>
<tbody>
<tr>
<td colspan="3"><strong>Business Issue</strong></td>
<td>
    <strong></strong>
    </td>
<td colspan="3">
    <strong>Customer Issue</strong></td>
</tr>

<tr>
<td>Issue Number</td>
<td></td>
<td>
    <asp:TextBox ID="txtIssueNumber" runat="server" Width="250px" Wrap="False"></asp:TextBox>
    </td>
<td>
    &nbsp;</td>
<td>
    Customer Name</td>
<td>
    &nbsp;</td>
<td>
    <asp:TextBox ID="txtCustomerName" runat="server" Width="250px"></asp:TextBox>
    </td>
</tr>

<tr>
<td>Issue</td>
<td></td>
<td>
    <asp:TextBox ID="txtIssue" runat="server" Width="250px" Wrap="False"></asp:TextBox>
    </td>
<td>
    &nbsp;</td>
<td class="style1">
    Item Number</td>
<td>
    &nbsp;</td>
<td>
    <asp:TextBox ID="txtItemNumber" runat="server" Width="250px"></asp:TextBox>
    </td>
</tr>

<tr>
<td>Submitted by</td>
<td></td>
<td>
    <asp:TextBox ID="txtSubmittedBy" runat="server" Width="250px" Wrap="False"></asp:TextBox>
    </td>
<td>
    &nbsp;</td>
<td class="style1">
    Item Description</td>
<td>
    &nbsp;</td>
<td>
    <asp:TextBox ID="txtItemDescription" runat="server" Width="250px"></asp:TextBox>
    </td>
</tr>

<tr>
<td class="style1">System Impacted</td>
<td class="style1"></td>
<td class="style1">
    <asp:TextBox ID="txtSystemImpacted" runat="server" Width="250px" Wrap="False"></asp:TextBox>
    </td>
<td class="style1">
    </td>
<td>
    Order Number</td>
<td class="style1">
    </td>
<td class="style1">
    <asp:TextBox ID="txtOrderNumber" runat="server" Width="250px"></asp:TextBox>
    </td>
</tr>
<tr>
<td>Status</td>
<td></td>
<td>
    <asp:DropDownList ID="ddlStatus" runat="server" Width="253px" Height="22px">
        <asp:ListItem></asp:ListItem>
        <asp:ListItem>Submitted</asp:ListItem>
        <asp:ListItem>Open</asp:ListItem>
        <asp:ListItem>Close</asp:ListItem>
        <asp:ListItem>Reassign</asp:ListItem>
    </asp:DropDownList>
    </td>
<td>
    &nbsp;</td>
    <td>Business Segment</td>
<td>
    &nbsp;</td>
<td>
    <asp:DropDownList ID="ddlBusinessSegment" runat="server" Width="253px" Height="22px">
        <asp:ListItem></asp:ListItem>
        <asp:ListItem>Dated</asp:ListItem>
        <asp:ListItem>Office</asp:ListItem>
        <asp:ListItem>School</asp:ListItem>
        <asp:ListItem>Multiple</asp:ListItem>
    </asp:DropDownList>
    </td>
</tr>
<tr>
<td>Priority</td>
<td></td>
<td>
    <asp:DropDownList ID="ddlPriority" runat="server" Width="253px" Height="22px">
        <asp:ListItem></asp:ListItem>
        <asp:ListItem>Low</asp:ListItem>
        <asp:ListItem>Medium</asp:ListItem>
        <asp:ListItem>High</asp:ListItem>
<asp:ListItem>Urgent</asp:ListItem>
    </asp:DropDownList>
    </td>
<td>
    &nbsp;</td>
    <td>&nbsp;</td>
<td>
    &nbsp;</td>
<td>
    &nbsp;</td>
</tr>
<tr><td></td><td></td><td></td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
<tr><td class="style2"><asp:Button ID="btnSubmit" runat="server" Text="Submit" 
        onclick="btnSubmit_Click" /></td>
<td class="style2"></td>
<td class="style2"><asp:Button ID="btnClear" runat="server" Text="Clear" 
        onclick="btnClear_Click" /></td>
<td class="style2"></td>
<td class="style2"></td>
<td class="style2"></td>
<td class="style2"></td>
</tr>
</tbody>
</table>


3.  I attempted to create a user control host web part but was experiencing .Net version issues, so I decided to use SmartPart for SharePoint 2010 CodePlex solution to host the user control. This was much faster and worked great.









Click "open the tool pane". Then select your SmartPart. In this case its "usercontrols_issuetrackerlistssearch.ascx"


Then Check-in and Publish your page.


4.  I created a search results page and filtered the core results web part to only show results from the issue list. Configured the search box on the results page to go against the issue list as well.

                a) Create a FAST Search center site in the top level site.


                b) Create a new page under Pages section. I've named it as "IssueTrackerResults" in my case.

        
c) Edit the page and the edit “Search Core Results” web part.

                d) In ‘Results Query Options’ provide the list url in “Append Text To Query” section. Then Press OK. Check-In and Publish the page.

No comments:

Post a Comment