Programmatically Creating DNN Roles A Gotcha Moment! 

This is just a quick blog post to hopefully share some helpful information.  Recently when working on a DotNetNuke Authentication Provider for a client I was tasked with syncing roles from the external system into DNN.  Simple enough right?  We can just use the "RoleController" and the "AddRole" method to add the role to the system. Well, that is the process but it doesn't work 100% unless you specify specific items.  The following sections show what I tried, and what actually works, along with a few notes.

My Starting Code

I think a logical starting point here would be to show what I used as my first attempt for adding a new role to the system.

RoleController oDnnRoleController = new RoleController;
RoleInfo oRole = new RoleInfo;
oRole.PortalId = this.PortalId;
oRole.RoleName = "MyRoleName";
oRole.IsPublic = false;
oRole.AutoAssignment = false;
oDnnRoleController.AddRole(oRole);

At first glance, this would seem just perfect right? Not so much, no error is generated, but the role is not added to the system.

The Fix

The fix was very simple, after digging through the code, and googling for a while to get the best solution.  Simply set the RoleGroupID = -1 and it will work just fine.  So my updated working code is as follows:

RoleController oDnnRoleController = new RoleController;
RoleInfo oRole = new RoleInfo;
oRole.PortalId = this.PortalId;
oRole.RoleName = "MyRoleName";
oRole.IsPublic = false;
oRole.AutoAssignment = false;
oRole.RoleGroupID = -1;
oDnnRoleController.AddRole(oRole);

Conclusion

I hope that this article helps someone else out looking to create DNN roles via Code. Feel free to share comments below.

Posted by Mitchel on Thursday, May 14, 2009
 

Comments

I have seen this before. This is very annoying in DotNetNuke. I don't understand why -1 is used instead of null, and I don't understand why the default value in the constuctor is 0 instead of -1. I reported a similar issue as bug, but they didn't consider it as a bug.

By Paco on Thursday, May 14, 2009 at 7:43 AM

Instead of using -1 can use Null.NullInteger. Is the recommended way

By Juanito Perez on Thursday, May 14, 2009 at 10:54 AM

-1 and Null.NullInteger are the exact same thing

By mitchel.sellers@gmail.com on Thursday, May 14, 2009 at 11:16 AM

Humm... but if you see the source code in all modules an even DNN core, the Team always use Null.NullInteger. So, why not follow the recommended practice rather than the "hardcode" the -1 value. I do not intend to enter into discussions. This is only my humble point of view.

By Juanito Perez on Thursday, May 14, 2009 at 12:10 PM

This is actually occurring due to subpar exception handling; the invalid RoleGroupId is being thrown by the database, caught by the provider, and swallowed (see DNNRoleProvider.vb on or about line 100). The method IS at least returning false, but that's really not useful at all -- the foreign key issue should be propagated up the stack.

I've actually had very good luck at getting this class of issues resolved; I'd suggest opening a work item for it. I think the code would be greatly improved by changing it to throw an ArgumentException when a bad roleGroupId is supplied (at the RoleController level) AND a foreign key exception at the provider level.

Brandon

By Brandon Haynes on Thursday, May 14, 2009 at 9:47 PM

Brandon,

I just logged it in gemini! http://support.dotnetnuke.com/issue/ViewIssue.aspx?id=9926&PROJID=23

By mitchel.sellers@gmail.com on Friday, May 15, 2009 at 7:29 AM

I agree with Juanito. Using "Magic Numbers" instead of constants/enums is poor programming practice and a nightmare for the maintenance programmers down the line. I've personally felt this pain PainFrequency.Common times!

By Craig Boland on Monday, June 08, 2009 at 5:10 PM

Name (required)

Email (required)

Website

CAPTCHA image
Enter the code shown above:

Content provided in this blog is provided "AS-IS" and the information should be used at your own discretion.  The thoughts and opinions expressed are the personal thoughts of Mitchel Sellers and do not reflect the opinions of his employer.

Friend of RedGate

www.datasprings.com - DotNetNuke ModulesICG

Click here for advertising information.

Content in this blog is copyright protected.  Re-publishing on other websites is allowed as long as proper credit and backlink to the article is provided.  Any other re-publishing or distribution of this content is prohibited without written permission from Mitchel Sellers.