Search code examples
javascriptdynamics-crmcrm

Geting CRM theme color using JavaScript


I am working my CRM Solution including some JS Web Resource, that should add new buttons on account entity(they are placed in the form body). I also made a new CRM theme.

Is there any JavaScript function to get color from that theme, so i can use it for my button?

The goal to automatically change the button color when the color I get from theme is changed, without any need to change my code.


Solution

  • You can use the CRM REST Builder to create the following code which will return the default (i.e., in use) theme:

    var req = new XMLHttpRequest();
    req.open("GET", Xrm.Page.context.getClientUrl() + "/api/data/v8.0/themes?$filter=isdefaulttheme eq true", true);
    req.setRequestHeader("OData-MaxVersion", "4.0");
    req.setRequestHeader("OData-Version", "4.0");
    req.setRequestHeader("Accept", "application/json");
    req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
    req.setRequestHeader("Prefer", "odata.include-annotations=\"OData.Community.Display.V1.FormattedValue\"");
    req.onreadystatechange = function () {
        if (this.readyState === 4) {
            req.onreadystatechange = null;
            if (this.status === 200) {
                var results = JSON.parse(this.response);
                for (var i = 0; i < results.value.length; i++) {
                     var themeid = results.value[i]["themeid"];
                }
            }
            else {
                alert(this.statusText);
            }
        }
    };
    req.send();
    

    You can also use WebAPI with the following URL:

    http(s)://SERVER/ORG/api/data/v8.0/themes?$filter=isdefaulttheme eq true