web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to Run the CRM Report through SDK

(0) ShareShare
ReportReport
Posted on by 390

Hi,

I want to connect to CRM Organization/API through SDK using my user id and run the SSRS report and download as PDF file. Is there any way to do this?

Thanks.

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at
    RE: How to Run the CRM Report through SDK

    We do this exact thing with a custom workflow plugin Code below:

    using Microsoft.Xrm.Sdk;
    using Microsoft.Xrm.Sdk.Workflow;
    using System;
    using System.Activities;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    using System.Collections;
    using Microsoft.Crm.Sdk.Messages;
    using System.Web;
    
    namespace Plugins
    {
        public class AttachAndSendReportServerPDF : CodeActivity
        {
            [RequiredArgument]
            [Input("Source Email")]
            [ReferenceTarget("email")]
            public InArgument<EntityReference> SourceEmail { get; set; }
    
            [RequiredArgument]
            [Input("Send Email When Complete?")]
            [Default("True")]
            public InArgument<bool> SendOnComplete { get; set; }
    
            [RequiredArgument]
            [Input("Report Server")]
            [Default("https://URL.ServerLocation.Com")]
            public InArgument<string> ReportServer { get; set; }
    
            [RequiredArgument]
            [Input("Report Directory")]
            [Default("")]
            public InArgument<string> ReportDirectory { get; set; }
    
            [Input("Parameter Name")]
            [Default("Id of Entity")]
            public InArgument<string> ParameterName { get; set; }
            
            [Input("Parameter is Entity Reference? (Current Record Guid)")]
            [Default("True")]
            public InArgument<bool> ParameterType { get; set; }
    
            [Input("Parameter Value (String - If Above Is False)")]
            [Default("")]
            public InArgument<string> ParameterStringValue { get; set; }
            
            protected override void Execute(CodeActivityContext _context)
            {
                string RSLink = "";
                try
                {
                    IOrganizationServiceFactory factory = _context.GetExtension<IOrganizationServiceFactory>();
                    IWorkflowContext _wfContext = _context.GetExtension<IWorkflowContext>();
                    IOrganizationService _service = factory.CreateOrganizationService(_wfContext.UserId);
    
                    RSLink = GenerateRSLink(_context, _wfContext);
                    string Base64 = GetLinkContents(RSLink);
                    CreateEmailAttachment(Base64, _service, _context);
                    if (_context.GetValue(SendOnComplete))
                        SendEmail(_service, _context);
                }
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(ex.Message + " : " + RSLink);
                }
            }
    
            protected void SendEmail(IOrganizationService _service, CodeActivityContext _context)
            {
                EntityReference Email = _context.GetValue(SourceEmail);
    
                SendEmailRequest req = new SendEmailRequest();
                req.EmailId = Email.Id;
                req.TrackingToken = "";
                req.IssueSend = true;
                SendEmailResponse res = (SendEmailResponse)_service.Execute(req);
            }
    
            protected void CreateEmailAttachment(string Base64, IOrganizationService _service, CodeActivityContext _context)
            {
                Entity pdf = new Entity("activitymimeattachment");
                pdf["subject"] = "SSRS - Report";
                pdf["filename"] = "ssrsReport.pdf";
                pdf["body"] = Base64;
                pdf["mimetype"] = "application/pdf";
                pdf["objectid"] = _context.GetValue(SourceEmail);
                pdf["objecttypecode"] = "email";
    
                _service.Create(pdf);
            }
    
            protected string GetLinkContents(string URL)
            {
                WebClient client = new WebClient();
                client.UseDefaultCredentials = true;
    			//You may or maynot need to do this. Depends on your requirements
                //client.Credentials = new NetworkCredential([Username], [Password], [Domain]);
    
                byte[] bytes = client.DownloadData(URL);
                client.Dispose();
    
                return Convert.ToBase64String(bytes);
            }
    
            protected string GenerateRSLink(CodeActivityContext _context, IWorkflowContext _wfContext)
            {
                string FullReportPath = string.Format("{0}/ReportServer?{1}&rs:Command=Render&rs:Format=PDF", _context.GetValue(ReportServer), Uri.EscapeUriString(_context.GetValue(ReportDirectory)));
                string Param_Name = _context.GetValue(ParameterName);
                if (Param_Name != null && Param_Name.Length > 0)
                {
                    if (_context.GetValue(ParameterType))
                    {
                        Guid Param_Id = _wfContext.PrimaryEntityId;
                        FullReportPath += string.Format("&{0}={1}", Param_Name, Param_Id.ToString());
                    }
                    else
                    {
                        string Param_String = _context.GetValue(ParameterStringValue);
                        FullReportPath += string.Format("&{0}={1}", Param_Name, Param_String);
                    }
                }
                return FullReportPath;
            }
        }
    }
    
  • Suggested answer
    Henry J. Profile Picture
    5,237 on at
    RE: How to Run the CRM Report through SDK

    To the best of my knowledge, you can't do this on a CRM Online scenario with a plugin (because of their isolation mode and SSRS Online Servers limitations).

    Does your custom workflow work online @Stephen.King?

    You can however achieve this with a JavaScript using unsupported code (see example here: www.c-sharpcorner.com/.../generate-pdf-file-using-javascript-in-microsoft-dynamics-crm-2015)

  • Aric Levin - MVP Profile Picture
    30,190 Moderator on at
    RE: How to Run the CRM Report through SDK

    Is this for a separate console/windows/web application using SDK, and not from CRM environment directly?

  • Community Member Profile Picture
    on at
    RE: How to Run the CRM Report through SDK

    It works on prem on 2016 and 365. Unsure if it works online haven't had chance to try it out.

  • Community Member Profile Picture
    on at
    RE: How to Run the CRM Report through SDK

    This is a workflow plug-in, it can be used on prem. In the couple of use cases I've used it. We have a process which does a bunch of data updates then sends an email with the data PDF attached when done.

  • Suggested answer
    Jayakumar Profile Picture
    390 on at
    RE: How to Run the CRM Report through SDK

    Thanks Stephen. I cannot use this in my case. But I learnt something new.

  • Jayakumar Profile Picture
    390 on at
    RE: How to Run the CRM Report through SDK

    @Stephen,

    Where to pass the rdl file name. My report is custom report which is uploaded in CRM. So what would be the path of the report directory. OrganizationName_MSCRM\CustomReports?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
UllrSki Profile Picture

UllrSki 2

#1
Community Member Profile Picture

Community Member 2

#3
SC-08081331-0 Profile Picture

SC-08081331-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans