If you log in to any social media platform you’ll see a red notification badge telling you how many unread messages you have. We don’t have this in the Microsoft Dynamics Portal by default, but with a little effort we can create this, and provide the user with a notification count and an inbox to browse through each time they log in, ending up with something like this:

The first thing we are going to do is add a new custom entity for a ‘Portal Message’. Although we already have Portal Comments, we want to keep this separate from those, so the new entity is needed. Make sure you check to define as an activity entity.

Also make sure you enable ‘Change Tracking’ from the Data Services section. This is important to make sure when new Portal Messages are created in D365, they will display pretty much right away in the portal for the user. If you don’t check it, you will see that it can take a while for them to display, even to the point where you need to restart the portal to see them. Not a smooth process at all!

For your fields, you shouldn’t need to add any. We want a subject, description, status reason, and date read. You can add a new field for data read, or you could use the ‘closed’ or ‘completed’ date of the activity. For your Status Reason field, change the Active status of ‘Open’ to ‘Unread’, and the Inactive status of ‘Completed’ to ‘Read’. We are going to use this in the portal so the user can indicate when they have read a message. Next, we will add a new field to the Contact record. This will be a rollup field to count the number of unread Portal Message records where the Contact is set as the Regarding on the activity. It should be a Whole Number.

For the details on the roll up, select Portal Messages as the Related Entity , and set the Filter to be if the Status Reason is ‘Unread’. Set the Aggregation as the Count of all Portal Messages.

Now we need a couple of workflows. The both are linked to the Portal Message entity, but the first one runs when a new record is created. It’s got to be a real time workflow. We want to force a recalculation of the roll up fields for Unread Messages each time a new Portal Message is created. To do this, we need to download a solution from Demian Rasko, the Dynamics 365 Workflow Tools solution which you can find here. We are going to use the Calculate Rollup Field option in this workflow.

For the Calculate Unread Messages step using Demian’s solution, you will add in the name of the rollup field created, and use the Parent Record URL from the Regarding Contact relationship, then the RecordURL(Dynamic) value. This means when the Portal Message is created, the rollup field for the related Contact will recalculate. We are adding in the Update Contact step at the end which forces it to then be updated and displayed accurately in the Portal. Without that step, the user wouldn’t see the correct updated field.

Our second workflow will only run upon initiation by the portal user. When they are viewing a message they will have a button to mark it as read. That will then kick off this workflow which will change stamp the date of when it was read, change the status to read, and then recalculate the rollup field to get the new number of unread messages. It is also a real time workflow, but only running on-demand.

So, our condition first checks to make sure the Status Reason of the Portal Message is ‘Unread’, then it updates the Date Read field using the date from the workflow process running. We change the status of the Portal Message to Read, then we use the step to calculate the rollup field for the unread messages. Finally, we want to force an update of the Contact record for the same reasons as we did in the first workflow.

Next we need to add some CSS. If you haven’t done this before, just copy and paste the code below in to Notepad, then save as something like ‘custom-styles.css’. Make sure you change the file type to All when you are saving otherwise it will just save as a .txt file which won’t work. Then navigate to the Home page while logged in as someone with an Administrator Web Role and create a new file. Upload it and save. You won’t see any changes yet as you’ve not added the css styling to anything, but keep in mind if you use Google Chrome, you might not see your style changes until you delete your cache. I tend to use Firefox for refreshing and seeing the changes displayed a bit more easily.

.notification {
  color: white;
  text-decoration: none;
  padding: 15px 26px;
  position: relative;
  display: inline-block;
  border-radius: 2px;
}

.notification:hover {
  background: red;
}

.notification .badge {
    position: absolute;
    top: 16px;
    right: -14px;
    padding: 5px 10px;
    border-radius: 50%;
    background: red;
    color: white;
}
.notification .badge-zero {
    position: absolute;
    top: 16px;
    right: -14px;
    padding: 5px 10px;
    border-radius: 50%;
    background: #9a9292;
    color: white;
}
.divider-inbox {
    height: 21px;
    margin: 0 4px;
    margin-top: 14.5px;
    border-right: 1px solid #fff;
    border-left: 1px solid #666;
    margin-left: 20px;
}
.btn-message {
    color: #ffffff;
    background-color: #bd2b2b;
    border-color: #6d0705;
}
.btn-message:hover {
    color: #5a4645;
    background-color: #dec4c4;
    border-color: #2b2b2d;
}
.btn-message:active, .btn-message.active, .open>.btn-message.dropdown-toggle {
    color: #e60707;
    background-color: #e6e6e6;
    border-color: #8a0c0a;
}

Now let’s go ahead and add some code to the top navigation of the portal so we can see the notifications. Here is what it’s doing. We are checking to see if the logged in portal user has more than 0 unread messages using the rollup field as the value (nps_unreadmessages in my example). If it is greater than zero, we are using the css class of badge, then showing the value from that field. The badge css will make the notification icon have a red background. If it equals zero, we are using a similar css class but this time badge-zero, which instead of red will be grey. Finally, if the user doesn’t have anything in that field which would be the case when you first create the field, just show zero and use the same badge-zero css. That way there will always be a number, and it won’t be so prominent in red if there are no unread messages.

Here is the code used above, you can simply copy it and paste in to your Header web template.

 <li class="dropdown" role="none">
            <a href="/inbox" class="notification">
            <span>Inbox</span>
          {% if user.nps_unreadmessages >0 %} 
              <span class="badge">
              {{user.nps_unreadmessages}}
              </span>
              {% else if user.nps_unreadmessages ==0 %} 
              <span class="badge-zero">
              {{user.nps_unreadmessages}}
              </span>
              {% else %}
              <span class="badge-zero">
              0
              </span>
              {% endif %}
              
            </a> 
            </li>
            <li class="divider-inbox" aria-hidden="true"></li>

Now we need to add some Entity Permission records to make sure the portal user can actually see the new Portal Message entity we created. You should already have an Entity Permission for the Contact entity with the Scope of Self that is installed with the Portal. If not, you can create one, but check first, no point in having two!

Now create a new Entity Permission record for the Portal Message entity. Set the Scope as Parent.

Set the Parent Entity Permission as the Contact ‘Self’ entity permission and set the Parent Relationship to be the one that shows the Contact and Portal Messages in the name. Set the Privileges to be Read only.

For the Web Roles, make sure you add any you currently use for any Contact that is logged in.

We are going to add to our CSS file now. The following will be used to determine how our Portal Message is displayed in the portal, and add some conditional formatting on the list of messages in the inbox to highlight those that are Unread.

.message-subject {
    display: block;
    width: 100%;
    height: 35px;
    padding-bottom: 15px;
    font-size: 30px;
    line-height: 1.42857;
    color: #555;
    font-weight: bold;
margin-left: 60px;
}
.message-description {
    display: block;
    width: 100%;
    height: 35px;
    padding: 6px 12px;
    font-size: 18px;
    line-height: 1.42857;
    color: #555;
    font-weight: normal;
    margin-left: 60px;
}
.unread-highlight {     
color: #94050c;
font-weight: bold;
background-color: #f5dbd9 !important;
}
.unread-highlight a {
    color: #94050c;
    text-decoration: underline;
}
.form-custom-actions {
    margin-bottom: 10px;
    margin-top: -50px;
}
.pull-right {
    float: left !important;
}

Now we need to create a new Entity Form to display the Portal Message for the portal user to review. Set the Entity as the Portal Message, and then pick the form you want to use. It’s a good idea to create one specifically to use in the portal, and a different one to use within D365. For the Portal Message, I don’t want the labels of Subject and Description to be displayed, so I have two forms for that purpose. Set the Mode as Read Only, and the Records Source Type as Query String, with the Record ID Query String Parameter Name as id. Finally, tick the box to Enable Entity Permissions.

Move along to the Additional Settings tab. Just a note, I was doing all of this in the new UI Portal app, but when I got to this, the Additional settings showed an error where I should have been able to add an Action button. I went back to the classic interface and it was fine. We are going to add a Workflow action. We can cause a workflow to be kicked off when the user clicks on a button. So here we are going to add the ondemand realtime workflow we added which marks the portal message as read. On Complete we will refresh the page and display a success message. The button label will be Mark As Read.

Scrolling further down, we are going to use some styling for the button so adding in btn-message as the class. I want to display it above the form and to the right. Finally, we have to add in some Fetch XML to determine when the button displays. Otherwise it won’t display at all. For this, just go to your Advanced Find, create a simple search for Portal Messages, where the Status Reason equals unread. Then click the button to download the FetchXML, then paste it in the Filter Criteria field.

On the Entity Form Metadata we need two added, one for the subject attribute and one for the description attribute. The only thing you need to add for each one is to scroll to the control style section and add in the CSS. We are using styles from the new CSS created, so add message-subject and message-description in the CSS Class fields on the appropriate Metadata record.

Now we need a new Entity List to display the Portal Messages. Select a view that is displaying all of the fields you want the user to see. In this, we have a view called ‘Portal – Users Messages’ which will show the subject, the create date, date read and the activity status reason.

Scroll down the page and tick the box to enable metadata filtering. Set the Orientation as Vertical and add the word you wish to appear as a button for the user to filter their messages.

Now we can set the filter based on the Status Reason attribute. Let’s allow the user to show all read or unread messages. By default the filter will be off so they will see all of their messages. After you’ve selected the attribute, choose check boxes as the selection mode, and then the display name as Display Messages (or whatever you wish to show). For the options, it will show all of the values in the Status Reason field. You can remove any you don’t wish to display by clicking the x next to them. Here we are just going to show the Unread and Read values.

Moving on to the Options tab, we can add in a bit of javascript (thanks to my brother Owen Rumney for helping me figure out how to do this!) to do some conditional highlighting. For any rows where the Status Reason is Unread, some additional css will be applied to them, making them stand out in red.

Scrolling down further we can make changes to the columns headers under the Grid Configuration section. You can override the display name for each attribute as required.

You can copy and paste the javascript below.

$(document).ready(function (){
   $(".entitylist.entity-grid").on("loaded", function () { 
$("td[data-attribute='statuscode']").each(function(status) {
    
if ($(this).text() === 'Unread') {
$(this).parent('tr').addClass('unread-highlight');
    }
});
   });
});

Near the end now! We need an Inbox page to display the list of messages, so we can add one right from the Home page (assuming you are logged in to the portal as a user with the Administrator Web Role). Make sure it has the partial URL of inbox. If you don’t that’s fine, but you will then need to edit the Header Web Template and change the link for the Inbox so it goes to your partial URL when it’s clicked on. The page template is Full Page without Child Links, and then set the Entity List as the Portal Messages one we just created.

From the Inbox page, add a child page. Call this one Message (or whatever you want displayed as the header. You can select the Full Page without Child Links page template, and set the Entity Form as the Portal Message we just created.

Finally, go back to the Portal Messages Entity List, and scroll to the Web page for Details View field and add the Portal Message web page you created, and make sure the ID Query String Parameter Name is id. This will make sure when the user clicks on a message from the list they will be taken to the correct message on the Portal Message page.

Now that we have everything set up, let’s see how to use it. First things, you will probably want to add in a new workflow for some kind of welcome message. Maybe it’s something that you use to add details on how you want people to use the portal. This is something pretty simple. The workflow will run on change of the ‘Login Enabled’ field. If it’s Yes, then a new Portal Message will be created. So that will always give us our first message for the user. Your messages could then be generated when a case is closed, or when an order is placed, letting them know about business closures, telling them about a new product or a sale. The possibilities are kind of endless!

So now we can see a user logged in. The notifications at the top show there is one unread message. We can see the metadata filter we added to the Entity List on the left. We can also see that the unread message is highlighted in red.

If we click on the subject of the message, we see the Mark As Read button at the top right which we added to the Entity Form for the Portal Message. We can also see the formatting we added to the Entity Form Metadata to use specific CSS classes for the subject and the description.

If we mark the message as read, then go back to the Inbox we can see that it’s no longer highlighted in red, and there is now a date and time in the Read column, and the status shows as Read. Awesome!

What other uses can you come up with for sending out messages to your portal users? Let me know in the comments below!