Ecco come fare una ricerca basata su keyword in SharePoint 2007 (MOSS).
Le keyword da cercare vanno inserite nella proprietà QueryText dell'oggetto KeywordQuery. Una cosa a cui prestare attenzione è la proprietà Culture importante per determinare quali sono le noise word legate alla proprietà IgnoreAllNoiseQuery (ad esempio gli articoli: il, lo, la, un...) e lo stemming (EnableStemming), ovvero la possibilità di cercare parole simili. Ad esempio se cerco lavoro vengono inclusi anche le parole lavorare, lavorato, lavoravano...
Infine tramite la proprietà KeywordInclusion scelgo se i risultati dovranno includere entrambe le parole cercate (AllKeywords ovvero And) oppure una delle parole (AnyKeyword ovvero Or).

C#

//using Microsoft.SharePoint;
//using Microsoft.SharePoint.Portal;
//using Microsoft.Office.Server;
//using Microsoft.Office.Server.Search.Administration;
//using Microsoft.Office.Server.Search.Query;

string url = "http://sharepoint2007";

using (SPSite site = new SPSite(url))
{
    //Microsoft.Office.Server.dll, namespace=Microsoft.Office.Server.ServerContext
    ServerContext context = ServerContext.GetContext(site);
    //Microsoft.Office.Server.Search.dll, namespace=Microsoft.Office.Server.Search.Administration
    //SearchContext searchContext = SearchContext.GetContext(context);
    //Microsoft.Office.Server.Search.dll, namespace=Microsoft.Office.Server.Search.Query;
    KeywordQuery kwd = new KeywordQuery(context);
    kwd.Culture = new System.Globalization.CultureInfo(1040);
    //ResultType=DefinitionResults,HighConfidenceResults,None,RelevantResults,SpecialTermResults
    kwd.ResultTypes = ResultType.RelevantResults;
    kwd.StartRow = 0;
    kwd.RowLimit = 100;
    kwd.EnableStemming = true;
    kwd.TrimDuplicates = true;
    kwd.IgnoreAllNoiseQuery = true;
    //KeywordInclusion=AllKeywords,AnyKeyword;
    kwd.KeywordInclusion = KeywordInclusion.AnyKeyword;
    //the word to search
    kwd.QueryText = "test open";
    //execute search
    ResultTableCollection results = kwd.Execute();
    ResultTable result = results[ResultType.RelevantResults];
    //build a table
    DataTable tbl = new DataTable();
    tbl.Load(result, LoadOption.OverwriteChanges);

    //show all item
    foreach (DataRow row in tbl.Rows)
    {
        Console.WriteLine(string.Format("Title: {0}", row["Title"]));
        Console.WriteLine(string.Format("  Rank: {0}, Author: {1}, Size: {2}"
            , row["Rank"], row["Author"], row["Size"]));
        Console.WriteLine(string.Format("  Path: {0}\r\n"
            , row["Path"]));
    }
}
Tags:
Search7 SharePoint497 SharePoint 2007218 SharePoint 2010224
Potrebbe interessarti anche: