Hi Friends,
In SharePoint 2010, many times we have business requirements where we need to generate custom search reports related to "Failed Queries" and "No result queries". We also had one similar requirement for one of our client so thought to share the running code with you guys to ease the work.
public void GenerateReport()
{
DataTable table = null;
DataRow dr = null;
try
{
if (!string.IsNullOrEmpty(reportRequest.SrchFailureRptStDt) &&
!string.IsNullOrEmpty(reportRequest.SrchFailureRptEdDt))
{
// Get Datatable Definition
table = GetTable();
#region Fetch data from SharePoint and populate Datatable
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(this.currSiteColl.Url))
{
AggregationContext aggregationContext = AggregationContext.GetContext(site.WebApplication);
if (aggregationContext != null)
{
List<ViewParameterValue> viewParamsList = new List<ViewParameterValue>();
DataPacket dataPacket = null;
bool isException = false;
try
{
viewParamsList.Add(new ViewParameterValue("StartDateId", Common.DateTimeToDateId(Convert.ToDateTime(this.reportRequest.SrchFailureRptStDt).ToUniversalTime())));
viewParamsList.Add(new ViewParameterValue("EndDateId", Common.DateTimeToDateId(Convert.ToDateTime(this.reportRequest.SrchFailureRptEdDt).ToUniversalTime())));
viewParamsList.Add(new ViewParameterValue("AggregationId", aggregationContext.AggregationId));
viewParamsList.Add(new ViewParameterValue("IncludeSubSites", true));
dataPacket = FrontEndDataRetriever.QueryData(aggregationContext, null, "fn_WA_GetTopFailedSearchQueries", viewParamsList, null, null, 1, 25000, false);
}
catch (Exception ex)
{
}
if (dataPacket.DataTable != null && dataPacket.DataTable.Rows.Count > 0)
{
foreach (DataRow objDataRow in dataPacket.DataTable.Rows)
{
table.Rows.Add(dr);
table.AcceptChanges();
}
#endregion
}
}
else
{
Logger.WriteLog(Logger.Category.Information, "ProcessMTIReports - " + Constants.SRCH_FAIL_REP_NAME, "Start Processing the report for data: Blank");
dr = table.NewRow();
dr["Term"] = "No search results returned.";
//dr["No. of Queries"] = 0;
//dr["% Abandoned"] = 0;
table.Rows.Add(dr);
table.AcceptChanges();
}
}
}
});
#endregion
}
}
catch (Exception ex)
{
throw;
}
}
private static DataTable GetTable()
{
DataTable table = new DataTable();
table.Columns.Add("Term", typeof(string));
table.Columns.Add("No. of Queries", typeof(int));
table.Columns.Add("% Abandoned", typeof(int));
return table;
}
Thanks
In SharePoint 2010, many times we have business requirements where we need to generate custom search reports related to "Failed Queries" and "No result queries". We also had one similar requirement for one of our client so thought to share the running code with you guys to ease the work.
public void GenerateReport()
{
DataTable table = null;
DataRow dr = null;
try
{
if (!string.IsNullOrEmpty(reportRequest.SrchFailureRptStDt) &&
!string.IsNullOrEmpty(reportRequest.SrchFailureRptEdDt))
{
// Get Datatable Definition
table = GetTable();
#region Fetch data from SharePoint and populate Datatable
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(this.currSiteColl.Url))
{
AggregationContext aggregationContext = AggregationContext.GetContext(site.WebApplication);
if (aggregationContext != null)
{
List<ViewParameterValue> viewParamsList = new List<ViewParameterValue>();
DataPacket dataPacket = null;
bool isException = false;
try
{
viewParamsList.Add(new ViewParameterValue("StartDateId", Common.DateTimeToDateId(Convert.ToDateTime(this.reportRequest.SrchFailureRptStDt).ToUniversalTime())));
viewParamsList.Add(new ViewParameterValue("EndDateId", Common.DateTimeToDateId(Convert.ToDateTime(this.reportRequest.SrchFailureRptEdDt).ToUniversalTime())));
viewParamsList.Add(new ViewParameterValue("AggregationId", aggregationContext.AggregationId));
viewParamsList.Add(new ViewParameterValue("IncludeSubSites", true));
dataPacket = FrontEndDataRetriever.QueryData(aggregationContext, null, "fn_WA_GetTopFailedSearchQueries", viewParamsList, null, null, 1, 25000, false);
}
catch (Exception ex)
{
}
if (dataPacket.DataTable != null && dataPacket.DataTable.Rows.Count > 0)
{
foreach (DataRow objDataRow in dataPacket.DataTable.Rows)
{
table.Rows.Add(dr);
table.AcceptChanges();
}
#endregion
}
}
else
{
Logger.WriteLog(Logger.Category.Information, "ProcessMTIReports - " + Constants.SRCH_FAIL_REP_NAME, "Start Processing the report for data: Blank");
dr = table.NewRow();
dr["Term"] = "No search results returned.";
//dr["No. of Queries"] = 0;
//dr["% Abandoned"] = 0;
table.Rows.Add(dr);
table.AcceptChanges();
}
}
}
});
#endregion
}
}
catch (Exception ex)
{
throw;
}
}
private static DataTable GetTable()
{
DataTable table = new DataTable();
table.Columns.Add("Term", typeof(string));
table.Columns.Add("No. of Queries", typeof(int));
table.Columns.Add("% Abandoned", typeof(int));
return table;
}
Thanks