Hi All,
I want to search the documents in a document library by it's name and metadata using web part......
All suggestions are welcome....
Thanks,
Srinivas
Technology Tips and News
Hi All,
I want to search the documents in a document library by it's name and metadata using web part......
All suggestions are welcome....
Thanks,
Srinivas
Hello,
You can write CAML query to get file by name or any other metadata. Here is sample code:
Hope it could help
Hi Srinivas,
You can use jscript in search files or documents in Document library.
You need Jquery - 1.9.0.min.js file and ceated a Content editor web part in document library page and add thr below cmd in Edit the HTML source. This script will search files in the current page [Not inside subfolders]
<script src="/sites/newsite/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: <input id="filterInput" type="text"/></td></tr></tbody></table></div>
Hi,
You can use SharePoint's search engine using the KeywordQuery class. When using search, you can restrict the results to a documents name and metadata (as long as it's been indexed). The search results are returned as a datatable.
just wrote a wiki article on this last night (albeit, I'm using the search results to display the data in a chart, but the search query bit is the same). The article is here: http://social.technet.microsoft.com/wiki/contents/articles/18202.using-the-keywordquery-class-to-search-sharepoint-content.aspx
Using search, you can also sort the results using Managed Properties.
As an example of searching for a document, then binding the results to a SPGridView:
private void GetRecentDocuments(String documentTitle) { 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 (FileExtension:Doc OR FileExtension:Docx)", documentTitle); 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 } }
Thanks Jean,
I tried this but it is not showing search results... Where it will be displayed
Thanks everyone I'll try ----
Thank you so much
Hi Srini,
You can find in the same page.
As i said earlier comments it will search only files in current page. If you want to search all doc.Please increase the item limit.
Hi Jean,
Thanks for quick reply.... Where I can change the limit
In Document library page click on Modify View Ribbon--> Item limit - ><label for="idRowLimit">Number of items to display default it will be 30.</label>
Hi Jean,
But I want to search the documents in Site collection..........
Waiting for your valuable response.
Hi Mathew,
I am new to sharepoint. In the above code I am getting so many errors.
Would please explain in detail?
Thanks in advance
Hi Srini,
I've explained it fairly conisely in the wiki article I referenced. Have a close read of it. Essentially, the only real differences are:
1. You would use a different keyword query
2. Instead of using the results (a DataTable) to populate a chart, you would use it to populate an SPGridView control.
What specific errors are you getting? The code snippet I pasted above requires you to have an SPGridView control added to the webpart with the name "_gridSearchResults".