技术开发 频道

创建Web部件来显示SharePoint Portal Server的搜索数据

    定义类级别的变量

    在定义类级别的变量后,替换 Server_Name 和 Database_Name 以反映您的环境。请使用以下代码:

private string text =""; const string tblName = "tblSearchTerms"; const string strConn = "Data Source=Server_Name;Initial Catalog=Database_Name;Integrated Security=SSPI;"; HtmlSelect startDate,endDate,selectDay,selectUser; Label lblDateRange,lblDay,lblUser; HtmlButton btnDateRange,btnDay,btnUser; string sTableData=""; HtmlTextArea Result; const string defaultText = ""; private SPWeb oWeb = null;

     定义子控件

    使用 HTML 子控件来提供用户界面。定义四个 HtmlSelect 控件来显示用户的三个选择,以便获得如下搜索统计数据:

    ? 数据范围(每天完成搜索的数量)。
    ? 当天数据(当天搜索的内容以及搜索的次数)。
    ? 用户数据(用户搜索的内容以及用户搜索的次数)。

    还可以定义一个 HtmlTextArea 控件来显示结果,并定义三个 HtmlButton 控件来执行前面三个选择中每个的代码。将以下代码行添加到 CreateChildControls。

oWeb = SPControl.GetContextWeb(Context); //Result text area Result = new HtmlTextArea(); Result.Cols = 50; //Start Date, End Date, Day, User combo boxes startDate = new HtmlSelect(); endDate = new HtmlSelect(); selectDay = new HtmlSelect(); selectUser = new HtmlSelect(); //Respective labels lblDateRange = new Label(); lblDateRange.Text = "Date Range Data:"; lblDay = new Label(); lblDay.Text = "Day Data:"; lblUser = new Label (); lblUser.Text = "User Data:"; //Buttons to execute queries and their click events btnDateRange = new HtmlButton(); btnDateRange.InnerText = "Go"; btnDateRange.ServerClick += new EventHandler (btnDateRange_click); btnDay = new HtmlButton(); btnDay.InnerText = "Go"; btnDay.ServerClick += new EventHandler (btnDay_click); btnUser = new HtmlButton(); btnUser.InnerText = "Go"; btnUser.ServerClick += new EventHandler (btnUser_click); //Populate the Date Range, Day, and user combo boxes with data //from the table SqlConnection objConn = new SqlConnection(strConn); //Set up stored procedure calls SqlCommand cmdDate = new SqlCommand("sp_GetDateData", objConn); SqlCommand cmdUsers = new SqlCommand("sp_GetUserData", objConn); cmdDate.CommandType = CommandType.StoredProcedure; cmdUsers.CommandType = CommandType.StoredProcedure; try { //Open stored procedure for Dates SqlDataReader drDate; objConn.Open(); drDate = cmdDate.ExecuteReader(); //Read dates into the controls while (drDate.Read()) { //Convert the time zone of the date to UTC //FormatDate converts it back to a local time //on the SharePoint Portal Server computer; the following two //lines negate time zone conversions but //are necessary to show dates in the local SharePoint calendar System.DateTime myDate = oWeb.RegionalSettings.TimeZone.LocalTimeToUTC((System.DateTime)drDate .GetValue(0)); string myFormattedDate = SPUtility.FormatDate(oWeb,myDate,SPDateFormat.DateOnly); startDate.Items.Add(SPEncode.HtmlEncode(myFormattedDate)); endDate.Items.Add(SPEncode.HtmlEncode(myFormattedDate)); selectDay.Items.Add(SPEncode.HtmlEncode(myFormattedDate)); } //Close reader and connection drDate.Close(); objConn.Close(); } catch(Exception ex) { Result.InnerText = "An Error has occurred: " + ex.Message; } try { //Open stored procedure for Users SqlDataReader drUsers; objConn.Open(); drUsers = cmdUsers.ExecuteReader(); //Read users into the control while (drUsers.Read()) { selectUser.Items.Add(drUsers.GetValue(0).ToString()); } //Close reader and connection drUsers.Close(); objConn.Close(); } catch(Exception ex) { Result.InnerText = "An Error has occurred: " + ex.Message; } //Add all the controls to render Controls.Add (startDate); Controls.Add (endDate); Controls.Add (btnDateRange); Controls.Add (selectDay); Controls.Add (btnDay); Controls.Add (selectUser); Controls.Add (btnUser); Controls.Add (Result); Controls.Add (lblDateRange); Controls.Add (lblUser); Controls.Add (lblDay);

    编写 HtmlButton 控件的单击事件代码

    在每个单击事件中,使用 SqlConnection 来打开到包含 tblSearchTerms 表的数据库的连接,并据此运行各自的查询。在找回数据后,将其传递到显示结果的 Web 部件上的 HtmlTextArea 控件。

    在 CreateChildControls 部分后添加以下代码:

public void btnDateRange_click(object sender, EventArgs e) { //Use ParseDate to convert from SharePoint calendar date //to System.DateTime //Use a time value of "12:00:00" as a placeholder to ensure //time conversion does not cross dates //Use the Date property of the System.DateTime object; //these dates are used for the stored procedure System.DateTime dtStartDate = SPUtility.ParseDate(oWeb,startDate.Value.ToString(),"12:00:00",false).Date; System.DateTime dtEndDate = SPUtility.ParseDate(oWeb,endDate.Value.ToString(),"12:00:00",false).Date; int cntRows = new Int16(); //Set up connection SqlConnection objConn = new SqlConnection(strConn); //Set up sp command SqlCommand cmdDateFiltered = new SqlCommand("sp_GetDatabyDate", objConn); cmdDateFiltered.CommandType = CommandType.StoredProcedure; //Add Parameters to sp command cmdDateFiltered.Parameters.Add("@StartDate", SqlDbType.DateTime); cmdDateFiltered.Parameters["@StartDate"].Value = dtStartDate; cmdDateFiltered.Parameters.Add("@EndDate", SqlDbType.DateTime); cmdDateFiltered.Parameters["@EndDate"].Value = dtEndDate; try { SqlDataReader drDateFiltered; objConn.Open(); drDateFiltered = cmdDateFiltered.ExecuteReader(); while (drDateFiltered.Read()) { //Read data into variables string myTerm = drDateFiltered.GetValue(0).ToString(); //Convert the time zone of the date to UTC //FormatDate converts it back to a local time //on the SharePoint Portal Server computer //The following two lines negate time zone conversions but //are necessary to show dates in the local SharePoint calendar System.DateTime myDate = oWeb.RegionalSettings.TimeZone.LocalTimeToUTC((System.DateTime)drDateFiltered.GetValue(1)); string myFormattedDate = SPUtility.FormatDate(oWeb,myDate,SPDateFormat.DateOnly); sTableData = sTableData + myFormattedDate + "\t" + myTerm + "\t" + myStrCount + "\n"; cntRows += 1; } //Close reader and connection drDateFiltered.Close(); objConn.Close(); } catch(Exception ex) { Result.InnerText = "An error has occurred: \n" + ex.Message; } //Set row count on HTML control and fill with result Result.Rows = cntRows + 2; Result.InnerText = "Date" + "\t" + "Search Term" + "\t" + "Count" + "\n" + "------------------------------------" +"\n" + SPEncode.HtmlEncode(sTableData); } public void btnDay_click(object sender, EventArgs e) { //Use ParseDate to convert from SharePoint calendar date //to System.DateTime //Use a time value of "12:00:00" as a placeholder to ensure //time conversion does not cross dates //Use the Date property of the System.DateTime object; //this date is used for the stored procedure System.DateTime dtStartDate = SPUtility.ParseDate(oWeb,selectDay.Value.ToString(),"12:00:00",false).Date; int cntRows = new Int16(); //Set up connection SqlConnection objConn = new SqlConnection(strConn); //Set up sp command SqlCommand cmdDateFiltered = new SqlCommand("sp_GetDatabyDate", objConn); cmdDateFiltered.CommandType = CommandType.StoredProcedure; //Add Parameters to sp command cmdDateFiltered.Parameters.Add("@StartDate", SqlDbType.DateTime); cmdDateFiltered.Parameters["@StartDate"].Value = dtStartDate; cmdDateFiltered.Parameters.Add("@EndDate", SqlDbType.DateTime); cmdDateFiltered.Parameters["@EndDate"].Value = dtStartDate; try { SqlDataReader drDateFiltered; objConn.Open(); drDateFiltered = cmdDateFiltered.ExecuteReader(); while (drDateFiltered.Read()) { //Read data into variables string myTerm = drDateFiltered.GetValue(0).ToString(); //Convert the time zone of the date to UTC //FormatDate converts it back to a local time //on the SharePoint server. The following two lines //negate time zone conversions but are necessary //to show dates in the local SharePoint calendar System.DateTime myDate = oWeb.RegionalSettings.TimeZone.LocalTimeToUTC((System.DateTime)drDateFiltered.GetValue(1)); string myFormattedDate = SPUtility.FormatDate(oWeb,myDate,SPDateFormat.DateOnly); string myStrCount = drDateFiltered.GetValue(2).ToString(); sTableData = sTableData + myFormattedDate + "\t" + myTerm + "\t" + myStrCount + "\n"; cntRows += 1; } //Close reader and connection drDateFiltered.Close(); objConn.Close(); } catch(Exception ex) { Result.InnerText = "An error has occurred: \n" + ex.Message; } //Set row count on HTML control and fill with result Result.Rows = cntRows + 2; Result.InnerText = "Date" + "\t" + "Search Term" + "\t" + "Count" + "\n" + "------------------------------------" +"\n" + SPEncode.HtmlEncode(sTableData); } public void btnUser_click(object sender, EventArgs e) { string strUser = selectUser.Value.ToString(); int cntRows = new Int16(); //Set up connection SqlConnection objConn = new SqlConnection(strConn); //Set up sp command SqlCommand cmdUserFiltered = new SqlCommand("sp_GetDatabyUser", objConn); cmdUserFiltered.CommandType = CommandType.StoredProcedure; //Add Parameters to sp command cmdUserFiltered.Parameters.Add("@UserName", SqlDbType.VarChar,255); cmdUserFiltered.Parameters["@UserName"].Value = strUser; try { SqlDataReader drUserFiltered; objConn.Open(); drUserFiltered = cmdUserFiltered.ExecuteReader(); while (drUserFiltered.Read()) { //Read data into variables string myTerm = drUserFiltered.GetValue(0).ToString(); //Convert the time zone of the date to UTC //FormatDate converts it back to a local time //on the SharePoint Portal Server computer. //The following two lines negate time zone conversions but //are necessary to show dates in the local SharePoint calendar System.DateTime myDate = oWeb.RegionalSettings.TimeZone.LocalTimeToUTC((System.DateTime)drUserFiltered.GetValue(1)); string myFormattedDate = SPUtility.FormatDate(oWeb,myDate,SPDateFormat.DateOnly); string myStrCount = drUserFiltered.GetValue(2).ToString(); sTableData = sTableData + myFormattedDate + "\t" + myTerm + "\t" + myStrCount + "\n"; cntRows += 1; } //Close reader and connection drUserFiltered.Close(); objConn.Close(); } catch(Exception ex) { Result.InnerText = "An error has occurred: \n" + ex.Message; } //Set row count on HTML control and fill with result Result.Rows = cntRows + 2; Result.InnerText = "Date" + "\t" + "Search Term" + "\t" + "Count" + "\n" + "------------------------------------" +"\n" + SPEncode.HtmlEncode(sTableData); }

    修改 RenderWebPart 方法

    最后,您需要修改 RenderWebPart 方法中的代码,以在 Web 部件中呈现子控件。在 RenderWebPart 中添加以下代码。

lblDateRange.RenderControl(output); output.Write(text); startDate.RenderControl(output); endDate.RenderControl(output); btnDateRange.RenderControl(output); output.Write(text); lblDay.RenderControl(output); output.Write(text); selectDay.RenderControl(output); btnDay.RenderControl(output); output.Write(text); lblUser.RenderControl(output); output.Write(text); selectUser.RenderControl(output); btnUser.RenderControl(output); output.Write(text); output.Write(text); Result.RenderControl(output); output.Write(text);

    关于日期的注释

    SQL 存储过程和 Web 部件代码假设您从一个时区中的一台 SharePoint Portal Server 计算机中累积数据。可以修改存储过程和 Web 部件代码以包括时间信息,但是时区信息会根据 Web 部件当前运行的 SharePoint Portal Server 配置进行调整,而不是根据所执行的每个单独搜索的时区信息。

    在浏览器中显示和检索日期信息时,会分别使用 SPUtility 类的 FormatDate 和 ParseDate 方法。Web 部件代码和 SQL Server 计算机之间的日期操作是使用 .NET System.DateTime 对象进行处理的。

    SPUtility 类的 FormatDate 方法会将日期的时区调整为本地 SharePoint Portal Server 计算机的时区,并且会设置日期的格式,以便它可以显示在 SharePoint Portal Server 计算机的本地日历中。示例代码并没有考虑搜索发生的时间。此外,时间“12:00:00”会传递到 ParseDate 方法,以确保从 Web 部件检索并且随后用于存储过程的日期没有跨天进行调整。

    小结

    在 SQL Server 数据库中累积 Microsoft Office SharePoint Portal Server 2003 搜索项数据,然后使用自定义的 Web 部件查询并显示信息是一种非常强大的方法,该方法可以查看包含在每天 IIS 日志文件中的数据。SharePoint Portal Server 会自动记录搜索项数据。但是,通过将这种数据从每天的日志文件合并到 SQL Server 数据库,可以使数据用于快速检索和分析。

0
相关文章