Tuesday, April 30, 2013

Create C# Classes from XML


  1. Find XSD.exe on your PC. Its a .net tool.
  2. Assuming xml name is Statements.xml, run the following:
    xsd.exe Statements.xml
  3. This generates a Statements.xsd file.
  4. Now run this:
    xsd.exe /dataset /language:CS Statements.xsd
  5. This generates Statements.cs, which is fairly useless unless the XML is perfect. At least you have the class names and the properties stubbed out. I just copy and paste these.

Monday, April 22, 2013

Save File to Sharepoint

This code shows how to use c# to send a file to Sharpoint using the WCF REST Services. 

Create service reference

Add a service reference to the C# Windows form project. The URL is 

http://test.sharepoint.nterprise.net/servicetesting/_vti_bin/ListData.svc

Create a Sharepoint Gateway class


        public string SendByteArrayToSharepoint(Byte[] bytes, string fileName)
        {
            try
            {
                string sharepointUrl = (string)Settings.Default["SharepointUrl"];
                Stream fileStream = new MemoryStream(bytes);

                ServiceTestingDataContext context = new ServiceTestingDataContext(_sharepointUri);
                context.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
                string slug = Path.Combine(@"/servicetesting/DropBox/", fileName);
                string contentType = NtfsUtilities.GetMimeFromByteArray(bytes);
                DropBoxItem dbi = new DropBoxItem()
                {
                    ContentType = contentType,
                    Name = Path.GetFileName(fileName),
                    Source = "source",
                    TargetSystem = "targetSystem",
                    Reference = "reference",
                    Notes = "notes",
                    OriginalFilename = Path.GetFileName(fileName),
                    Path = fileName
                };

                context.AddToDropBox(dbi);
                context.SetSaveStream(dbi, fileStream, true, contentType, slug);
                context.SaveChanges();
                //at this point, the document ID is NULL, as it was assigned on the server.
                //Calling context.LoadProperty(dbi, "DocumentID"); causes the dbi object to be refreshed
                context.LoadProperty(dbi, "DocumentID");
                return dbi.DocumentID;
            }
            catch (Exception ex)
            {

                throw;
            }
        }


Use the class

Read the file into a byte array. then call the method SendByteArrayToSharepoint:
string SharepointPDFUrl = WriteBytesToSharepoint(theByteArray, "fileName.pdf");


Wednesday, April 17, 2013

SSRS report to PDF saved in hard drive folder

This code executes an SSRS report located on a remote server, then serializes the report as PDF to a folder on the hard drive:

You have to create a web reference called ReportServiceReference, and import the url  http://bidev.develop.fcbt/ReportServer/ReportExecution2005.asmx?wsdl

This is the report I will be running.


 

using System;
using System.IO;
using System.Net;
using Applications.LoanAccounting.BillsAndStatements.Properties;
using Applications.LoanAccounting.BillsAndStatements.ReportServiceReference;

namespace Applications.LoanAccounting.BillsAndStatements
{
    public class DocumentRenderingManager
    {
        #region Private members

        private ReportExecutionService _reportExecutionService;
        #endregion


        #region Constructor
        public DocumentRenderingManager()
        {
            _reportExecutionService = new ReportExecutionService();
            _reportExecutionService.Credentials = CredentialCache.DefaultCredentials;
        }

        #endregion

        #region General public methods

        public string RunBillingReportByHeaderId(string headerId)
        {

            Guid id = Guid.NewGuid();

            string fileName = String.Format("{0}{1}{2}.pdf",
                (string)Settings.Default["PDFGenerationLocation"],
                "BillReport",
                id);

            string _reportName = (string)Settings.Default["BillingReportFolderName"] +
                (string)Settings.Default["BillingReportName"];

            string deviceInfo = null;
            const string format = "PDF";
            Byte[] results;
            string encoding = String.Empty;
            string mimeType = String.Empty;
            string extension = String.Empty;
            Warning[] warnings = null;
            string[] streamIDs = null;

            try
            {
                ExecutionInfo ei = _reportExecutionService.LoadReport(_reportName, null);
                ParameterValue[] parameters = new ParameterValue[1];
                parameters[0] = new ParameterValue();
                parameters[0].Label = "BillHeaderId";
                parameters[0].Name = "BillHeaderId";
                parameters[0].Value = headerId;
                _reportExecutionService.SetExecutionParameters(parameters, "en-us");

                results = _reportExecutionService.Render(format,
                              deviceInfo,
                              out extension,
                              out encoding,
                              out mimeType,
                              out warnings,
                              out streamIDs);

                using (FileStream stream = File.OpenWrite(fileName))
                {
                    stream.Write(results, 0, results.Length);
                }
            }
            catch (Exception ex)
            {
                throw;
            }

            return fileName;
        }
        #endregion
    }
}