Show Attachments for an entity in Custom Webresource in Microsoft Dynamics CRM

Recently in our project we had a requirement where we needed the attachments for an entity in a custom webresource so that when the user clicks the attachment it downloads. For that first of all you would need to get the attachmentid by querying the annotations which are of type attachment for the entity. Once you have the attachmentid, you need to prepare the URL in the below format.

CRM 2011

—————————-

Xrm.Page.context.getServerUrl() + “/Activities/Attachment/download.aspx?AttachmentType={AttachmentType}&IsNotesTabAttachment=1&AttachmentId={” + attachmentId + “}”;

 

CRM 2013

————————-

Xrm.Page.context.getClientUrl() + “/Activities/Attachment/download.aspx?AttachmentType={AttachmentType}&IsNotesTabAttachment=1&AttachmentId={” + attachmentId + “}”;

{AttachmentType} – It has to be specified based on the type of the attachment.

However once you click, you might get an error invalid_wrpc_token.

To overcome this you need to add a key to your registry

1. Go to HKEY_LOCAL_MACHINE -> Software -> Microsoft -> MSCRM in CRM App server

2. Create a new D-WORD ‘IgnoreTokenCheck’. Set value to 1.

3. Restart IIS in the CRM app server.

 

Browse to CRM again and you should be able to download the attachment clicking the URL.

 

Hope this helps!

Role based view in Microsoft Dynamics CRM

Many of must be having the requirement of configuring role based views in CRM. For e.g – say a person with salesperson security role should have ‘Active contacts’ as the default view for the contact entity. Like wise there might be requirement where you would need to hide views based on security roles.

If you have the same requirement, you can explore my tool in codeplex. This tool meets all of the above requirements and does a lot apart from that. Please check out the documentation tab of this tool in codeplex which explains step by step as to how to use this tool

https://rolebasedviews.codeplex.com/

Hope this helps!

Execute custom SSRS report from Microsoft Dynamics CRM using custom code.

Recently we had the requirement in our project where the customer wanted to execute the Export To Excel feature for an editable grid that we implemented. The following in an overview of the requirement.

1. There is a custom jQuery Grid which shows a consolidated view to customer for a custom entity and its related child entities. The grid is an editable grid which was accomplished through a CRM webresource.

2. For that grid, the customer wanted an export to excel feature with the following functionality

  • Excel report in the same format as the grid is appearing
  • Excel report would give the user option to

                 1. Export selected rows
                 2. Export all records on the current page
                 3. Export All records.

To meet the above requirement we did the following.

1. Created a webresource in CRM with the same look and feel as OOB Export To Excel pop-up, with the options the user was looking for.

2. Created custom SSRS reports in the same format as the grid and deploy it to the report server

3. Set up a custom website (CRM 2013) or Web application under ISV folder (CRM 2011), call the website page with the user choices passed as the querystring and execute the SSRS report based using asp.net code in the page

The following is the code to render the ssrs report to the client in excel format. Add a reference to Microsoft.ReportViewer.WebForms dll in your code and include the namespace Microsoft.Reporting.WebForms.

// Parse the query strings. The querystrings would be basically your user choices
<Your logic goes here>

var reportViewer = new ReportViewer();

reportViewer.ProcessingMode = ProcessingMode.Remote;

// Set the username and password for connecting to reportserver
var domain = ConfigurationManager.AppSettings[“ServerDomain”].ToString();
var userName = ConfigurationManager.AppSettings[“ServerUsername”].ToString();
var password = ConfigurationManager.AppSettings[“ServerPassword”].ToString();
IReportServerCredentials irsc = new CustomReportCredentials(userName, password, domain);
reportViewer.ServerReport.ReportServerCredentials = irsc;

// set the reportserverurl. Typically it would be in the format of http://<servername>/ReportServer
reportViewer.ServerReport.ReportServerUrl = new Uri(ConfigurationManager.AppSettings[“ReportServerUrl”]);

// set the report path i.e the path where the report has been deployed in the report server. You do not need to specify .rdl extension here.
reportViewer.ServerReport.ReportPath = “/TestReportFolder/TestReport”;

//populate the parameter list if any paramter is required by the report from the querystring.
var paramList = new System.Collections.Generic.List<Microsoft.Reporting.WebForms.ReportParameter>();

//for example, here i am passing the loggedin userid and the report is expecting a parameter @UserId
paramList.Add(new ReportParameter(“UserId”, userid));

reportViewer.ServerReport.SetParameters(paramList);

// declare variables
string mimeType, encoding, extension, deviceInfo;
string[] streamids;
Microsoft.Reporting.WebForms.Warning[] warnings;
string format = “Excel”;

deviceInfo =
“<DeviceInfo>” +
“<SimplePageHeaders>True</SimplePageHeaders>” +
“</DeviceInfo>”;

byte[] bytes = reportViewer.ServerReport.Render(format, deviceInfo, out mimeType, out encoding, out extension, out streamids, out warnings);

// set the excel file name

var fileName = “ExcelFile.xls”;

Response.ContentType = “application/excel”;
Response.AddHeader(“Content-disposition”, “attachment;filename=” + fileName);

Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.End();

and following is the code for the class CustomReportCredentials

internal class CustomReportCredentials : IReportServerCredentials
{
private string _UserName;
private string _PassWord;
private string _DomainName;

public CustomReportCredentials(string UserName, string PassWord, string DomainName)
{
_UserName = UserName;
_PassWord = PassWord;
_DomainName = DomainName;
}

public System.Security.Principal.WindowsIdentity ImpersonationUser
{
get { return null; }
}

public ICredentials NetworkCredentials
{
get { return new NetworkCredential(_UserName, _PassWord, _DomainName); }
}

public bool GetFormsCredentials(out Cookie authCookie, out string user,
out string password, out string authority)
{
authCookie = null;
user = password = authority = null;
return false;
}
}

And the user would get a pop-up from the browser to download the excel file.

 

Hope this helps!

Get EntityTypeCode of an entity in CRM 2013 using JScript

Recently in my project i was required to get the entity type code of entity. A little bit of searching and I could find multiple ways to get them in jscript.

However the main problems were:

1. they were working only on entity forms

2. you would have to make a metadata call to get the objecttypecode

To overcome both of this in CRM 2013, you could use the Xrm.Internal namespace. Please note that this is not a documented api by Microsoft to be used by the end users.

However it exposes some utility functions which you can use. Of them i used the Xrm.Internal.getEntityCode(“<entitylogicalname>”) method.

For e.g – Xrm.Internal.getEntityCode(“account”) would return you 1.

And this is not restricted to Form only. You can use it anywhere in the context of CRM.

To explore other methods of this namespace, refer to this link –

https://debajitcrm.wordpress.com/2014/04/29/exploring-the-xrm-internal-namespace-in-crm-2013/

 

Hope this helps!