locked
CRM Reports Export as word RRS feed

  • Question

  • Can i export the report in plugin, I have a report which accepts few parameters,

    I have a requirement where i need to export this report into work and put in SharePoint

    Can any one suggest the best way to do it

    Thanks in Advance


     
    Thursday, April 19, 2012 1:38 PM

Answers

  • Hi,

    yes you can render report and export it in word document.

    refer for the same http://social.microsoft.com/Forums/sl-SI/crmdevelopment/thread/68792a05-8706-4f87-90fe-c625552dc404


    Mahain : Check My Blog
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Thursday, April 19, 2012 2:26 PM
    Moderator
  • If you try manually running the report, Is it working?

    Following is a working plugin code which you can modify according to you requirement:-

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using SSR.ReportService2005;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using System.IO;
    using System.Web.Services.Protocols;
    using SSR.ReportExecution;
    using Microsoft.Crm.Sdk.Query;
    namespace SnapShotReport
    {
        public class GenerateReport : IPlugin
        {
            public void Execute(IPluginExecutionContext context)
            {
                CrmAuthenticationToken token = new CrmAuthenticationToken();
                token.AuthenticationType = 0;
                token.OrganizationName = "UAT";
                CrmService service = new CrmService();
                service.Url = "http://Servername/mscrmservices/2007/crmservice.asmx";
                service.CrmAuthenticationTokenValue = token;
                service.Credentials = System.Net.CredentialCache.DefaultCredentials;
                WhoAmIRequest userRequest = new WhoAmIRequest();
                WhoAmIResponse user = (WhoAmIResponse)service.Execute(userRequest);
                try
                {
                    //Guid ChangeID = (Guid)((Microsoft.Crm.Sdk.Key)((Microsoft.Crm.Sdk.DynamicEntity)context.InputParameters.Properties["Target"]).Properties[context.PrimaryEntityName + "id"]).Value;
                    Guid ChangeID = new Guid(context.OutputParameters.Properties["id"].ToString());
                    TargetRetrieveDynamic target = new TargetRetrieveDynamic();
                    target.EntityId = ChangeID;
                    target.EntityName = "new_ticket";
                    // Define the entity attributes (database table columns) that are
                    // to be retrieved.
                    ColumnSet cols = new ColumnSet();
                    cols.AddColumns(new string[] { "new_ticketid", "new_changeticketnumber" });
                    // Create a retrieve request object.
                    RetrieveRequest retrieve = new RetrieveRequest();
                    retrieve.Target = target;
                    retrieve.ColumnSet = cols;
                    retrieve.ReturnDynamicEntities = true;
                    // Create a response reference and execute the retrieve request.
                    RetrieveResponse response = (RetrieveResponse)service.Execute(retrieve);
                    DynamicEntity retrievedEntity = (DynamicEntity)response.BusinessEntity;
                    string oName = (String)retrievedEntity.Properties["new_changeticketnumber"];
                    ReportExecutionService rs = new ReportExecutionService();
                    // rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
                    rs.Credentials = new System.Net.NetworkCredential("crmadmin", "_htacrm03", "hta");
                    rs.Url = "http://ServerName/reportserver/ReportExecution2005.asmx";
                    // Render arguments
                    byte[] result = null;
                    // Make sure you use your correct org database name of the following line
                    string reportPath = "/UAT_MSCRM/Snapshot";
                    string format = "PDF";
                    string historyID = null;
                    string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
                    // Prepare report parameter.
                    SSR.ReportExecution.ParameterValue[] parameters = new SSR.ReportExecution.ParameterValue[1];
                    parameters[0] = new SSR.ReportExecution.ParameterValue();
                    parameters[0].Name = "CTID";
                    parameters[0].Value = ChangeID.ToString();
                    // SSR.ReportExecution.DataSourceCredentials[] credentials = null;
                    // string showHideToggle = null;
                    string encoding;
                    string mimeType;
                    string extension;
                    SSR.ReportExecution.Warning[] warnings = null;
                    //SSR.ReportExecution.ParameterValue[] reportHistoryParameters = null;
                    string[] streamIDs = null;
                    ExecutionInfo execInfo = new ExecutionInfo();
                    ExecutionHeader execHeader = new ExecutionHeader();
                    rs.ExecutionHeaderValue = execHeader;
                    execInfo = rs.LoadReport(reportPath, historyID);
                    rs.SetExecutionParameters(parameters, "en-us");
                    String SessionId = rs.ExecutionHeaderValue.ExecutionID;
                    Console.WriteLine("SessionID: {0}", rs.ExecutionHeaderValue.ExecutionID);
                    try
                    {
                        result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
                        execInfo = rs.GetExecutionInfo();
                        Console.WriteLine("Execution date and time: {0}", execInfo.ExecutionDateTime);
                        string encodedData = System.Convert.ToBase64String(result);
                        //Save pdf report as an attachment to Notes entity
                        annotation note = new annotation();
                        note.subject = "Snapshot Report has been successfully created for Ticket No: " + oName;
                        note.filename = oName + ".pdf";
                        note.mimetype = "application/pdf";
                        note.documentbody = encodedData;
                        note.objectid = new Lookup();
                        note.objectid.type = "new_ticket";
                        note.objectid.Value = ChangeID;
                        note.objecttypecode = new EntityNameReference();
                        note.objecttypecode.Value = "new_ticket";
                        //Create Notes
                        Guid annotationId = service.Create(note);
                    }
                    catch (SoapException e)
                    {
                        throw new Exception(e.Detail.InnerText);
                    }
                }
                catch (SoapException error)
                {
                    throw new InvalidPluginExecutionException(error.Detail.SelectSingleNode("//description").InnerText);
                }
            }
        }
    }


    Regards Faisal

    Wednesday, April 25, 2012 12:50 PM

All replies

  • Hi,

    yes you can render report and export it in word document.

    refer for the same http://social.microsoft.com/Forums/sl-SI/crmdevelopment/thread/68792a05-8706-4f87-90fe-c625552dc404


    Mahain : Check My Blog
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Thursday, April 19, 2012 2:26 PM
    Moderator
  • I wanted to do this in Plugin

    I am wondering how can i handle moving the plugin from one env to other

    And also what should be the datasoucename

    Thursday, April 19, 2012 2:30 PM
  • Hi Varun,

    you can do in plugin it won't create issue, if you are having concern about configuration you can create a custom entity in CRM to store report configuration and read that in plugin.

    you need to use MSCRM_DataSource


    Mahain : Check My Blog
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Thursday, April 19, 2012 2:48 PM
    Moderator
  • I tried with that code but it give me an exceptione weird!!!

    System.Web.Services.Protocols.SoapException: The data source 'MSCRM_FetchDataSource' cannot be found.

    I even tried with MSCRM_DataSource but still get the same issue

    Wednesday, April 25, 2012 10:01 AM
  • Is this a fetch report ?

    Mahain : Check My Blog
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, April 25, 2012 11:22 AM
    Moderator
  • Yes it is a fetch report,

    Even i tried with sql report but still get the same issue, For sql report i used 'MSCRM_DataSource'

    Wednesday, April 25, 2012 12:18 PM
  • If you try manually running the report, Is it working?

    Following is a working plugin code which you can modify according to you requirement:-

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using SSR.ReportService2005;
    using Microsoft.Crm.Sdk;
    using Microsoft.Crm.SdkTypeProxy;
    using System.IO;
    using System.Web.Services.Protocols;
    using SSR.ReportExecution;
    using Microsoft.Crm.Sdk.Query;
    namespace SnapShotReport
    {
        public class GenerateReport : IPlugin
        {
            public void Execute(IPluginExecutionContext context)
            {
                CrmAuthenticationToken token = new CrmAuthenticationToken();
                token.AuthenticationType = 0;
                token.OrganizationName = "UAT";
                CrmService service = new CrmService();
                service.Url = "http://Servername/mscrmservices/2007/crmservice.asmx";
                service.CrmAuthenticationTokenValue = token;
                service.Credentials = System.Net.CredentialCache.DefaultCredentials;
                WhoAmIRequest userRequest = new WhoAmIRequest();
                WhoAmIResponse user = (WhoAmIResponse)service.Execute(userRequest);
                try
                {
                    //Guid ChangeID = (Guid)((Microsoft.Crm.Sdk.Key)((Microsoft.Crm.Sdk.DynamicEntity)context.InputParameters.Properties["Target"]).Properties[context.PrimaryEntityName + "id"]).Value;
                    Guid ChangeID = new Guid(context.OutputParameters.Properties["id"].ToString());
                    TargetRetrieveDynamic target = new TargetRetrieveDynamic();
                    target.EntityId = ChangeID;
                    target.EntityName = "new_ticket";
                    // Define the entity attributes (database table columns) that are
                    // to be retrieved.
                    ColumnSet cols = new ColumnSet();
                    cols.AddColumns(new string[] { "new_ticketid", "new_changeticketnumber" });
                    // Create a retrieve request object.
                    RetrieveRequest retrieve = new RetrieveRequest();
                    retrieve.Target = target;
                    retrieve.ColumnSet = cols;
                    retrieve.ReturnDynamicEntities = true;
                    // Create a response reference and execute the retrieve request.
                    RetrieveResponse response = (RetrieveResponse)service.Execute(retrieve);
                    DynamicEntity retrievedEntity = (DynamicEntity)response.BusinessEntity;
                    string oName = (String)retrievedEntity.Properties["new_changeticketnumber"];
                    ReportExecutionService rs = new ReportExecutionService();
                    // rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
                    rs.Credentials = new System.Net.NetworkCredential("crmadmin", "_htacrm03", "hta");
                    rs.Url = "http://ServerName/reportserver/ReportExecution2005.asmx";
                    // Render arguments
                    byte[] result = null;
                    // Make sure you use your correct org database name of the following line
                    string reportPath = "/UAT_MSCRM/Snapshot";
                    string format = "PDF";
                    string historyID = null;
                    string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
                    // Prepare report parameter.
                    SSR.ReportExecution.ParameterValue[] parameters = new SSR.ReportExecution.ParameterValue[1];
                    parameters[0] = new SSR.ReportExecution.ParameterValue();
                    parameters[0].Name = "CTID";
                    parameters[0].Value = ChangeID.ToString();
                    // SSR.ReportExecution.DataSourceCredentials[] credentials = null;
                    // string showHideToggle = null;
                    string encoding;
                    string mimeType;
                    string extension;
                    SSR.ReportExecution.Warning[] warnings = null;
                    //SSR.ReportExecution.ParameterValue[] reportHistoryParameters = null;
                    string[] streamIDs = null;
                    ExecutionInfo execInfo = new ExecutionInfo();
                    ExecutionHeader execHeader = new ExecutionHeader();
                    rs.ExecutionHeaderValue = execHeader;
                    execInfo = rs.LoadReport(reportPath, historyID);
                    rs.SetExecutionParameters(parameters, "en-us");
                    String SessionId = rs.ExecutionHeaderValue.ExecutionID;
                    Console.WriteLine("SessionID: {0}", rs.ExecutionHeaderValue.ExecutionID);
                    try
                    {
                        result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
                        execInfo = rs.GetExecutionInfo();
                        Console.WriteLine("Execution date and time: {0}", execInfo.ExecutionDateTime);
                        string encodedData = System.Convert.ToBase64String(result);
                        //Save pdf report as an attachment to Notes entity
                        annotation note = new annotation();
                        note.subject = "Snapshot Report has been successfully created for Ticket No: " + oName;
                        note.filename = oName + ".pdf";
                        note.mimetype = "application/pdf";
                        note.documentbody = encodedData;
                        note.objectid = new Lookup();
                        note.objectid.type = "new_ticket";
                        note.objectid.Value = ChangeID;
                        note.objecttypecode = new EntityNameReference();
                        note.objecttypecode.Value = "new_ticket";
                        //Create Notes
                        Guid annotationId = service.Create(note);
                    }
                    catch (SoapException e)
                    {
                        throw new Exception(e.Detail.InnerText);
                    }
                }
                catch (SoapException error)
                {
                    throw new InvalidPluginExecutionException(error.Detail.SelectSingleNode("//description").InnerText);
                }
            }
        }
    }


    Regards Faisal

    Wednesday, April 25, 2012 12:50 PM