Tutorial: Customizing RegistrationWorkflow


RegistrationWorkflow contains a SendMail activity which sends a confirmation e-mail to the person who wants to register to your site. The content of the e-mail such as the subject and body are specified right inside the workflow definition (/Root/System/Workflows/RegistrationWorkflow.xaml). This short tutorial will show you how to localize and even customize these texts using String Resources.

We usually refer to the content which represents the WF4 Workflow in the repository as StateContent. It’s created when you start the desired workflow. Using StateContent is the best way to exchange information between a workflow and your Sense/Net powered portal. We could also say it’s kind of a gateway.

So what we need to do is:

  • Extend RegistrationWorkflow content type with two more fields
  • Create localized string resources
  • Create a custom content handler that handles extra fields
  • Modify workflow definition

Our first step is to add two more fields: EmailSubject and EmailBody to the Content Type Definition. These fields can be ShortText fields and hidden because we don’t need the user to see it.

[code:xml]<Field name="EmailSubject" type="ShortText">
  <DisplayName>EmailSubject</DisplayName>
  <Configuration>
    <VisibleBrowse>Hide</VisibleBrowse>
    <VisibleEdit>Hide</VisibleEdit>
    <VisibleNew>Hide</VisibleNew>
  </Configuration>
</Field>
<Field name="EmailBody" type="ShortText">
  <DisplayName>EmailBody</DisplayName>
  <Configuration>
    <VisibleBrowse>Hide</VisibleBrowse>
    <VisibleEdit>Hide</VisibleEdit>
    <VisibleNew>Hide</VisibleNew>
  </Configuration>
</Field>[/code]

Now we need the localized string resources. Open Content Explorer and create a new resource file under /Root/Localization or you can also extend an already existing file.
I added the following lines into RegistrationWorkflowResources.xml:

[code:xml]<?xml version="1.0" encoding="utf-8"?>
<Resources>
  <ResourceClass name="RegistrationWorkflow">
    <Languages>
      <Language cultureName="en">
        <data name="EmailBody" xml:space="preserve">
          <value>Please confirm your registration by visiting this url: {0}</value>
        </data>
        <data name="EmailSubject" xml:space="preserve">
          <value>Please confirm your registration</value>
        </data>
      </Language>
      <Language cultureName="hu">
        <data name="EmailBody" xml:space="preserve">
          <value>Kérjük erősítse meg a regisztrációját a következő linkre kattintva: {0}</value>
        </data>
        <data name="EmailSubject" xml:space="preserve">
          <value>Regisztráció megerősítése</value>
        </data>
      </Language>
    </Languages>
  </ResourceClass>
</Resources>[/code]

As you can see there's a parameter {0} in EmailBody. This will contain the confirmation link which is built up by two parts. First part defines the site URL and the second part specifies the URL of the confirmation action. The second part is only available after StateContent has been saved so that it will be added to the URL in the workflow definition.

Basically, overwriting system files is not a good approach so we’re not going to modify the RegistrationWorkflow content handler in order to set the values of the extra fields. We will create a new content handler instead and refer to it in the CTD. In our new content handler, called CustomRegistrationWorkflow, we need to override two Save() methods and implement our own logic then call the base method to do the rest of it:

[code:c#][ContentHandler]
public class CustomRegistrationWorkflow : RegistrationWorkflow
{
  public CustomRegistrationWorkflow(Node parent) : this(parent, null) { }
  public CustomRegistrationWorkflow(Node parent, string nodeTypeName) : base(parent, nodeTypeName) { }
  protected CustomRegistrationWorkflow(NodeToken nt) : base(nt) { }

  public override void Save(){
      GenerateEmailContent();
      base.Save();
  }
  public override void Save(NodeSaveSettings settings){
      GenerateEmailContent();
      base.Save(settings);
  }
  private void GenerateEmailContent(){
      this["EmailSubject"] = SenseNetResourceManager.Current.GetString("RegistrationWorkflow", "EmailSubject");
      this["EmailBody"] = String.Format(SenseNetResourceManager.Current.GetString("RegistrationWorkflow", "EmailBody"), PortalContext.Current.RequestedUri.GetLeftPart(UriPartial.Authority));
  }
}[/code]

Don’t forget to change the handler in the CTD:

[code:xml]<ContentType name="RegistrationWorkflow" parentType="Workflow"handler="SenseNet.Workflow.CustomRegistrationWorkflow" xmlns="http://schemas.sensenet.com/SenseNet/ContentRepository/ContentTypeDefinition">[/code]

One more step left: modify the workflow definition.
The definition file can be found under the /Root/System/Workflows folder. Using Visual Studio you’re able to make the changes in a nice visual editor. Look for SendMail then change the Body and Subject parameters as follows:

Subject: CStr(StateContent("EmailSubject"))
Body: CStr(StateContent("EmailBody")) + ConfirmationItem.ActionUrl("Browse")

That’s pretty much of it. Now when a new user registrates he/she will be informed in the language of your site.

Comments are closed

Welcome to the blog!

Sense/Net ECM is ever evolving. Community means the world to us so we want to keep you apprised on what’s happening on our side of the woods. Want to make us happy? Add a comment and tell us what you think!

Month List