Will "the Mighty" Strohl

How to Add a User to a Role Programmatically

There may come a time when you will want to programmatically add a user to an existing role in DotNetNuke®.  It’s actually quite easy.  If you take a look at how the core code in DNN does it, just emulate that.  This method of using and reusing the existing DNN core code is what DNN development is supposed to be about, so make sure you don’t reinvent the wheel.

In the source code for DotNetNuke®, there is a controller class for nearly everything that you want to do with the various objects that you work with in DNN every day.  Since I am familiar with the DNN source, I knew where to go.  But many people are not familiar with the source code.  In those cases, you need to either do some searches in the Object Browser, or browse around until you find the code you’re looking for.  Thinking it through logically, you can usually browse right to the code you’re looking for since the core team has done a fantastic job of keeping the code files organized.

Keep all of that in mind, I immediately gravitated to the Security folder of the Components directory.  I did this because roles are part of the security framework in DNN, and all of the entities and objects are in the Components folder.  Inside this folder, I saw and opened the RolesController.vb file.  To make this clear, I opened the following file in the DNN source code:


The controller classes are usually what we are looking for when we want to utilize the existing logic in DNN.  So, if I want to manage a role by putting someone into it, I naturally wanted to look for a Role Controller already – before I even opened the source code solution.

One of the methods of the RoleController class is AddUserRole(), and it has a few overloads.  We are only concerned about the root method right now.  This method accepts a few arguments, and when successfully called, it adds an existing user to an existing role.

RoleController.AddUserRole(integer PortalID, integer UserId, _ 
    integer RoleId, date EffectiveDate, date ExpiryDate)

Using this method may look self-explanatory, but I will explain it anyway.  When you intend to use this method, you should already have access to an existing UserInfo object, and a RoleInfo object.  Both are regularly available through standard module development.

  • PortalID (integer) – This is the id number representing the portal where the user and role exist.  The SqlDataProvider will use this value to correctly match up the objects.
  • UserId (integer) – This is the id number matching the user that you want to add to a role.  Most often, you would simply access this value from the PortalModuleBase class that your module inherits.
  • RoleId (integer) – The id number of the role can be found from any number of ways.  We can only assume that you already have access to this right now.
  • EffectiveDate (date) – This is the date that you want the role assignment to begin.  Usually, the current date is what you’re intending to pass to this argument.  This argument accepts a null value.
  • ExpiryDate (date) – This should be the date that you want the role assignment to expire, and as a result, the user will be removed from it.  This argument accepts a null value.

In more recent versions of DNN, there is also another value which we do not pass to the method.

  • CreatedByUserID (integer) – This is a value implemented for auditing purposes, to let us know who added the person to the role.

Let’s assume that we have a UserID of 123 and a RoleID of 10.  Going further, we will also assume that this is a first instance of a portal, equaling a PortalID of 0.  The most common role assignments do not pass an EffectiveDate or an ExpiryDate.  We will use this as an example as well.  Here is how we would call the method:

' we are assuming that you already have an intRoleId from your logic
RoleController.AddUserRole(Me.PortalID, Me.UserId, intRoleId)
' ... or ...
' Here we assume a role assignment for 30 days, beginning now
RoleController.AddUserRole(Me.PortalID, Me.UserId, intRoleId, DateTime.Now, DateTime.Now.AddDays(30))

That’s all there is to it! :) Easy, right?

blog comments powered by Disqus