Skip to main content

Update entity image of entity in Dynamics CRM using javascript

Hi All,

Recently, I came across a requirement where I wanted user to change entity image from html file up-loader.

Below is the piece of code used to update entity image. 

  Uploader.prototype.PostRequest = function (content) {
            var configurationId = parent.Xrm.Page.data.entity.getId();
            var tabConfiguration = { EntityImage: '' };
            tabConfiguration.EntityImage = content; //byte[] content of the web resource
            var jsonContact = JSON.stringify(tabConfiguration);
            //OData URI
            var oDataURI = parent.Xrm.Page.context.getClientUrl()
                + "/XRMServices/2011/OrganizationData.svc/"
                + "hsl_tabconfigurationSet(guid'" + configurationId + "')";
            var req = new XMLHttpRequest();
            req.open("POST", encodeURI(oDataURI), false);
            req.setRequestHeader("Accept", "application/json");
            req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
            req.setRequestHeader("X-HTTP-Method", "MERGE");
            req.send(jsonContact);
            //req.onreadystatechange = function () {
            //debugger;
            if (req.readyState == 4 /* complete */) {
                req.onreadystatechange = null;
                if (req.status == 204 || req.status == 1223) {
                    return "SUCCESS";
                }
                else {
                    return "FAILURE: UpdateRecord Error: Cannot update tab configuration with configuration id = " + configurationId + ".";
                }
            }
            //};
        };
Uploader.prototype.UploadImage = function (control) {
            if (control[0].files.length == 0) {
                alert('Please select file to upload');
            }
            else {
                var reader = new FileReader();
                var file = control[0].files[0];
                reader.readAsDataURL(file);
                if (reader.result) {
                    var fileContent = reader.result.split(',')[1];
                    var returnMessage = this.PostRequest(fileContent);
                    alert(returnMessage);
                }
            }
        };

In Above code, function UploadImage is being called on button click event where file upload (html type file) control has been passed.

Content parameter which is being passed to PostRequest function is nothing but bytes of the image.

thanks,
p.

Comments

Popular posts from this blog

AX 7 - Get record marked and record selected on grid control in X++

Marked Records In AX, we often face a situation where we need to get the reference of those records which are marked on grid control. Using below code you can get those marked records in X++. Records marked can be seen in screenshot on the left. Array markedRecords = gridControl_DS.recordsMarked(); var lastMarkedRecord = markedRecords.lastIndex(); You will get a recId of last marked record in lastMarkedRecord. Using recordMarked() method of grid control data source, you can get an array of marked records which can be used for further iteration based on requirement. Selected Records At the same time, there is another operation called as Selected Records. When a grid control loads, a default first record is selected from the grid. To iterate through selected records, use below code. for (inv = gridControl_ds.getFirst(true) ? gridControl_ds.getFirst(true) : gridControl_ds.cursor(); inv; inv = gridControl_ds.getnext()) {    //logic here } gridControl_...

Mark Customer Transaction from X++ in AX2012

Use below code to mark customers open transaction from X++. static void MarkOpenTransaction() {     custInvoiceJour custInvoiceJour;     SalesTable  salesTable;     specTrans   specTrans;     custTrans   custTrans;     custTransOpen   custTransOpen;     TaxWithholdTrans    taxWithHoldTrans;     TaxWithhold_CustPaym taxWithhold;     CustVendOpenTransManager manager;     ExchangeRateHelper  exch;     AmountCur   totalSettleAmount;     ;     select firstOnly custInvoiceJour where custInvoiceJour.InvoiceId == '004452' ; // && _custInvoiceJour.SalesId == xml_SalesID.text();     if (custInvoiceJour)     {         ttsBegin ;  ...