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

Thursday, 20 November 2014

CRM 2013 to 2015 Upgrade

CRM 2013 to 2015 Upgrade
Here I will discuss about important things need to know. I.e. what is changing, what is important and what Microsoft is Recommending
Supported Software
·        Windows Server 2012/2012 R2
·        SQL Server - 2012
·        Windows Client - Windows7 and Windows 8
·        Browsers -- IE 10/11
·        Exchange Server - 2010/2013
·        Outlook - 2010/2013
Discontinuing support
·        Window server 2008/2008R2
·        SQL Server 2008/2008R2
·        Windows Vista
·        Internet Explorer 8/9 (It was used to take to mobile forms but now CRM continues to open normally and features stop working in older versions)
·        Exchange and Office 2007
Server Upgrade Process        
Upgrade is a one-way process CRM 2015 cannot be uninstalled
Two methods to upgrade
·        Install media on current server
·        Migration upgrade (Recommended) deploying another CRM instance with a separate database and it is most efficient way
Upgrade must start from CRM 2013 6.1(Service Pack 1) and Server must be on CRM 2013 SP1 or higher
Upgrade Process
While upgrading process, CRM applies check to determine if it's SAFE to upgrade organization and it provides to link to an article it there is an issue
The issues which will be taken care by the product are informed as part of warnings
Important things to be considered from upgrading CRM 2013 to CRM 2015
According to CRM 2013 architecture there was change in core database structure
·        Collapsed a series of tables to optimize frequency
·        Allowed customers to run in temporary "expanded state"
·        For 2015, the merge process is mandatory for upgrade. The base and extension merge process.
Base and Extension Table merge
All extension tables merged by default during upgrade process as Merge is mandatory for CRM 2015. If database cannot be merged, the upgrade will process will cease and the owner notified
Solutions Framework Updates (Move custom from one to another)
·        CRM 2013 solution could not be imported in CRM 2011
·        CRM 2013 SP1 could not be imported in CRM 2013
With CRM 2013 SP1, CRM Product came with ability to export to a specific version of CRM. CRM 2015 solution will work CRM 2015 deployments and will not imported to CRM 2013. The reason behind this that there is a change in fundamental structure of CRM.
Upgrading Forms form 2011 and 2013
There is change in the forms design in CRM 2013 and it also allowed to retain 2011 forms. Moving forward in CRM 2015 continues the same. CRM capabilities are added to the new forms (2013).
·        CRM 2011 forms are all called with name Information.
·        CRM 2013 forms are called with Entity Name (Account, Lead, Case etc...)
We can activate and deactivate forms.
On CRM 2013 form, you will find a button called Merge forms, that button is only activated on 2013 forms, that allows to choose 2011 and it merges 2011 form to 2013.
You will find more information in Implementation Guide of CRM 2015 SDK.
 
--
Happy CRM'ing
Gopinath