If you are working on sharepoint based intranet and would like to integrate already developed Crystal reports to your sharepoint site then your search ends here!!
You just need to create one web part which holds the .rpt files from custom document library and shows the data/charts from the report on your site. So create document library named "Reports"(you can change the name but make changes in code accordingly.) on your site.
So create Sharepoint web part and add following code in to it:
Required Namespaces:
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Web;
public class CustomCrystalReportWP : System.Web.UI.WebControls.WebParts.WebPart
{
String strReportName = "";
ReportSourcePart edPart = new ReportSourcePart();
public CustomCrystalReportWP()
{
}
// serialise the name of the report
[Personalizable(PersonalizationScope.Shared, false)] // Storage.
public string ReportSource
{
get
{
return strReportName;
}
set
{
strReportName = value;
}
}
protected override void CreateChildControls()
{
CrystalDecisions.Web.CrystalReportViewer crystalReportViewer1 = new CrystalDecisions.Web.CrystalReportViewer();
ReportDocument crdoc = new ReportDocument();
this.SetPersonalizationDirty();
base.CreateChildControls();
if (strReportName.Length > 0)
{
SPWeb thisSite;
thisSite = SPControl.GetContextWeb(Context);
SPFolder folder = thisSite.GetFolder("Reports");
if (folder.Exists)
{
// get collection of Crystal Reports rpt files in the document library
SPFileCollection files = folder.Files;
// open the rpt file and get the contents
SPFile srcfile = files[strReportName];
byte[] content = srcfile.OpenBinary();
// make a temporary folder
DirectoryInfo dir2 = new DirectoryInfo("~/temp");
if (!dir2.Exists)
dir2.Create();
if (File.Exists("~/temp/temp.rpt"))
{
File.Delete("~/temp/temp.rpt");
}
// write the report definition to a temporary file
BinaryWriter bw = new BinaryWriter(File.Open("~/temp/temp.rpt", FileMode.Create));
bw.Write(content);
bw.Close();
// set up the crystal report
crdoc.Load("~/temp/temp.rpt");
// and the Crystal report Viewer
crystalReportViewer1.ReportSource = crdoc;
crystalReportViewer1.ReuseParameterValuesOnRefresh = false;
crystalReportViewer1.HasRefreshButton = true;
this.Controls.Add(crystalReportViewer1);
// clean up
File.Delete("~/temp/temp.rpt");
}
}
}
// Display a custom editor in the tool pane, this gets displayed when you edit the web part settings.
public override EditorPartCollection CreateEditorParts()
{
ArrayList arEditorParts = new ArrayList();
edPart.ID = this.ID + "RptSrcEditorPart";
arEditorParts.Add(edPart);
return new EditorPartCollection(arEditorParts);
}
// Create a custom EditorPart to edit the WebPart.
class ReportSourcePart : EditorPart
{
DropDownList rptslist = new DropDownList();
// Get settings from web part.
public override void SyncChanges()
{
CustomCrystalReportWP part = (CustomCrystalReportWP)this.WebPartToEdit;
}
// Apply new settings to web part.
public override bool ApplyChanges()
{
CustomCrystalReportWP part = (CustomCrystalReportWP)this.WebPartToEdit;
part.strReportName = rptslist.SelectedValue;
return true;
}
// Render the control.
protected override void CreateChildControls()
{
// Set the title to display in the properties pane
this.Title = "Crystal Reports List";
// add elements to dropdown
SPWeb thisSite;
thisSite = SPControl.GetContextWeb(Context);
//thisSite.Site.P
SPFolder folder = thisSite.GetFolder("Reports");
SPFileCollection files = folder.Files;
foreach (SPFile file in files)
{
rptslist.Items.Add(file.Name);
}
Controls.Add(rptslist);
this.ChildControlsCreated = true;
base.CreateChildControls();
}
}
}
You have to add CrystalImageHandler.aspx and CrystalImageHandler.aspx.cs to the root folder of your site to display the graphics on your site. You can copy those files from http://www.codeproject.com/KB/sharepoint/CustomCRWP2.aspx then add the reference to httpHandlers in your web.config,
Now your web part is ready to deploy:)
No comments:
Post a Comment