Pages

Friday, January 3, 2014

Show SharePoint Search Results in SPGrid View - programmatically

Introduction

As I said in my earlier post I was working and trying some stuff related to SharePoint 2013 search server. Today I was trying to query and display search results in SPGrid view. I have uploaded my solution package in my drive; Please download and look for the code if you need. (Download)
I used certain blog posts as references.


Solution

Create a web part (I have created visual web part). On the .ascx page initiate a SPGridView. (For that you can use the below code)
On the ascx.cs page do the blow
On page load



   

        protected void Page_Load(object sender, EventArgs e)
        {

           string query = string.Format("Project*"); //You can give what ever query you need
           ExecuteFederatedQuery(query);
        }

 


And also you need below both methods. So please paste them also on the ascx.cs page


   

        void ExecuteFederatedQuery(string queryText)
        {
            long lastupdate;
            bool crawl;

            //Create Search service proxy
            SearchServiceApplicationProxy proxy = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy
       (SPServiceContext.GetContext(SPContext.Current.Site));

            
            LocationConfiguration locationConfig = null;
        
            LocationConfiguration[] locationConfigurations = proxy.GetLocationConfigurations(out lastupdate, out crawl);//Get all federated search locations. 
            Location location;
            LocationList locationList = new LocationList();
            QueryManager queryManager = new QueryManager();
            foreach (LocationConfiguration locationConfiguration in locationConfigurations)
            {
                //Specify any federated search location. Here default search location will be captured.  
                if (locationConfiguration.InternalName.Equals("LocalSearchIndex"))
                {
                    locationConfig = locationConfiguration;
                    break;
                }
            }
            location = new Location(locationConfig.InternalName, proxy);
            //Set the userQuery
            location.UserQuery = queryText;
            locationList.Add(location);
            queryManager.Add(locationList);
            queryManager.IsTriggered(locationList);
            XmlDocument xDoc = queryManager.GetResults(locationList);//Return the query
            if (xDoc != null)
            {
                DataSet resultSet = new DataSet();
                resultSet.ReadXml(new XmlNodeReader(xDoc));

                SortData(resultSet.Tables[1]);

            }


        }

        private void SortData(DataTable resultTable)
        {

            //Sort the results by Title name
            DataView dataView = resultTable.DefaultView;
            dataView.Sort = "Title ASC";
            DataTable resuDataTable = dataView.ToTable();


            var fileTable = new DataTable();

            DataRow fileRow;
            fileTable.Columns.Add("Title");
            fileTable.Columns.Add("Modified_Date");
            fileTable.Columns.Add("Authors");
            fileTable.Columns.Add("DocURL");

            foreach (DataRow resultRow in resuDataTable.Rows)
            {

                fileRow = fileTable.NewRow();
                fileRow["Title"] = resultRow["title"];
                fileRow["DocURL"] = resultRow["url"];
                fileRow["Modified_Date"] = resultRow["lastmodifiedtime"];
                fileRow["Authors"] = resultRow["author"];

                fileTable.Rows.Add(fileRow);
            }

            //Assign the datasource
            spGridView.DataSource = fileTable;
            spGridView.DataBind();

            
        }

 

Conclusion

The results will look something like this.


4 comments: