我们在(一)里完成了web part的实现在(二)里完成了通过调用SQL2005 reporting services的web service,查询并显示报表现在,我们要给Web part增加属性来设置报表服务器的地址和报表路径首先,为了能够在SharePoint中设置属性,需要增加对Microsoft.SharePoint的引用,原来我们的BARreportWebPart是从System.Web.UI.WebControls.WebParts.WebPart继承的,现在要改为从Microsoft.SharePoint.WebPartPages.WebPart继承。否则你在Sharepoint web part属性设置里面看不到你的自定义属性然后,增加两个字符类型的属性:ReportServerURL,ReportPath。具体的attribute设置请参考如何给Web part增加自定义属性Creating a Web Part with Custom Properties(http://msdn2.microsoft.com/en-us/library/ms948927.aspx)这是最后的代码using System;using System.Collections.Generic;using System.Text;using System.ComponentModel;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Serialization;using Microsoft.SharePoint.WebPartPages;
namespace BARreportWebPart{ [DefaultProperty("Text"), ToolboxData("<{0}:BARreportWebPart runat=server></{0}:BARreportWebPart>"), XmlRoot(Namespace = "BARreportWebPart")]
public class BARreportWebPart : Microsoft.SharePoint.WebPartPages.WebPart//System.Web.UI.WebControls.WebParts.WebPart { string _reportServerURL="";// = "http://ctc-bar:81/ReportServer/ReportService.asmx"; string _reportPath = "";// = "/BARreports/EBCdetailList";
public BARreportWebPart() {
}
protected override void Render(System.Web.UI.HtmlTextWriter writer) { if (string.Empty == ReportServerURL.Trim() || null == ReportServerURL || string.Empty == ReportPath.Trim() || null == ReportPath) { writer.Write("please set the ReportServerURL, ReportPath"); return; } string reportServerURL = ReportServerURL; string reportPath = ReportPath;
ReportAdapter rsAdapter = new ReportAdapter(reportServerURL, reportPath); ReportingServer.ReportParameter[] parameters = rsAdapter.GetParameters(); ReportingServer.ParameterValue[] parameterValues = null; if (parameters.Length > 0) { parameterValues = new ReportingServer.ParameterValue[parameters.Length]; for (int i = 0; i < parameters.Length; i++) { parameterValues[i] = new ReportingServer.ParameterValue(); parameterValues[i].Name = parameters[i].Name; parameterValues[i].Label = parameters[i].Prompt; } parameterValues[0].Value = "2007-1-1"; parameterValues[1].Value = "2007-3-1"; } System.Text.Encoding enc = System.Text.Encoding.UTF8; byte[] result = rsAdapter.RenderReport(parameterValues); string htmlResult = enc.GetString(result); //htmlResult = htmlResult.Replace(reportServerURL.Replace("/ReportService.asmx", "?"), "http://" & Request("SERVER_NAME") & Request("SCRIPT_NAME") & "?Report="); //writer.Write(htmlResult); }
// Creates a custom property : // This property will be displayed as a text box in the // property pane.
// Create a custom category in the property sheet. //[Category("Custom Properties")] // Assign the default value. [DefaultValue("http://ctc-bar:81/ReportServer/ReportService.asmx")] // Property is available in both Personalization // and Customization mode. [WebPartStorage(Storage.Personal)] // The caption that appears in the property sheet. [FriendlyNameAttribute("ReportServerURL")] // The tool tip that appears when pausing the mouse pointer over // the friendly name in the property pane. [Description("Report Server URL: (like http://ctc-bar:81/ReportServer/ReportService.asmx )")] // Display the property in the property pane. [Browsable(true)] [XmlElement(ElementName = "ReportServerURL")] // The accessor for this property. public string ReportServerURL { get { return _reportServerURL; } set { _reportServerURL = value; } }
// Assign the default value. [DefaultValue("")] // Property is available in both Personalization // and Customization mode. [WebPartStorage(Storage.Personal)] // The caption that appears in the property sheet. [FriendlyNameAttribute("ReportPath")] // The tool tip that appears when pausing the mouse pointer over // the friendly name in the property pane. [Description("Report Path")] // Display the property in the property pane. [Browsable(true)] [XmlElement(ElementName = "ReportPath")] // The accessor for this property. public string ReportPath { get{ return _reportPath; } set { _reportPath = value; } }
}}