Click here to Skip to main content
15,883,531 members
Articles / Web Development / ASP.NET

Crystal Reports WebPart for SharePoint

Rate me:
Please Sign up or sign in to vote.
4.56/5 (5 votes)
6 Oct 2009CPOL6 min read 198K   4.3K   26  
An article on building a SharePoint WebPart to dynamically display Crystal Reports.
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Text.RegularExpressions; 

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.Win32;

using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Web;

namespace CustomCRWP
{
    [Guid("beaaf3b3-b1cd-4862-bd13-54a49e4407d4")]
    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("Crystal Reports rpt Files");
                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("Crystal Reports rpt Files");
                SPFileCollection files = folder.Files;

                foreach (SPFile file in files)
                {
                    rptslist.Items.Add(file.Name);
                }

                Controls.Add(rptslist);
                this.ChildControlsCreated = true;
                base.CreateChildControls();
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO Computation Limited
New Zealand New Zealand
Ray Goddard currently runs his own software business and is deep into writing internet based applications and trying to keep track of new developments.

Comments and Discussions