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.

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, or a field token for the user identified by the form's data. 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.