Call Azure Function From Dynamics PowerApps Portal

Quite often we come across scenarios in Dynamics or PowerApps Portal where we need to use custom server side code to fulfill any functionality which might not be possible to achieve using liquid Code or normal script.
In these scenarios we have options like using Flow/Workflow, Logic Apps, Azure Function or even companion Apps.
Here in this article I will be posting my experience of using or better to say that how we can call Azure function using Client side Script.

To call a azure function we need to first Create one.

Create a new Azure Function

  1. Go to portal.azure.com and search for Function App
  1. Click on Add Button and then you will get a Create Function App screen
    in my case I’m using DemoAppAzureFunction.

You can click on create + Review Button an then Create button, after providing the required details

Then it will take some time to provision and to complete the deployment process.

Copy the storage account No from the first row in the List. This is required to connect your Visual Studio code with the Azure Function Resource.
Once your Azure App deployments complete you can click on Go to Resource Button.
A new overview screen will appear with all the details of your newly created Function App

Now Open your Visual Studio and Open a New Project after that select Azure Functions from Cloud in Visual C#

Do Sign in Visual Studio with the same account as your Azure Account.


You can then click on OK to create the new Azure function Project.
On Next Step you will get a Screen where you can enter your type of Azure Function Trigger (Select HTTP trigger) and then Select your storage Account from your Azure Subscription using browse option from the Storage Account drop-down.

on click of browse you will see the screen like below, where you can provide the location of your hosted azure app to the visual studio. This will help you to publish the code later.
You need to be a Sign in user on Visual Studio to perform this step with the azure account.

After this step you can click on OK And you will get your Azure OOB basic Code, Which you can deploy and test by hitting the URL.

You can run the code locally on Visual studio and you can debug your code
by hitting the localhost URL with your parameters

We can then Build and publish the code on Azure to test it from the browser.

To check your hosted code after publishing you can go back to Azure and click on the Function tab from the Left hand side navigation Link and there you will find the details of your Azure Function.


Now click on the function which you just created and then click on the Get Function URL button to get your Azure function URL.

You can then hit this URL on the Browser to get the response from the function.

Now to call this Azure function from your portal page using Jquery, you will have to add your page URL on the CORS of your Azure APP.
Suppose my Portal URL is https://portal.powerappsportals.com

Now to call the Azure Function from my portal Page I will using the following Jquery Ajax code

	$.ajax({
			url: "https://demoappazurefunction.azurewebsites.net/api/Function1",
      //If needed you can add parameters in your Query string
			dataType: 'json',
			type: "GET",
      //you can also pass your parameters as header(recommended)
			beforeSend: function(xhr){				
				xhr.setRequestHeader("UserId", "XXXXXX21-9XXd-ea11-aXXX-0XXXXXXXX7");
			},
			success: function(result){
                    if(status=200){
console.log(result)					          
                    }                    
                }
		}).fail(function($xhr) {  
                    var message = $xhr.responseJSON.message;
});

This way you can call and perform your server Side functionality from the Dynamics or PowerApps Portals.

Hope it Helps 🙂

Create a new Portal-Page in Dynamics PowerApps Portal

To create a new page in Dynamics portal, the important step is to create a web-page first as the web-page is the backbone of your portal page.
So to create the Portal page we will first create the web page in Dynamics CRM.
Before Creating the portal I will show you how my newly provisioned portal looks. I have installed a Partner Portal here.

Read More »

Build Custom Control using PowerApps Component Framework

It’s been some time now since Microsoft in it’s April release, released it’s new PCF component feature where you can build your custom control using PowerApp Component Framework. If you are not sure what PCF frame work is please read this article.

I recently got time to do some development related to it, So I’m documenting my experience of developing the custom controls.

Read More »

Retrieve Personal Setting Data of LoggedIn User in Dynamics 365 CRM Using Js.

There can be scenarios where we need to get the logged-in User personal Settings data from CRM and use it in some requirements. Microsoft has exposed a UserSetting Entity WebAPI which we can use for this purpose.

Below is the msdyn link for more info.

https://docs.microsoft.com/en-us/dynamics365/customerengagement/on-premises/developer/entities/usersettings

Here is a small example of script code which I have used to get the personal setting data with few values using javascript. It can be helpful.

Capture 3

function checkUserDefaultHomePage() {
    var currentUser = Xrm.Utility.getGlobalContext().userSettings.userId;

    Xrm.WebApi.retrieveRecord("usersettingscollection", currentUser, "?$select=homepagearea,homepagesubarea").then(
        function success(result) {
            //homepagesubarea = Default Tab.
            //homepagearea= Default Pane.
            console.log("Retrieved values: Name: " + result.homepagesubarea + ", Revenue: " + result.homepagearea);
            if (result.homepagesubarea == "nav_conts")//contacts
            {
                //your line of code
            }
            else
                return false;
        },
        function (error) {
            console.log(error.message);
            return false;
            // handle error conditions
        }
    );
}

Get Logged-In User Security Roles

//This returns the collection of GUID (GUID of security roles assigned to this user)
 
var userRoles = Xrm.Utility.getGlobalContext().userSettings.securityRoles;

for (var i = 0; i < userRoles.length; i++) {
//you can parse all the GUID in the collection and write your code as per requirement
}
return false;

Thanks. 🙂

Adding Filtered Views in UCI In Dynamics 365 CRM. Finally Achieved.

Filtered Subgrid in Dynamics 365 CRM U 

Finally, I was able to achieve the functionality of adding a filter to the subgrid view on the entity after the functionality was removed from the new UCI forms.

Earlier in the Legacy form, we use the unsupported way of adding the functionality for filtered view through DOM( Document.getElementBId()) objects. Now I found the way to achieve this through retrieve-multiple Plugin and in a fully supported way and the Paging issue also got resolved.

Read More »

How to handle UTC Daylight saving using Javascript in MSCRM.

Call the function Now from the onload event or change event. The current function will populate the current date and time on your date time field and will also take care of the daylight savings.

Capture 24

function Now(primaryControl, attributename) {
    debugger;
    var formcontext = primaryControl.getFormContext(); //formcontext is new
    // upgraded way of 
    //calling Xrm.Page which now 
    // got depricated in D365
    //If you are still working on crm 16 or below you can directly use Xrm.Page 
    if (formcontext.ui.getFormType() == 1) {
        var date = new Date();
        if (date.dst()) {
            date.setHours(date.getHours() + 1);
            formcontext.getAttribute(attributename).setValue(date);
            return;
        }
        formcontext.getAttribute(attributename).setValue(date);
    }
}

Date.prototype.stdTimezoneOffset = function () {
    debugger;
    var jan = new Date(this.getFullYear(), 0, 1);
    var jul = new Date(this.getFullYear(), 6, 1);
    return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}

Date.prototype.dst = function () {
    debugger;
    return this.getTimezoneOffset() < this.stdTimezoneOffset();
}

Know More on MSCRM Audit & Audit History

Auditing

Microsoft Dynamics 365 comes with several tools to help you manage data. One of these is the auditing feature, which allows you to track changes made to data in Dynamics 365. It also tracks each time a user logs into your system. If auditing is enabled, CRM automatically creates logs for the changes that are tracked. By default, auditing is not turned on.

Read More »