Business Solution: Customizing RegistrationWorkflow

From Sense/Net Wiki
Jump to: navigation, search
  •  
  •  
  •  
  •  
  • 100%
  • 6.0
  • Enterprise
  • Community
  • Planned


Overview

Problem description: I want to customize and localize the content of the confirmation e-mail after a user filled out the registration form.
Solution: RegistrationWorkflow CTD needs two extra fields which will hold the localized texts for the subject and body of the email. To avoid overwriting system files a new content handler will be created to put the right values into both extra fields. Finally, we will modify the workflow definition in order to use the localized values.

Steps

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.

RegistrationWorkflow and confirmation e-mail before customization

1. Extend RegistrationWorkflow content type with two more fields

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.

<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>

2. Create localized string resources

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:

<?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>

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.

3. Create a custom content handler that handles extra fields

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:

[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.AbsoluteUri.Replace(PortalContext.Current.RequestedUri.AbsolutePath, String.Empty));
  }
}

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

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

4. Modify workflow definition

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.

RegistrationWorkflow and confirmation e-mail after customization

Video

Related links

References