Friday, 13 March 2015

Zip and UnZip the folder using C# [.net 4.5 feature]

I used to use some third party dll for zipping the folders. Now there is no need to use other dll, .net 4.5 gave a namespace for it.

using System.IO.Compression;

Here is the code of creating a zip folder and extracting the content from zipped folder

ZipFile.CreateFromDirectory(@"D:\Contents", @"D:\Contents.zip");

ZipFile.ExtractToDirectory(@"D:\Contents.zip", @"D:\Contents");

--
Happy Coding
Gopinath

Wednesday, 11 March 2015

Version compatibility

Version compatibility

Solutions exported from an earlier version of CRM can be imported into later versions as shown in the following chart.
 
 
For more information, Check this
 
--
Happy CRM'ing
Gopinath

MS CRM Version

How to know MS CRM Version

We can know the CRM version, roll up and service pack by checking the version number of CRM.


The first digit indicates the application version(CRM 5 - 2011, 6 - 2013, 7 - 2015)
The second digit indicates the service pack level.
The third digit indicates the update rollup.

So the above image is from an organization on RTM 2013 update rollup 2


This image is from organization RTM CRM 2015 with out any rollups.

--
Happy CRM'img
Gopinath.

Tuesday, 10 March 2015

Retrieve EntityImage URL of the record in CRM 2013 and 2015

Entity Image URL

CRM gives us the relative URL of the entity image which can used directly.

Here is the code of it.

string strEntityImageURL = string.Empty;
Entity entObject = objService.Retrieve("contact", new Guid("00000000-0000-0000-0000-000000000000"), new ColumnSet(new string[] { "entityimage_url" }));
if (entObject != null && entObject.Contains("entityimage_url"))
{
    strEntityImageURL = entObject.Attributes["entityimage_url"].ToString();
}

--
Happy CRM'img
Gopinath


 

Restrict creating Notes on Inactive records - Zero Code

Today, I got an complaint from the customer saying that they were able to create Notes on inactive case record.

Wrote a simple system workflow to handle this.



--
Happy CRM'img
Gopinath

Monday, 9 March 2015

Read images of Notes and printing selected images in MS CRM

Read image from Notes and show them on HTML Page

Hello, today I got a requirement to show images attached to the notes of the entity and client will select some of them and should be able to print them.

I have created  a html webresource to read the images of the notes and given check box beside to select and issue print command.

Here is the POC of it.

<html>
<head>
    <title></title>
    <style type="text/css">
        .tdClass {
            border: 1px solid black;
            width: 33%;
            text-align: center;
            padding: 12px;
            font-family: sans-serif;
            font-size: medium;
            vertical-align: central;
            background-color: aliceblue;
        }
    </style>
    <script src="ClientGlobalContext.js.aspx"></script>
    <script src="../WebResources/new_SDK.REST.js" type="text/javascript"></script>
    <script type="text/javascript">
        //check if document is loaded or not
        var imgControl = document.createElement("IMG");
        var tableId = "salesNotes";
        var vRecordCount = 0;
        function byId(e) { return document.getElementById(e); }
        function newEl(tag) { return document.createElement(tag); }
        function newTxt(txt) { return document.createTextNode(txt); }
        //Check if documented loaded fully
        document.onreadystatechange = function () {
            if (document.readyState == "complete") {
                var vResult = GetNotesImages();
            }
        }
        //this function is used to get image from notes
        function GetNotesImages() {
            //get regarding object id
            var regardingObjectId = window.parent.Xrm.Page.data.entity.getId();
            //assign notes entity name
            var entitySchemaName = "Annotation";
            var odataQuery = "select=AnnotationId,DocumentBody,MimeType,NoteText&" + "$filter=ObjectId/Id eq guid'" + regardingObjectId + "' and IsDocument eq true and startswith(MimeType,'image/') ";
            //call retrieveMultipleRecords method in SDK.REST javascript script library
            SDK.REST.retrieveMultipleRecords(entitySchemaName, odataQuery, getnotesImagesCallback, function (error) { alert(error.message); }, function () { });
        }

        //process callbanck result
        function getnotesImagesCallback(resultSet) {
            // CreateTable(resultSet);
            if (resultSet != null && resultSet != 'undefined') {
                vRecordCount = resultSet.length;
                constructTable(resultSet);
            }
        }
        // Create a table with the result
        function constructTable(notesResults) {
            var divTarget = document.getElementById('divDynamicContent');
            var txtNoRows = parseInt(notesResults.length);
            var tbl = document.createElement("table");
            tbl.id = 'tblDynamic';
            var tblHead = ["S.no", "Description", "Image", "Select"]
            var newCell;
            var newTHEAD = tbl.createTHead();
            newTHEAD.id = "mytblHead";
            var newRow = newTHEAD.insertRow(-1);
            for (var i = 0; i < tblHead.length; i++) {
                newCell = newRow.insertCell(i);
                newCell.innerHTML = tblHead[i];
            }
            var tblBody = document.createElement("tbody");
            // creating all cells
            for (var j = 0; j < txtNoRows; j++) {
                // creates a table row
                var row = document.createElement("tr");
                row.id = "tr_" + j;
                for (var i = 0; i < 4; i++) {
                    var cell = document.createElement("td");
                    var cellText;
                    if (i == 0) {
                        cellText = document.createElement('label')
                        cellText.htmlFor = "sno_" + j;
                        cellText.style["width"] = "100px";
                        cellText.appendChild(document.createTextNode(j + 1));
                    }
                    else if (i == 1) {
                        cellText = document.createElement('label')
                        cellText.htmlFor = "id_" + j;
                        cellText.style["width"] = "400px";
                        if (notesResults[j].NoteText != undefined) {
                            cellText.appendChild(document.createTextNode(notesResults[j].NoteText));
                        }
                        else {
                            cellText.appendChild(document.createTextNode(""));
                        }
                    }
                    else if (i == 2) {
                        //cellText = document.createElement("<input type='radio' name='radiotest'id='idFirstInputRadio' value='first choice'>");
                        var cellText = document.createElement("img");
                        cellText.src = "data:" + notesResults[j].MimeType + ";base64," + notesResults[j].DocumentBody;
                        cellText.width = "300";
                        cellText.height = "300";
                        cellText.id = "img" + j;
                    }
                    else if (i == 3) {
                        var cellText = document.createElement("input");
                        cellText.type = "checkbox";
                        cellText.name = "name";
                        cellText.id = "chk_" + j;
                        cellText.checked = true;
                    }
                    cell.appendChild(cellText);
                    row.appendChild(cell);
                }
                // add the row to the end of the table body
                tblBody.appendChild(row);
            }
            // put the <tbody> in the <table>
            tbl.appendChild(tblBody);
            // appends <table> into <body>
            divTarget.appendChild(tbl);
        }

        // Print the selected content of the page.
        function PrintSelectedContent() {
            var vTableStart = "<table>";
            var vTableEnd = "</table>";
            var vTrs = "";
            var ttl;
            for (var vCount = 0; vCount < vRecordCount; vCount++) {
                if (document.getElementById("chk_" + vCount).checked == true) {
                    vTrs = vTrs + "<tr>";
                    var vCells = document.getElementById('tr_' + vCount).getElementsByTagName('td');
                    for (var iCell = 0, iTotal = vCells.length; iCell < iTotal; iCell++) {
                        if (vCells[iCell].firstChild.getAttribute('type') != 'checkbox') {
                            var vTableCell = document.createElement("td");
                            vTableCell.className = "tdClass";
                            vTableCell.style.width = "30%";
                            vTableCell.style.fontFamily = "sans-serif"
                            vTableCell.style.fontSize = "medium";
                            // vTableCell.style.border = "1px solid black";
                            vTableCell.style.textAlign = "center"
                            vTableCell.style.padding = "10px";
                            vTableCell.style.height = "100"
                            vTableCell.style.backgroundColor = "aliceblue";
                            vTableCell.innerHTML = vCells[iCell].innerHTML;
                            vTrs = vTrs + vTableCell.outerHTML;
                        }
                    }
                    vTrs = vTrs + "</tr>";
                }
            }
            var vCompleteTable = vTableStart + vTrs + vTableEnd;
            var printWindow = window.open('', '', 'height=400,width=800');
            printWindow.document.write('<html><head><title>DIV Contents</title>');
            printWindow.document.write('</head><body >');
            printWindow.document.write(vCompleteTable);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            printWindow.print();
        }
    </script>
    <meta charset="utf-8">
</head>
<body style="-ms-zoom: 1;">
    <button onclick="PrintSelectedContent();">Selected Content</button>
    <div id="imagediv" style="width: 50px; height: 20px;"></div>
    <div id="divDynamicContent" style="width: 100%; height: 100%;">
    </div>
    <div id="dvContainer">
    </div>
</body>
</html>

--
Happy CRM'ing
Gopinath.