About Me

My photo
a Dynamic and Energetic guy.....

Wednesday, September 14, 2011

Create SharePoint search results using FullTextSqlQuery

            DataTable search = new DataTable();
            using (SPSite site = new SPSite(SPContext.Current.Web.Site.Url))
            {
                SearchQueryAndSiteSettingsServiceProxy settingsProxy = SPFarm.Local.ServiceProxies.GetValue();
                SearchServiceApplicationProxy searchProxy = settingsProxy.ApplicationProxies.GetValue("Search Service Application");

                FullTextSqlQuery query = new FullTextSqlQuery(searchProxy);

                query.QueryText = "SELECT FirstName,LastName ,AccountName,WorkPhone,UserName,PreferredName,AboutMe,NationalityP,NationalityS FROM SCOPE() WHERE \"Scope\"='People' ";
                query.ResultTypes = ResultType.RelevantResults;
                query.ResultsProvider = Microsoft.Office.Server.Search.Query.SearchProvider.Default;
                query.KeywordInclusion = KeywordInclusion.AllKeywords;

                ResultTableCollection results = query.Execute();
                if (results.Count > 0)
                {
                    ResultTable relevant = results[ResultType.RelevantResults];
                    search.Load(relevant);

                    // serialize DataTable to XML
                    StringBuilder resultsXml = new StringBuilder();
                    using (StringWriter sw = new StringWriter(resultsXml))
                    {
                        search.WriteXml(sw);
                        sw.Flush();
                    }

                    // move XML to the XmlDataSource
                    XmlDataSource xds = new XmlDataSource();
                    xds.EnableCaching = false;
                    xds.Data = resultsXml.ToString();
                }
            }

No comments:

My Masters