Thursday, February 20, 2014

Appending to User Query in SP2010 CoreResults WebPart


How does one Append to the Query in CoreResults WebPart


Difference between FixedQuery and AppendedQuery
Fixed Query - Overrides the user Query... Might be useful in cases where search is to be performed automatically without User Input or through Bots
Appended Query - More commonly used. For appending to the user query


ConfigureDataSourceProperties

      protected override void ConfigureDataSourceProperties()
        {

            try
            {
                string curreURL = SPContext.Current.Site.Url;

                string searchTerm = Page.Request.QueryString["k"].ToString();

                if (curreURL.ToLower().Contains("uk"))
                {
                    this.AppendedQuery = "dtturl:\"uk\"";
                }
               
                base.ConfigureDataSourceProperties();

                CoreResultsDatasource dataSource = this.DataSource as CoreResultsDatasource;
 
            }
            catch (Exception ex)
            {
                Trace.WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss tt") + " - " + ex.Message);
            }
            CloseTraceFile();
        }

No comments: