Powered By Blogger

Wednesday, October 5, 2022

Power Apps Portal - How to write Code for Refreshing Cache Programatically?

As you already know for refreshing the Cache server-side in the Cache, you need to visit <portal_path>/_services/about URL. And you need to press "Clear Cache"  for clearing the cache manually. So, that you can see the latest changes made in the CRM. 


But Sometimes, there is a need to Refresh Cache Programatically to avoid refreshing it manually. Below is the code which can help you with that:

{% block main %}

{% include 'PortalWebAPIAjax' %}

{% assign urlname = sitemarkers["your site marker name"] %}

{% assign guid = request.params['id'] %}

<script>

  function rtc() {

    debugger;

    try {

      var n = Date.now();

      var timestamp = n.toString();

      var guid = "{{guid}}";

      //alert(guid);

      webapi.safeAjax({

        type: "PATCH",

        url: "/_api/entity_logicalname(" + guid + ")/ema_cacherefresh",

        contentType: "application/json",

        data: JSON.stringify({

          "value": timestamp

        }),

        success: function(res) {

          console.log(res);

        }

      });

    } catch (err) {

      alert(err.message);

    }

  }

  $(document).ready(function() {

    rtc();

    //jump to actual detail page

    window.location.href = "{{ urlname.url }}?id={{guid}}";

  });

</script>

{% endblock %}


Note: 

1. The text Highlighted in yellow, needs to get replaced with your component name.

2. Here PortalWebAPIAjax is the separate Web Page for calling Web API. Below is the Code for this web page.

PortalWebAPIAjax :

<script>(function(webapi, $){

function safeAjax(ajaxOptions) {

var deferredAjax = $.Deferred();


shell.getTokenDeferred().done(function (token) {

// add headers for AJAX

if (!ajaxOptions.headers) {

$.extend(ajaxOptions, {

headers: {

"__RequestVerificationToken": token

}

});

} else {

ajaxOptions.headers["__RequestVerificationToken"] = token;

}

$.ajax(ajaxOptions)

.done(function(data, textStatus, jqXHR) {

validateLoginSession(data, textStatus, jqXHR, deferredAjax.resolve);

}).fail(deferredAjax.reject); //AJAX

}).fail(function () {

deferredAjax.rejectWith(this, arguments); // on token failure pass the token AJAX and args

});


return deferredAjax.promise();

}

webapi.safeAjax = safeAjax;

})(window.webapi = window.webapi || {}, jQuery)</script>

No comments:

Post a Comment

SQL Queries (SQL4CDS) – Dataverse / Dynamics 365

    1) Get the list of table  with  audit  enabled - SELECT    logicalname,           displayname,           isauditenabled FROM      metada...