Monday, February 29, 2016

Retrieve Access Team Members using FetchXML in CRM 2013/2015




Requirement to fetch the users who are added on the Access Team of the record and do some operation.

Here is the Fetch XML to retrieve the users that were added to access team. 

I was using for Order entity. If you are using for any other entity, you need to change the entity name in the 2nd line and ID schema name in the 4th line of the Fetch Xand pass record Id in the filter condition.

public static void GetAccessTeamMembers(IOrganizationService iService)
{
      string strFetchXML = @"<fetch>
                             <entity name='salesorder' >
                               <attribute name='name' />
                                   <link-entity name='principalobjectaccess' from='objectid' to='salesorderid' link-type='inner' alias='poa' >
                                      <attribute name='objectid' alias='objectid' />
                                      <link-entity name='team' from='teamid' to='principalid' link-type='inner' >
                                        <link-entity name='teamtemplate' from='teamtemplateid' to='teamtemplateid' >
                                          <attribute name='teamtemplatename' />
                                        </link-entity>
                                        <link-entity name='teammembership' from='teamid' to='teamid' link-type='inner' intersect='true' >
                                          <link-entity name='systemuser' from='systemuserid' to='systemuserid' link-type='inner' >
                                            <attribute name='fullname' />
                                         </link-entity>
                                       </link-entity>
                                      </link-entity>
                                      <filter type='and'>
                                       <condition attribute='objectid' operator='eq' value='GUID_OF_THE_RECORD' />
                                        </filter>   
                                    </link-entity>
                                  </entity>
                                </fetch>";

      EntityCollection entColAccessTeamMembers = iService.RetrieveMultiple(newFetchExpression(strFetchXML));
}

Read images of Notes and printing selected images in MS CRM

Read images of Notes and printing selected images in MS CRM


Read image from Notes and show them on HTML Page

Show images attached to the notes of the entity and client will select some of them and should be able to print them.

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 {
            border1px solid black;
            width33%;
            text-aligncenter;
            padding12px;
            font-familysans-serif;
            font-sizemedium;
            vertical-aligncentral;
            background-coloraliceblue;
        }
    </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-zoom1;">
    <button onclick="PrintSelectedContent();">Selected Content</button>
    <div id="imagediv" style="width50pxheight20px;"></div>
    <div id="divDynamicContent" style="width100%height100%;">
    </div>
    <div id="dvContainer">
    </div>
</body>
</html>