Skip to content

<AddToRoles> ​

After a form submits successfully, adds the user identified by UserId to each of the named RoleNames. Use the If attribute to run conditionally — for example, to add the user to a Newsletter role only when they checked the opt-in box.

StartDate and EndDate set the role's effective and expiration dates, useful for time-limited memberships.

Verified registration sites: roles are granted before the user verifies

When <AddToRoles> follows <AddUser> and targets the new account with UserId="[[__UserId]]", the role is granted the moment the form submits. If your site's registration type is Verified (Site Settings → User Accounts → Registration Settings), that is before the person has clicked the link in the verification email, so the role is active on an account that may never be verified, and anyone can register with a throwaway email address.

Do not use <AddToRoles> to grant roles DNN assigns automatically (Registered Users, Subscribers, or any role with Auto Assignment turned on). DNN adds those itself once the user is verified. If you grant any other role at signup, such as a trial role, it takes effect immediately, so protect the pages it unlocks accordingly.

Action timing

Action tags only run when the form submits successfully. They evaluate their tokens at that point — not when the form loads — so [[FieldName]] tokens read user input. Actions run in document order; an action that fails throws away every action listed below it.

Example ​

In the example below, a <Variable> captures the current user's ID into [[uid]], then <AddToRoles> adds them to the Role1 and Editors roles. Because the role list is comma-delimited, RoleDelimiter="," is set explicitly — the default delimiter is the pipe character |.

html
<AddForm>
  <Variable Name="uid" Value="[[User:Id]]" />
  <SubmitCommand CommandText="INSERT INTO Users(FirstName, LastName) VALUES(@FirstName, @LastName)" />
  <AddToRoles RoleNames="Role1,Editors" UserId="[[uid]]" RoleDelimiter="," />
  <table>
    <tr>
      <td><Label For="txtFirstName" Text="First Name" /></td>
      <td><TextBox Id="txtFirstName" DataField="FirstName" DataType="String" /></td>
    </tr>
    <tr>
      <td><Label For="txtLastName" Text="Last Name" /></td>
      <td><TextBox Id="txtLastName" DataField="LastName" DataType="String" /></td>
    </tr>
    <tr>
      <td colspan="2"><AddButton Text="Add" /> <CancelButton Text="Cancel" /></td>
    </tr>
  </table>
</AddForm>

Properties ​

PropertyValuesDefaultDescription
RoleNames *delimited listOne or more DNN role names
UserId *integer | token(current user)DNN UserID of the user to add
RoleDelimitercharacter|Character that separates the values in RoleNames
IfexpressionWhen set and the expression is false, the action is skipped (since v4.1)
StartDatedateEffective date — when the membership begins (since v4.1)
EndDatedateExpiration date — when the membership ends (since v4.1)
Culturelocale id(current culture)Locale used to parse StartDate / EndDate. Use invariant for culture-invariant parsing (since v4.1)

* Required property

Property Details ​

  • RoleNames: A delimited list of DNN role names. Use the pipe | (the default) or set a different separator via RoleDelimiter. Field tokens may be used. Roles that don't exist on the portal are silently skipped.

  • UserId: The DNN UserID of the user to add. Use [[User:Id]] for the currently logged-in user, a field token for the user identified by the form's data, or [[__UserId]] for an account created by <AddUser> earlier in the same form (see the warning above about Verified registration sites). If UserId is omitted, the current user is used (and if no one is logged in, the action exits without doing anything).

  • If: A simple equality expression. When set and the expression is false, the action is skipped. Comparisons are text-only and case-insensitive. Use = for equality and <> for inequality.

    html
    <AddToRoles If="[[SignMeUp]] = True" RoleNames="Newsletter" UserId="[[uid]]" />
  • StartDate / EndDate: Date strings parsed using the locale specified by Culture (or the current culture if Culture is omitted). When supplied, DNN treats the role membership as effective from StartDate through EndDate. Field tokens are typically used to populate these.