Sunday 7 July 2013

Sharepoint list search using Visualstudio

Reference URL: http://jasear.wordpress.com/2012/04/19/sharepoint-2010-basic-list-search-filter-webpart/
.ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$"%>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ListSearchUserControl.ascx.cs" Inherits="SearchWebpart.ListSearch.ListSearchUserControl" %>
<script type="text/javascript" src="/sites/new1/_layouts/scripts/jquery-1.6.3.min.js" src="/sites/new2/_layouts/scripts/jquery-1.6.3.min.js">
 

     $(document).ready(function () { 

        var base_RefreshPageTo = RefreshPageTo; 

         RefreshPageTo = function (event, url) { 

  

             var filterName = getQuerystring('FilterName'); 

             var filterValue = getQuerystring('FilterMultiValue'); 

             var newUrl = url + '&FilterName=' + filterName + '&FilterMultiValue=' + filterValue; 

             if (filterName != '' && filterValue != '') { 

                 base_RefreshPageTo(event, newUrl); 

             } 

             else { 

                 base_RefreshPageTo(event, url); 

            } 

             return; 

         } 

     }); 

     function getQuerystring(key, default_) { 

         if (default_ == null) default_ = ""; 

         key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 

         var regex = new RegExp("[\\?&]" + key + "=([^&#]*)"); 

         var qs = regex.exec(window.location.href); 

         if (qs == null) 

            return default_; 

         else 

             return qs[1]; 

     } 

 </script>



<table> 

     <tr> 

         <td> 

             <strong>Search Criteria:</strong> 

         </td> 

         <td> 

             <asp:TextBox ID="TbSearchText" runat="server" Width="300px"></asp:TextBox> 

         </td> 

         <td> 

             &nbsp; 

         </td> 

         <td> 

             <strong>Field name:</strong> 

         </td> 

         <td> 

            <asp:DropDownList ID="DdlListFields" runat="server"> 

             </asp:DropDownList> 

         </td> 

         <td> 
             &nbsp; 

         </td> 

         <td> 

             <div align="right"> 

                 <asp:Button ID="BtnSearch" runat="server" OnClick="BtnSearch_Click" Text="Search" /> 

                 <asp:Button ID="BtnClearFilter" runat="server" Visible="false" OnClick="BtnClearFilter_Click"

                     Text="Clear Criteria" /> 

             </div> 

        </td> 

     </tr> 

 </table>

.ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Text;
using System.Collections.Specialized; 
using System.Collections.Generic;
using System.Drawing;




namespace SearchWebpart.ListSearch
{
    public partial class ListSearchUserControl : UserControl
    {

        //SPSite site = SPContext.Current.Site;
        //SPWeb web;
        //SPList list;
         public class OptionEntity 

     { 
              public string Id { get; set; } 

        /// <summary> 

         /// Gets or sets the title. 

         /// </summary> 

         /// <value>The title.</value> 

         public string Title { get; set; }
         public string To { get; set; }

         public OptionEntity() 

         { 

         } 

        
     }



       private List<OptionEntity> GetSharePointListFieldItems() 

         { 

             List<OptionEntity> fieldItems = new List<OptionEntity>(); 

             fieldItems = new List<OptionEntity>(); 

             OptionEntity item; 

            SPField field; 

            StringCollection viewFieldCollection = SPContext.Current.ViewContext.View.ViewFields.ToStringCollection(); 

             foreach (string viewField in viewFieldCollection) 

             {
                 //SPSite site = SPContext.Current.Site;
                 //SPWeb web = site.OpenWeb();
                 //SPList list = web.Lists["peoplepicker"];
                 field = SPContext.Current.List.Fields.GetFieldByInternalName(viewField); 
                
                 item = new OptionEntity(); 

                 item.Id = field.InternalName;

                 item.Title = field.Title;
               
               
                

                fieldItems.Add(item);

             }

             return fieldItems; 

         } 

         protected override void CreateChildControls() 
         { 

            base.CreateChildControls(); 

            List<OptionEntity> items = GetSharePointListFieldItems(); 

             DdlListFields.DataSource = items; 

             DdlListFields.DataTextField = "Title"; 

             DdlListFields.DataValueField = "Id"; 

             DdlListFields.DataBind();


         } 

         /// <summary> 

         /// Raises the <see cref="E:System.Web.UI.Control.Load"/> event. 

         /// </summary> 

         /// <param name="e">The <see cref="T:System.EventArgs"/> object that contains the event data.</param> 

         protected override void  OnLoad(EventArgs e) 

         { 

            base.OnLoad(e); 

             if (!IsPostBack) 

             { 

                 if (Request.QueryString["FilterName"] != null) 

                 { 

                     DdlListFields.SelectedValue = Request.QueryString["FilterName"].ToString(); 

                 } 

  

                if (Request.QueryString["FilterMultiValue"] != null) 

                 { 

                    TbSearchText.Text = Request.QueryString["FilterMultiValue"].ToString().Replace("*", ""); 

                   BtnClearFilter.Visible = true;  
                } 

             } 

        } 

         /// <summary> 

         /// Handles the Click event of the BtnSearch control. 

        /// </summary> 

         /// <param name="sender">The source of the event.</param> 

         /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 

         protected void BtnSearch_Click(object sender, EventArgs e) 

        { 

            string redirectUrlFormat = "{0}?FilterName={1}&FilterMultiValue={2}"; 
            string[] selectionCollection = TbSearchText.Text.ToString().Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries); 

             StringBuilder sbValues = new StringBuilder(); 

             foreach (string selection in selectionCollection) 

             { 

                 sbValues.Append("*" + selection.Trim() + "*;"); 

            } 

  
             string urlToRedirectTo = string.Format(redirectUrlFormat, Request.Url.GetLeftPart(UriPartial.Path), DdlListFields.SelectedValue, sbValues.ToString()); 

             Response.Redirect(urlToRedirectTo); 

         } 

         /// <summary> 

         /// Handles the Click event of the BtnClearFilter control. 

        /// </summary> 

        /// <param name="sender">The source of the event.</param> 

         /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> 

         protected void BtnClearFilter_Click(object sender, EventArgs e) 

         { 

             Response.Redirect(Request.Url.GetLeftPart(UriPartial.Path)); 

         } 

    }
}

sharepoint list Search using jquery

Search using jquery:

Reference url:http://geekswithblogs.net/SoYouKnow/archive/2011/01/26/sharepoint-list-views-ndashquick-amp-easy-jquery-filter.aspx

<script src="/sites/testingsite/SiteAssets/jquery-1.9.0.min.js" type="text/javascript"></script><script type="text/javascript">

jQuery(document).ready(function($){
    $('#filterInput').keyup(function()
    {
        DynamicFilter($('#filterInput').val());
    });
})
function stripHTML (field) {
    return field.replace(/<([^>]+)>/g,'');
}
function DynamicFilter(text)
{
    $('table [class="ms-listviewtable"]').find('tr').each(function()
        {
           
            if ($(this).attr("class") != "ms-viewheadertr ms-vhltr")
            {
                 source = stripHTML($(this).html());
                 text = text.toLowerCase();
                            source = source.toLowerCase();
                if (source.indexOf(text) < 0)
                {
                    $(this).hide();
                } else {
                   
                    $(this).show();
                }
            }
        }
    );
}</script><div id="mainDiv"><table><tbody><tr><td width="125" class="Filter">Search:&#160; <input id="filterInput" type="text"/></td></tr></tbody></table></div>
&#160;