Custom Metadata Search using SharePoint Search Object Model
Posted by zieglers on October 9, 2009
Here is a little code snippet re custom metadata search using SharePoint Object Model. This code is just querying a predefined scope – MySearchScope - for custom metadata field – MyCustomMetadata. Search Criteria is passed from a text field named txtSearchCriteria.
There are couple of things to note here:
1. Make sure you created a Managed Property for your custom metadata in your SSP.
2. Make sure that your custom scope is configured properly. It’s good practice to have a separate content source assigned for your custom scope.
3. Make sure that your custom scope is Shared, compiled and ready to use.
4.Be careful with QueryText construction. Custom scope must be enclosed in ” ” in WHERE clause.
==================================================================================
DataTable queryDataTable = new DataTable();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(siteUrl))
{
// Construct query object based on document number entered bu user
// Create a new FullTextSqlQuery class – use property intializers to set query
FullTextSqlQuery myQuery = new FullTextSqlQuery(ServerContext.Current)
{
// Construct query text
QueryText = “SELECT Title, Rank, Size, Description, Write, Path, MyCustomMetadata FROM portal..scope() WHERE MyCustomMetadata = ‘” + txtSearchCriteria.Text + “‘ AND ( (\“SCOPE\“ = ‘MySearchScope ‘) ) ORDER BY \”Rank\” DESC”,
ResultTypes = ResultType.RelevantResults
};
// execute the query and load the results into a datatable
ResultTableCollection queryResults = myQuery.Execute();
ResultTable queryResultsTable = queryResults[ResultType.RelevantResults];
// Load table with results
queryDataTable.Load(queryResultsTable, LoadOption.OverwriteChanges);
}
});
==================================================================================
zieglers
