Saturday, November 5, 2016

Download Attachments in MSCRM using Javascript(HTML)



Recently we got a requirement to download the attachment files which are uploaded to a related records as Notes.
So, we created an HTML page which opens as a dialogue on click of a button. We need to retrieve the related annotations in HTML page or first retrieve them in button click javascript then pass them as a dialogue arguments. Then dynamically create an hyper links for each attachments in HTML, so that whenever user clicks that link, it should download the respective attachment file. Below is the code..!


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<html>
<head>
    <meta charset="utf-8">
</head>
<body>
    <title></title>
    <link href="../css/dialogprompt.css" rel="stylesheet" />
    <script src="../../ClientGlobalContext.js.aspx" type="text/javascript"></script>
    <script src="../scripts/jquery.js" type="text/javascript"></script>
    <script src="../scripts/json2.js" type="text/javascript"></script>
    <script src="../scripts/XrmServiceToolkit.js" type="text/javascript"></script>
    <script type="text/javascript">

        $(document).ready(function () {

            // Retrieve notes (OR) pass notes details from the parent window as a Dialogue Arguments
            var notes = retrieveNotesFromCurrentRecord();
            //var notes = window.getDialogArguments();

                if (notes.length > 0) {
                    for (var j = 0; j < notes.length; j++) {
                        $('#tbl_files tr:last ').after("<tr><td class='d0'>"
                            + (j + 1) + "</td><td class='d1'></td><td class='d2'>"
                            + notes[j].CreatedOn + "</td></tr>");

                        var fileLink = $("<a id='" + notes[j].AnnotationId + "' href='#'>"
                            + notes[j].FileName + "</a>");
                        fileLink.click(function () { download(this.id); });
                        $('#tbl_files tr:last .d1').append(fileLink);
                    }
                }
        });

        function retrieveNotesFromCurrentRecord() {

            // To-do: Logic to retrieve notes from the current record
        }

        function download(annotId) {

            var urlbase = window.parent.Xrm.Page.context.getClientUrl();
            var URL = urlbase + '/userdefined/edit.aspx?etc=5&id={' + annotId + '}';
            var docUrl;

            $.get(URL, function (data) {
                data = $.parseHTML(data);
                var securityTokenElement = $(data).find("[WRPCTokenUrl]");
                if (securityTokenElement) {
                    var securityTokenUrl = securityTokenElement.attr("WRPCTokenUrl");

                    if (securityTokenUrl) {
                        docUrl = urlbase + "/Activities/Attachment/download.aspx?AttachmentType=5&AttachmentId={"
                            + annotId + "}&IsNotesTabAttachment=undefined" + securityTokenUrl;
                    }
                }

                var element = document.createElement('a');
                element.setAttribute('href', docUrl);
                element.style.display = 'none';
                document.body.appendChild(element);
                element.click();
                document.body.removeChild(element);
            });
        }
        </script>
    <header>
        <div class="title" id="divTitle">Attchment Files</div>
        <div class="description" id="divDescription">Please click on the link to download the attachment.</div>
    </header>
    <div class="main">
        <div>
            <table id="tbl_files">
                <th>Sl. No</th>
                <th>Output File</th>
                <th>Uploaded On</th>
            </table>
        </div>
    </div>
</body>
</html>

Friday, January 15, 2016

Microsoft Dynamics CRM Errors And Solutions

Below are the errors faced in MSCRM and their solution.

1. Error while Publishing All Customization
    Solution: Try to publish individual components. You may face the same problem while publishing all the entities in bulk. In that case, publish only few entities once.


2. 'selectNodes is undefined or not an object' in Javascript
    Solution: Use getElementsByTagName("<tagName>") instead of selectNodes function.


3. Error creating/editing workflow process.

        Query Builder Error

        The specified field does not exist in Microsoft Dynamics CRM

    Solution: Open Deployment Manager and compare the installed CRM version with the version showing in front of your organization. If there is a mismatch, right click on the organization and upgrade.


4. Report Viewer Configuration Error in SSRS Report

The Report Viewer Web Control HTTP Handler has not been registered in the application's web.config file. Add <add verb="*" path="Reserved.ReportViewerWebControl.axd" type= "Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" /> to the system.web/httpHandlers section of the web.config file, or add <add name="ReportViewerWebControlHandler" preCondition="integratedMode" verb="*" path="Reserved.ReportViewerWebControl.axd" type="Microsoft.Reporting.WebForms.HttpHandler, Microsoft.ReportViewer.WebForms, Version=11.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" /> to the system.webServer/handlers section for Internet Information Services 7 or later.

    Solution: 
  • Open web.config file in reporting server and add yellow marked tag to system.web/httpHandlers section.
  • To add the Report Viewer Web Control HTTP Handle for your application. Please take the following steps:
          - Open IIS Manager
          - Expand Sites node and select your site
          - Double click Handler Mappings feature
            Click Add Managed Handler in the Actions Pane to add the handler


5. Maximum Controls Limit Exceeded

The dashboard that you are trying to save has more components than the maximum number allowed: 10. Remove some components and try again.

    Solution: Login to the CRM server and open Window Powershell as Administrator. Then run the below commands one by one.

Add-PSSnapin Microsoft.Crm.Powershell
$setting = Get-CrmSetting -SettingType DashboardSettings
$setting.MaximumControlsLimit = 10
Set-CrmSetting -Setting $setting



6. Error importing solution

The solution file is invalid. The compressed file must contain the following files at its root: solution.xml, customizations.xml, and [Content_Types].xml. Customization files exported from previous version of Microsoft Dynamics CRM are not supported.



    Solution: We need to open the zip file and change the customizations.xml file.
Remove the nodes <SavedQueries>... </SavedQueries> and <visualization>... </visualization> and re-import the solution.



Tuesday, June 2, 2015

Auto Number: Record Locking in MSCRM Plugin

    We came across the scenario where custom Auto Numbering plugin generates the duplicate numbers for an entity when two users create the records simultaneously. The reason behind this is because dynamics only locks a record inside a transaction if we update it. However what we are doing here is retrieving the record and then updating it. So effectively the record will be locked just at the end of the plugin execution which is not very helpful indeed.
Below are the steps to illustrate this:

1.   First we are retrieving the config record which contains the prefix, last generated number and other details

2.   Then we are reading the last updated number value and other config details

3.   Then we are applying the logic to build the number

4.   Finally, we are updating both the config(to maintain the last generated number) and the entity record in context with the required values.

So it is possible that two plugins is being executed and both those plugins performed step 1 above (so they both retrieved a config record). So they both will have the same value for the “Last Number” field. Then they will both apply the logic in step 2 and 3. Finally the first plugin will execute step 4 and update the config record (this will lock the config record)  but it will finish execution immediately which will unlock the config record. So now the second plugin is executing step 4 but since it already has retrieved the old value so the update logic will be the same and it will override the config record with the same number instead of incrementing it.

So, we should update the logic in the plugin above to the following:

1.   First, do a fake update to the required config record. We don’t have to change  any values and we can just do       service.update(configRecord);

2.   Then we apply step 1, 2, 3 and 4 from the above logic.

Below is the sample code to show how to lock  the configuration record:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Get the record for which Auto Number to be generated
Entity  targetEntityRecord = (Entity)context.InputParameters["Target"];

// Fetch the configuration record
Entity configRecord = GetConfigurationRecord(service);

// If configuration record found, then do a fake update
if (configRecord.Id != Guid.Empty)
  service.Update(configRecord);
else
  throw new  InvalidPluginExecutionException("Config record not found.");

// Get the last generated number from configuration record
int  lastNumber =  Int32.Parse(configRecord.Attributes["new_lastnumber"].ToString());

// Increment the last generated number
lastNumber++;

// Build a number with your own logic
string autoNumber = string.Format("{0}-{1}", "PREFIX" , lastNumber);

// Set new generated number to both target entity and Configuration record
targetEntityRecord.Attributes.Add("new_name", autoNumber);
configRecord.Attributes["new_lastnumber"] = lastNumber;

// Update the Configuration record
service.Update(configRecord);

I would like to thank my friend Shakarchi Ethra for describing this Dynamics behaviour.

Friday, July 11, 2014

Integrate Twitter widget with MS CRM

You can see all the tweets of your twitter account in MS CRM. Below are the steps to be followed to integrate Twitter widget in MSCRM.

- Log on to your Twitter account.
- Click on Settings symbol (1) in twitter and select Settings.
- Select 'Widgets' in left panel.
- Click on 'Create new' button.
- Select 'User timeline' under 'Choose a timeline source' option.



A) Setting Widget properties in Twitter.

- Leave Username(2) as auto populated with your twitter user name, select 'Options'(3), 'Height'(4), 'Theme'(5) and 'Link color'(6) as your wish.
- Any changes you made in these properties are reflecting under Preview(8) in right side.
- Click on 'Create widget'
- Copy an HTML code(7) generated just below the Preview.
- Log on to MS CRM organization.
- Create one HTML webresource by adding the copied HTML code inside <body> tag.
- Create/Edit a dashboard to add newly created HTML webresource as a component in that dashboard.
- Save, Close and publish the dashborad.

- Go to that dashboard in CRM and you should be able to see all your tweets on CRM dashboard as in below image.


B) Twitter widget integrated with MS CRM dashboard

Thursday, June 19, 2014

Export a CSV without headers in SSRS

        In SSRS report we have an option to export report to many formats like Excel, CSV, PDF, Word and so on. In case of CSV export, we will get all the column headers in exported file. Some time we need CSV without column header. Below is the solution to get this done.

à Open rsreportserver.config file under C:\Program Files\Microsoft SQL Server\{INSTANCE}\Reporting Services\ReportServer in reporting server.
à Add below mentioned XML tags after <Extension Name="CSV"... />
<Extension Name="CSV (No Header)" Type="Microsoft.ReportingServices.Rendering.DataRenderer.CsvReport,Microsoft.ReportingServices.DataRendering">
<OverrideNames>
   <Name Language="en-us"> CSV No Header</Name>
</OverrideNames>
<Configuration>
   <DeviceInfo>
      <NoHeader>true</NoHeader>
   </DeviceInfo>
</Configuration>
</Extension>

à Save the file.

 After this changes, run one report. Under export option in that report, you must be able to see the option 'CSV No Header' as in below image(a). And exported data will not contains any headers.


CSV No Header
a) Export to CSV with no header