Custom search webpart in sharepoint 2010

Hi all,

I need to search documents in a library based on the given date range and the search keyword. To be more clear, webpart contains two dates(start date and end date) and text box for keyword search. How can i achieve this? Please guide me.

Thanks in ad

July 2nd, 2013 1:52pm

Hi Sam,

I wrote a TechNet Wiki article about this just last week. It goes through using the KeywordQuery class to search SharePoint based on a date range, content type and another property. The wiki article is here: http://social.technet.microsoft.com/wiki/contents/articles/18202.using-the-keywordquery-class-to-search-sharepoint-content.aspx

The basic's of it are:

private void GetRecentDocuments(String documentTitle, String dateRangeFrom, String dateRangeTo)
{
	try
	{
		var ssaProxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site));
		var keywordQuery = new KeywordQuery(ssaProxy);
		keywordQuery.RowLimit = 100;
		keywordQuery.SelectProperties.Clear();
		keywordQuery.SelectProperties.Add("Title");
		keywordQuery.SelectProperties.Add("FileExtension");
		keywordQuery.SelectProperties.Add("LastModifiedTime");
		keywordQuery.SelectProperties.Add("Path");
		keywordQuery.SelectProperties.Add("PrimaryDate");
		keywordQuery.TrimDuplicates = true;
		keywordQuery.SortList.Add("Rank", SortDirection.Descending);
		keywordQuery.ResultsProvider = SearchProvider.Default;
		keywordQuery.ResultTypes |= ResultType.RelevantResults;
		keywordQuery.QueryText = String.Format("Name:\"{0}\" AND (PRIMARYDATE>{1} AND PRIMARYDATE<{2})", documentTitle, dateRangeFrom, dateRangeTo);
		ResultTableCollection searchResults;
		try
		{
			searchResults = keywordQuery.Execute();
		}
		catch (Exception)
		{
			//"Your query is malformed. Please rephrase your query."
			return;
		}
		if (searchResults.Exists(ResultType.RelevantResults))
		{
			var searchResult = searchResults[ResultType.RelevantResults];
			var results = new DataTable { TableName = "SearchResults" };
			results.Load(searchResult, LoadOption.OverwriteChanges);
			_gridSearchResults.DataSource = results;
			_gridSearchResults.Columns.Clear();
			_gridSearchResults.Columns.Add(new SPBoundField(){DataField = "Title", ShowHeader = false});
			_gridSearchResults.Columns.Add(new SPBoundField() { DataField = "PrimaryDate", ShowHeader = false });
			_gridSearchResults.DataBind();
			_gridSearchResults.AllowPaging = true;
		}
	}
	catch (Exception e)
	{
		//Unhandled Exception running query
	}
}

Free Windows Admin Tool Kit Click here and download it now
July 2nd, 2013 2:23pm

Hi Mathew,

Thank you so much for your reply. The above solution is searching in all the lists. How can i search in only one list?

I have one more question, can we customize OOTB search by adding date range and get results based on querystring(keyword search) and date range? if yes, can you please provide the solution or any links. 

T

July 3rd, 2013 10:29am

To search in one list just change the keyword query. The keyword query will take a query formatted in the same way you would write a query for the OOTB search box.

E.g. To restrict the search to a document library

keywordQuery.QueryText = String.Format("Name:\"{0}\" AND Path:http://devmy101/Lists/mycustomlist AND (PRIMARYDATE>{1} AND PRIMARYDATE<{2})", documentTitle, dateRangeFrom, dateRangeTo);

E.g. To restrict the search to a content type

 keywordQuery.QueryText = String.Format("Name:\"{0}\" AND contenttype:MyCustomContentType AND (PRIMARYDATE>{1} AND PRIMARYDATE<{2})", documentTitle, dateRangeFrom, dateRangeTo);

You can probably use the OOTB core results webpart, but you will need to style the results using XSL, as the results are returned to the browser as XML.

Free Windows Admin Tool Kit Click here and download it now
July 3rd, 2013 11:00am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics