Create Custom Content by Search Web Part


This is an example of appending additional query to the one that is in the query builder.  You can based on this one and build more complicated logic to alter your query on the fly.

using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.Office.Server.Search.WebControls;
using System.Collections.Generic;
using SWP = Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint.WebPartPages;

public class CustomContentSearchWebPart : ContentBySearchWebPart

    {
private string additionalQuery = String.Empty;

        [WebBrowsable(true), 
        WebDisplayName("Additional Query"), 
        WebDescription("Additional query for web part to insert at the end of the regular query from the query builder."),
        Category("Custom"), SWP.WebPartStorage(SWP.Storage.Shared),
        Personalizable(PersonalizationScope.Shared)]
        public string AdditionalQuery {
            get
            {
                return additionalQuery;
            }
            set
            {
                additionalQuery = value;
            }
        }

        public override ToolPart[] GetToolParts() {

            var toolParts = new List<ToolPart>();

            toolParts.AddRange(base.GetToolParts());

            toolParts.Add(new CustomPropertyToolPart());

            return toolParts.ToArray();
        }

///
        /// Overide the OnLoad
        ///
        ///
        protected override void OnLoad(EventArgs e)
        {
            try
            {                
                if (this.AppManager != null)
                {
                    if (this.AppManager.QueryGroups.ContainsKey(this.QueryGroupName) &&
                        this.AppManager.QueryGroups[this.QueryGroupName].DataProvider != null)
                    {
                        this.AppManager.QueryGroups[this.QueryGroupName].DataProvider.BeforeSerializeToClient +=
                            new BeforeSerializeToClientEventHandler(enhanceQuery);
                    }
                }

                base.OnLoad(e);
            }
            catch (Exception ex)
            {
               //output exception
            }

        }

///
        /// Handles the BeforeSerializeToClient event to override the query
        ///
        ///
        ///
        private void enhanceQuery(object sender, BeforeSerializeToClientEventArgs e)
        {
            try
            {
                DataProviderScriptWebPart dataProvider = sender as DataProviderScriptWebPart;
                if (!String.IsNullOrEmpty(AdditionalQuery))
                    dataProvider.QueryTemplate += AdditionalQuery;
            }
            catch (Exception ex)
            {
                //output exception
            }
        }

}

Comments

Popular posts from this blog

SharePoint 2013 App Details Page Error

SharePoint 2013 - Working with Display Template for Content Search Web Part

SharePoint 2013 Features Information List