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.

Read Sharepoint file(s) inside a folder

Read SharePoint file(s) in C#

Today I got a requirement to read a document inside a folder. I have integrated CRM with SharePoint and once we upload a document, list component creates a folder with the title appended with GUID of the record and places the files inside.

Here is the code to read the files from that folder.


using (ClientContext clientContext = new ClientContext(SharepointsiteURL))
            {
                clientContext.Credentials = new System.Net.NetworkCredential(SharepointUsername, Sharepointpassword, "<Domain>");
                Web objWeb = clientContext.Web;
                List douLib = objWeb.Lists.GetByTitle("Case");
                clientContext.Load(objWeb);
                CamlQuery caml = new CamlQuery();
                string strQuery = "<View Scope=\"RecursiveAll\"> " +
                    "<Query>" +
                    "<Where>" +
                                "<Eq>" +
                                    "<FieldRef Name=\"FileDirRef\" />" +
                                    "<Value Type=\"Text\"><Share Point Folder Relative URL></Value>" +
                                 "</Eq>" +
                    "</Where>" +
                    "</Query>" +
                    "</View>";
                caml.ViewXml = strQuery;
                ListItemCollection items = douLib.GetItems(caml);
                clientContext.Load(items);
                clientContext.ExecuteQuery();
                WebRequest objWebRequest;
                MemoryStream objMemStream;
                WebResponse objResponse;
                Stream fileStream;
                for (int intCount = 0; intCount < items.Count; intCount++)
                {
                    // Get the path of
                    objWebRequest = WebRequest.Create(new Uri("<FileURL>"));
                    objWebRequest.Credentials = new System.Net.NetworkCredential(SharepointUsername, Sharepointpassword, "<Domain>");
                   objMemStream = new MemoryStream();
                    try
                    {
                        objResponse = objWebRequest.GetResponse();
                        fileStream = objResponse.GetResponseStream() as Stream;
                        fileStream.CopyTo(objMemStream);
                        objMemStream.Position = 0;
                        byte[] objByteArray = objMemStream.ToArray();
                       // System.IO.File.WriteAllBytes(@"\test.xlsx", objByteArray);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }

--
Happy CRM'ing
Gopinath.

Wednesday, 4 March 2015

Export more than 10000 records from Advance Find

Export more than 10000 records from Advance Find

There is a limitation of 10000 records when we export from Advance Find. Unfortunately we do not receive any message saying that this is a limitation and you are able to export only 10000.

But there is a approach to export more records.

  1. Log on to the Microsoft Dynamics CRM 3.0 server as a user who has Write permissions for the following registry subkey:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSCRM
  2. Click Start, click Run, type regedit, and then click OK.
  3. In Registry Editor, locate and then click the following registry subkey:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MSCRM
  4. On the Edit menu, point to New, click DWORD Value, type maxrecordsforexporttoexcel, and then press ENTER.
  5. Double-click maxrecordsforexporttoexcel, click Decimal, type the maximum number of records that you want to export to an Excel worksheet, and then click OK.
  6. Close Registry Editor.
For more information refer KB Article

Happy CRM'img
Gopinath


 

Retrieve Access Team User of an Entity

Retrieve Access Team User of an Entity
 
Here is the advance find search criteria to search the users who were added to the access team of the entity.
 
Happy CRM'ing
Gopinath