Thursday, 26 May 2016

05:03 - No comments

Adding New Button to Ribbon at form level and grid level for custom entity in CRM || MS CRM

Adding New Button to Ribbon at form level and grid level for custom entity in CRM



1. Export the solution to which we wants to create custom button
2. Extract the solution and open the "custmizations.xml" file to edit.
3. Find "RibbonDiffXml" tag for the particular custom entity which you want to add button.
 Now replace the "RibbonDiffXml" tag with the following code, it will adds a button to the chosen entity at grid level and form level.



<RibbonDiffXml>
        <CustomActions>

<!-- This to add button in formlevel after exportdata button , CI_formlevel is any unique Id, sequence Number is the position of the button,75 will shows button before Export Excel,you can find sequence numbers of system buttons in SDK-->

          <CustomAction Id="CI_formlevel" Location="Mscrm.Form.new_entityname.MainTab.ExportData.Controls._children" Sequence="75">
            <CommandUIDefinition>
              <Button Id="B_formbutton" Command="Cmd_JavaScript1" LabelText="Custom Button" ToolTipTitle="My Form Button" ToolTipDescription="Form Level Button" TemplateAlias="o1" Image16by16="$webresource:Icon_imgSmall" Image32by32="$webresource:Icon_imgMedium" />
            </CommandUIDefinition>
          </CustomAction>


<!-- this to add button at gridlevel -->

          <CustomAction Id="CI_Gridlevel" Location="Mscrm.HomepageGrid.new_entityname.MainTab.ExportData.Controls._children" Sequence="75">
            <CommandUIDefinition>
              <Button Id="B_GIncident" Command="Cmd_JavaScript1" LabelText="Grid Button" ToolTipTitle="Grid Button" ToolTipDescription="Grid Level button" TemplateAlias="o1" Image16by16="$webresource:Icon_imgSmall" Image32by32="$webresource:Icon_imgMedium" />
            </CommandUIDefinition>
          </CustomAction>
        </CustomActions>
        <Templates>
          <RibbonTemplates Id="Mscrm.Templates"></RibbonTemplates>
        </Templates>
        <CommandDefinitions>
          <CommandDefinition Id="Cmd_JavaScript1">
            <EnableRules>
              <EnableRule Id="Mscrm.Enabled" />
            </EnableRules>
            <DisplayRules />
            <Actions>

<!-- call java script library function -->

              <JavaScriptFunction Library="$webresource:new_jscrlib" FunctionName="myfunction"></JavaScriptFunction>
            </Actions>
          </CommandDefinition>
        </CommandDefinitions>
        <RuleDefinitions>
          <TabDisplayRules />
          <DisplayRules />
          <EnableRules />
        </RuleDefinitions>
        <LocLabels />
      </RibbonDiffXml>

Monday, 23 May 2016

00:31 - 2 comments

EXPORT REPORT AS PDF AND ATTACH THE SAME TO EMAIL ACTIVITY || MS CRM

Implementation Steps:
            Step 1: Create SSRS report.
            Step 2: Create Custom workflow to access those report.
            Step 3: Convert reports into PDF file.
            Step 4: Create On-Demand System workflow and then create Email Template.
            Step 5: Add the Custom workflow add give the create Email template as an input.

            Step 6: Activate the workflow and trigger it.

Step 1: Creating SSRS report 
·         From the Start Menu à Select All Programs à Microsoft SQL Server 2012 (or 2008) à Click on  à SQL Server Data Tools for Visual Studio 2012.
·         File à New à Project

             Select "Report Server Project" from the list à Enter a project Name and specify project Location à click OK

·         In Solution Explorer, right-click Reports, point to Add, and click New Item. If the Solution Explorer window is not visible, from the View menu, click Solution Explorer.
·         In the Add New Item window, click Report.


             In Name, type Reports.rdl and click Add

·         Report Designer opens and displays the new .rdl file in Design view.
Step 2: Deploying the Report in CRM
·           Deploying Report in CRM is quite an easy task like we use to SSRS reporting portal. Go to Module menu, Tools and Reports.


    
     Click on new Button in reports, a popup will open for new report.


Select “Existing report” from Report type drop down and select report and click on “save”, your report will be added to reports in CRM.



After add the Report in CRM.




·         From the Start Menu àSelect All Programs àMicrosoft Visual Studio (or 2008).
·         File àNew àProject. Select "Class Library" from the list àEnter a project Name and specify project Locationà click OK
·         In Solution Explorer, right-click Referencesà Add References then add the CRM references

·         In same way to add the Service Reference also.
·        Write a code to get the Report from CRM and convert that report as a PDF file.
      Add the below code to convert the PDF file.
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Workflow;
using System.Linq;
using System.Net;
using System.ServiceModel;
using Microsoft.Xrm.Client.Services;
using System.IO;


namespace CustomReport
{
public class Class1 : CodeActivity
{
[Input("E-Mail")]
[ReferenceTarget("email")]
public InArgument<EntityReference> Email { get; set; }

protected override void Execute(CodeActivityContext executionContext)
{
try
{
IWorkflowContext context = executionContext.GetExtension<IWorkflowContext>();
IOrganizationServiceFactory serviceFactory = executionContext.GetExtension<IOrganizationServiceFactory>();
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

Guid accountId = context.PrimaryEntityId;
byte[] result = null;

ReportExecutionService rs = new ReportExecutionService();
rs.Credentials = new NetworkCredential("USER NAME", "PASSWORD", "DOMAIN");
rs.Url = "http://(REPORT SERVER IP ADDRESS)/ReportServer/ReportExecution2005.asmx";

string reportPath = "/Account Details";           
string format = "PDF";
string historyID = null;
string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
string encoding;
string mimeType;
string extension;
Warning[] warnings = null;
string[] streamIDs = null;
ExecutionInfo execInfo = new ExecutionInfo();
ExecutionHeader execHeader = new ExecutionHeader();
rs.ExecutionHeaderValue = execHeader;
execInfo = rs.LoadReport(reportPath, historyID);

String SessionId = rs.ExecutionHeaderValue.ExecutionID;

result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);

                //Create email activity
                Entity attachment = new Entity("activitymimeattachment");
                attachment["objectid"] = Email.Get<EntityReference>(executionContext);
                attachment["objecttypecode"] = "email";
                attachment["filename"] =
                attachment["subject"] = "Reports.pdf";
                attachment["body"] = System.Convert.ToBase64String(result);

try
{
service.Create(attachment);
}
catch (Exception ex)
{
throw new Exception(string.Format("Error creating attachment - {0}", ex.Message));
}

try
{
service.Execute(new SendEmailRequest()
{
EmailId = Email.Get<EntityReference>(executionContext).Id,
IssueSend = true,
TrackingToken = string.Empty
});
}
catch (Exception ex)
{
throw new Exception(string.Format("Error sending email - {0}", ex.Message));
}

              
}
catch (Exception err)
{
throw new Exception("Exception"+err);
}
}
}
}

·         Build the solution and deploy the solution into CRM using plugin registration tool.

     Step 3: Create System Workflow:

·         From Microsoft Dynamics CRM à Select Setting àProcesses Click



·         Then Create give a valid Name and select type as workflow and choose which entity it should be trigger.
·         Add a new step as create record. Then click on set properties button.

Choose an Email and create an Email template


      In Email template give “Form” address, “To” address and body of the text, then Save and Close the Email template.
·         After that add the custom workflow.


Then click on “Set Properties”


           Give input as create Email then “Save and Close”
            After that activate the workflow.


Finally when we trigger the on demand workflow the Email will generate and it have the attachment of the Report as a PDF file.