Show user login details in a quickest and more flexible way


You all might be aware of the CRM inbuilt User Audit Access functionality, which shows user login access. I also proposed the same solution to my client, when he asked me about how to check user login date and time in CRM. But, he wanted a user-friendly and flexible way to access this data. 
Problem with current inbuilt functionality:
    1. Accessing Data
                  To access the user login audit data, as a user I have to perform several steps. which is not the quickest or most convenient way. i.e.  Go to Advanced Settings >> Auditing >> Audit Summary View >> Filter Event Column with "User Access via Web".

    2. No filter on User Record
                It shows the login data for all users. The filter is not available for the user record, which makes it hard to find login details for specific users or a list of users. 

Solution:
        To overcome the above scenarios, we are going to build the quickest and more flexible way to show user login details. In a summary, we are going to create a virtual entity to show User Audit Access data.
Please follow the below steps:

Prerequisite: You must have an understanding of the virtual entity.

1. Create a Virtual entity


2. Create required fields and add them to the form and view. You can find the purpose of the fields in the Description column.

    
Created fields will show the data from respective fields of system entities:


    Add created fields in Advance find view and also configure sorting in Descending order with Last Login Date Time field.





3. Now we need to write a Plugin code to show user audit access data:
            Create a plugin project and add two class files to store Fetch XMLs and write a plugin code. You can access both the code files from the Here.
Now, Let's understand the code in detail.

UserAuditAccessFetchXMLs.cs  
       It contains Fetch XMLs used to retrieve user auditing data. 

    Variables
            retrieveMultipleXML: This XML is used to retrieve multiple audit access records based on the condition. As you can see there are 3 custom nodes are being used. You will get to know the purpose of these nodes later on in this blog.



            retrieveXML: This XML is used to retrieve a particular audit access record. Here as well, we have added a custom value to the value attribute instead of GUID. You will get to know the purpose of custom value later on in this blog.




RetrieveUserAuditData.cs

            It contains code that runs on Retrieve and RetrieveMultiple event triggers. As you all know for virtual entities, we need to write logic on Retrieve and RetrieveMultiple events to show data. As the data is going to be dynamically fetched by the code, we also have to write a logic accordingly filter or sorting selected by the user. Otherwise, it will always show all the rows. Let's take a look at the code structure.

        Retrieve Multiple
             Step 1: Whenever the RetrieveMultiple event gets triggered, we get the requested fetch XML. Our first step is to get the filtered and sorted attributes from the requested fetch XML.




             Step 2: The filtered and sorted fields from requested fetch XML is of virtual entity and we have to apply those while retrieving data from system entities. But for that, we need to replace the virtual entity fields with respective system fields. 









         Step 3: Once our Fetch XML is ready then we have to execute it and the retrieved system entities data is going to be mapped with virtual entity fields. Then pagination logic needs to be implemented.



Retrieve:
            Inside RetrieveMultiple code, we have mapped Audit entity record GUID to virtual entity record GUID. So, to retrieve a single record, we have to just simply pass the virtual entity record GUID to retrieve the audit record. This is done by replacing replaceAuditId, which is used in our retrieve Fetch XML. Again we have mapped virtual entity fields with respective system entity field.


So, our code is ready now. Register this Plugin assembly on you environment. 
Now, next step is to create Data Provider and Data Source entity and select the same assembly for Retrieve and RetrieveMultiple.
Create a new Data Source is CRM with registered data provider.

Give Data source a name and save the record.

Update Data source in virtual entity, Save the changes and Publish the entity.
We are done with customization and development, Now it's a testing time🙂.
In the video, First I demonstrated the system's inbuilt Audit Summary View, where I applied a filter on Changed Date as Today and Event as User Access Via Web and it shows 22 records.
Then, I moved to the created virtual entity and only applied the filter on Last Login Date Time as Today, the result was the same. It also showed 22 records. later on, I applied an additional filter to show only specific users' login details and the correct result is shown.

And lastly, representation of User Audit details on virtual entity form.






















Comments

Popular posts from this blog

Check Webresource Dependencies on form

Create a System Dashboard from a Personalized Dashboard

Open Windows Email App with To, CC and BCC information from Dynamics