Custom Notification workflow on activity assignment or reassignment

This blog posts demonstrates how to configure a notification workflow in Service Manager that sends out notifications on assignment or reassignment of an Activity.


1.       Steps for creating this custom workflow:


1.       Create a new XML file – Name it:  Custom.ActivityWorkflows.xml


a.       The name of the file should match the mp. If you decide to name your management pack something different the file name will have to match


2.       Copy and paste the complete XML provided in section 2.


3.       Save the File


4.       Last Step is to Import the management pack


a.       Service Manager Console


b.      Open Go to Administration -> Management Packs


c.       Click on “Import” in the task pane


d.      Click “Add” in the MP Import screen


e.      Select the management pack file that you created


f.        Click Import             


g.       If you get errors, please check again your MP against the XML I have provided.


5.       Verification


a.       Open Service Manager Console


b.      Go to Administration->Workflows->Status


c.       You should see “Assigned to user added to activity” in the view pane as shown below


Activity assigned to user workflow



d.       Assign an activity to a user and make sure the mail is sent


2.       Complete XML of the management pack:                   


Please note that this is for the RC build.


<ManagementPack ContentReadable=true SchemaVersion=1.1 OriginalSchemaVersion=1.1 xmlns:xsd=http://www.w3.org/2001/XMLSchema xmlns:xsl=http://www.w3.org/1999/XSL/Transform>


  <Manifest>


    <Identity>


      <ID>Custom.ActivityWorkflows</ID>


      <Version>7.0.5813.0</Version>


    </Identity>


    <Name>Custom Activity Workflows</Name>


    <References>


      <Reference Alias=WorkItem>


        <ID>System.WorkItem.Library</ID>


        <Version>7.0.5813.0</Version>


        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>


      </Reference>


      <Reference Alias=ActivityManagement>


        <ID>ServiceManager.ActivityManagement.Library</ID>


        <Version>7.0.5813.0</Version>


        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>


      </Reference>


      <Reference Alias=EnterpriseManagement1>


        <ID>Microsoft.EnterpriseManagement.ServiceManager.UI.Administration</ID>


        <Version>7.0.5813.0</Version>


        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>


      </Reference>


      <Reference Alias=EnterpriseManagement>


        <ID>Microsoft.EnterpriseManagement.ServiceManager.UI.Console</ID>


        <Version>7.0.5813.0</Version>


        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>


      </Reference>


      <Reference Alias=System>


        <ID>System.Library</ID>


        <Version>7.0.5813.0</Version>


        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>


      </Reference>


      <Reference Alias=CoreActivity>


        <ID>System.WorkItem.Activity.Library</ID>


        <Version>7.0.5813.0</Version>


        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>


      </Reference>


      <Reference Alias=SystemCenter>


        <ID>Microsoft.SystemCenter.Subscriptions</ID>


        <Version>7.0.5813.0</Version>


        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>


      </Reference>


      <Reference Alias=CustomSystem_Notifications_Library>


        <ID>System.Notifications.Library</ID>


        <Version>7.0.5813.0</Version>


        <PublicKeyToken>31bf3856ad364e35</PublicKeyToken>


      </Reference>


    </References>


  </Manifest>


  <Categories>


    <Category ID=Category.Custom.ActivityWorkflows.ManagementPack Value=EnterpriseManagement!Microsoft.EnterpriseManagement.ServiceManager.ManagementPack>


      <ManagementPackName>Custom.ActivityWorkflows</ManagementPackName>


      <ManagementPackVersion>7.0.5813.0</ManagementPackVersion>


    </Category>


    <Category ID=Category.ActivityAssignedToUserAddRule Target=ActivityAssignedToUserAddRule Value=EnterpriseManagement1!Microsoft.EnterpriseManagement.ServiceManager.Rules.WorkflowSubscriptions />


    <Category ID=ServiceManager.Console.NotificationManagement.NotificationTemplates.ActivityAssignedToUserNotificationTemplate.Category Target=ActivityAssignedToUserNotificationTemplate Value=EnterpriseManagement1!ServiceManager.Console.NotificationManagement.NotificationTemplates.Enumeration />


  </Categories>


  <Monitoring>


    <Rules>


      <Rule ID=ActivityAssignedToUserAddRule Enabled=true Target=ActivityManagement!System.WorkItem.Activity.WorkflowTarget ConfirmDelivery=true Remotable=true Priority=Normal DiscardLevel=100>


        <Category>System</Category>


        <DataSources>


          <DataSource ID=DS TypeID=SystemCenter!Microsoft.SystemCenter.CmdbInstanceSubscription.DataSourceModule>


            <Subscription>


              <RelationshipSubscription RelType=$MPElement[Name=’WorkItem!System.WorkItemAssignedToUser’]$ SourceType=$MPElement[Name=’CoreActivity!System.WorkItem.Activity’]$ TargetType=$MPElement[Name=’System!System.Domain.User’]$>


                <AddRelationship />


              </RelationshipSubscription>


              <PollingIntervalInSeconds>30</PollingIntervalInSeconds>


              <BatchSize>100</BatchSize>


            </Subscription>


          </DataSource>


        </DataSources>


        <WriteActions>


          <WriteAction ID=WA TypeID=SystemCenter!Microsoft.EnterpriseManagement.SystemCenter.Subscription.WindowsWorkflowTaskWriteAction>


            <Subscription>


              <EnableBatchProcessing>false</EnableBatchProcessing>


              <WindowsWorkflowConfiguration>


                <AssemblyName>Microsoft.EnterpriseManagement.WorkflowFoundation</AssemblyName>


                <WorkflowTypeName>Microsoft.EnterpriseManagement.WorkflowFoundation.ConfigurationWorkflow</WorkflowTypeName>


                <WorkflowParameters>


                  <WorkflowParameter Name=InstanceId Type=guid>$Data/BaseManagedEntityId$</WorkflowParameter>


                  <WorkflowArrayParameter Name=UserRelationshipIdList Type=guid>


                    <Item>$MPElement[Name=’WorkItem!System.WorkItemAssignedToUser’]$</Item>


                  </WorkflowArrayParameter>


                  <WorkflowArrayParameter Name=NotificationTemplateIdList Type=guid>


                    <Item>$MPElement[Name=’ActivityAssignedToUserNotificationTemplate’]$</Item>


                  </WorkflowArrayParameter>


                </WorkflowParameters>


                <RetryExceptions />


                <RetryDelaySeconds>60</RetryDelaySeconds>


                <MaximumRunningTimeSeconds>7200</MaximumRunningTimeSeconds>


              </WindowsWorkflowConfiguration>


            </Subscription>


          </WriteAction>


        </WriteActions>


      </Rule>


    </Rules>


  </Monitoring>


  <Templates>


    <ObjectTemplate ID=ActivityAssignedToUserNotificationTemplate TypeID=CustomSystem_Notifications_Library!System.Notification.Template.SMTP>


      <Property Path=$Context/Property[Type=’CustomSystem_Notifications_Library!System.Notification.Template.SMTP’]/Subject$>&lt;1033&gt;Activity $Context/Property[Type=’WorkItem!System.WorkItem’]/Id$ is assigned to you&lt;/1033&gt;</Property>


      <Property Path=$Context/Property[Type=’CustomSystem_Notifications_Library!System.Notification.Template.SMTP’]/Priority$>2</Property>


      <Property Path=$Context/Property[Type=’CustomSystem_Notifications_Library!System.Notification.Template.SMTP’]/IsBodyHtml$>False</Property>


      <Property Path=$Context/Property[Type=’CustomSystem_Notifications_Library!System.Notification.Template’]/Content$>&lt;1033&gt;Details:


 


Title: $Context/Property[Type=’WorkItem!System.WorkItem’]/Title$


Description: $Context/Property[Type=’WorkItem!System.WorkItem’]/Description$


Created By: $Context/Path[Relationship=’WorkItem!System.WorkItemCreatedByUser’ TypeConstraint=’System!System.User’]$?$DisplayName$?


&lt;/1033&gt;</Property>


      <Property Path=$Context/Property[Type=’CustomSystem_Notifications_Library!System.Notification.Template’]/Encoding$>utf-8</Property>


      <Property Path=$Context/Property[Type=’CustomSystem_Notifications_Library!System.Notification.Template’]/SeedClass$>System.WorkItem.Activity$3fc62adf-d590-ed19-587e-e029aecb738d</Property>


      <Property Path=$Context/Property[Type=’CustomSystem_Notifications_Library!System.Notification.Template’]/Protocol$>SMTP</Property>


    </ObjectTemplate>


  </Templates>


  <LanguagePacks>


    <LanguagePack ID=ENU IsDefault=true>


      <DisplayStrings>


        <DisplayString ElementID=Custom.ActivityWorkflows>


          <Name>Custom Activity Workflows</Name>


        </DisplayString>


        <DisplayString ElementID=ActivityAssignedToUserAddRule>


          <Name>Assigned to user added to activity</Name>


        </DisplayString>


        <DisplayString ElementID=ActivityAssignedToUserNotificationTemplate>


          <Name>Activity assigned to user template</Name>


        </DisplayString>


      </DisplayStrings>


    </LanguagePack>


  </LanguagePacks>


</ManagementPack>


 


3.       Here is some explanation of some of the sections of the Management pack to better understand the “how”


This section requires advanced understanding of SM and is not needed to achieve the goal of sending proper notification on Activity assignment. You should skip this section is you are only interested in the “how to get it to work”.


1.        Manifest Section


a.       The references section identifies the Management Pack and lists the dependencies for this management pack


b.      Aliases defined for these references make it easier to refer to these management packs in the subsequent sections


2.        Categories section


a.       This enables the Workflow to appear in the Service Manager Console UI (Administration->Workflow->Status node)


b.      This allows you to either enable or disable the workflow from the Console UI, thus avoiding having to go and edit (or delete) the management pack each time you want to disable the workflow


3.        ObjectTemplate


a.       This contains an activity notification template. I have added this in this MP to provide a notification template for Activity.


b.      UI provides easy editing of the template content: Go to Administration->Notifications->Templates, select “Activity assigned to user template” and click on the “Properties”


Activity Notification Template




4.        Monitoring Section


a.       In the Monitoring section we define the Rules.  Each rule is generally comprised of a DataSource and WriteAction


b.      DataSource defines discovery of the instances based on certain criteria.


                                   i.      In this case we want to discover any Activity for which the “Assigned to” user is changed.


                                  ii.      “Assigned To” user is a relationship on the Workitem (base class for Activity) – So whenever the assignment is changed the existing relationship is deleted and a new relationship is added


                                iii.      The criteria below discovers all instances of Activity for which a “AssignedTo” Relationship was added


<RelationshipSubscription


RelType=$MPElement[Name=’WorkItem!System.WorkItemAssignedToUser’]$ Name of the relationship


SourceType=$MPElement[Name=’CoreActivity!System.WorkItem.Activity’]$ Type of object (restrict it to Activity and derived classes)


TargetType=$MPElement[Name=’System!System.Domain.User’]$> Type of object (restrict it to Domain User and derived classes)


                <AddRelationship /> When a new instance of relationship is added


              </RelationshipSubscription>


c.       WriteAction defines the “action” you want to perform on each instance that was discovered by the rule


                                   i.      Write Action is generally associated with an Activity/Task.


                                  ii.      In this case we are re-using a workflow that is used by “Change Request Event Workflow Configuration” and “Activity Event Workflow Configuration”.


                                iii.      Note:  This is not a published activity in our Authoring library, so the parameters potentially could change in future


<AssemblyName>Microsoft.EnterpriseManagement.WorkflowFoundation</AssemblyName>                <WorkflowTypeName>Microsoft.EnterpriseManagement.WorkflowFoundation.ConfigurationWorkflow</WorkflowTypeName>


                                iv.      The workflow takes the following parameters.


1.       InstanceId – The identifier for your relationship instance. This is required.


<WorkflowParameter Name=InstanceId Type=guid>$Data/BaseManagedEntityId$</WorkflowParameter>


2.       For notifications you need the following parameters.


   <WorkflowArrayParameter Name=UserRelationshipIdList Type=guid>                           <Item>$MPElement[Name=’WorkItem!System.WorkItemAssignedToUser’]$</Item>


   </WorkflowArrayParameter>


 


   <WorkflowArrayParameter Name=NotificationTemplateIdList Type=guid>


<Item>$MPElement[Name=’ActivityAssignedToUserNotificationTemplate’]$</Item>


   </WorkflowArrayParameter>


 


This is an array parameter. So you can specify multiple relationships and notification templates.


The order of the items will determine the template used for the relationship. The first notification template will be used for the first relationship, the second template will be used for the second relationship and so on.. The number of items in both array parameters above should be the same.


 If your relationship or notification template is an a different unsealed mp you will have to put the guid Id with the Item tag above.


 In order to get the GUID for your own template from the DB you can use a simple query. “select Objecttemplateid from ObjectTemplate where ObjectTemplateName='<name of the template>’


3.       For applying template the workflow takes the following parameter.


   <WorkflowParameter Name=TemplateId Type=guid> MPElement[Name='<Name of template>’]</WorkflowParameter>


You can also use this workflow to apply a template to the activity. The template is applied before the mail is sent.


5.        Language Pack section:


a.       This enables display of user friendly names in the UI.


b.      You should also review the blog for details regarding localizing the UI


 


4.       How to perform a similar task for a different type:


You can use the above workflow for other types as well.


For example, if you want do similar task for a Change Request assignment change.


You need to create or use an existing change request notification template and replace references to System.WorkItem.Activity with System.WorkItem.ChangeRequest. You will have to add a reference to the System.WorkItem.ChangeRequest.Library library in the Manifest->References section and use the correct alias when refering to the type.