{"id":2523,"date":"2013-02-05T11:36:27","date_gmt":"2013-02-05T11:36:27","guid":{"rendered":"https:\/\/www.wapshere.com\/missmiis\/?p=2523"},"modified":"2013-02-05T11:36:27","modified_gmt":"2013-02-05T11:36:27","slug":"getting-something-back-from-the-enumerateresourcesactivity","status":"publish","type":"post","link":"https:\/\/www.wapshere.com\/missmiis\/getting-something-back-from-the-enumerateresourcesactivity","title":{"rendered":"Getting something back from the EnumerateResourcesActivity"},"content":{"rendered":"<p>I have just spent a very frustrating day trying to figure out how to get access to the resource found by the EnumerateResourcesActivity, not helped even slightly by the usual machine-generated &#8220;<a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ee623975(v=vs.100).aspx\">documentation<\/a>&#8220;. I&#8217;m still not very happy with it, and can&#8217;t for the life of me understand why it can&#8217;t give me a straight\u00c2\u00a0object collection as output, but at least I&#8217;ve got the data I need from it now.<\/p>\n<p><!--more--><\/p>\n<p>The EnumerateResourcesActivity is the recommended way to search for objects using an XPath filter. But, quite bizarrely, the only readily accessible data you get back from it is how many objects it found &#8211; not the actual objects! This is despite the fact that one of the input parameters is a list of attributes you want back. I feel cheated!!<\/p>\n<p>The only time I&#8217;d used this activity before was for <a href=\"https:\/\/www.wapshere.com\/missmiis\/generate-unique-attribute-activity\">checking uniqueness<\/a>, and all I needed then was an object count. But today I needed to write a custom Notification Activity and (as there&#8217;s no way I know of to add an Identity Picker to a custom workflow UI)\u00c2\u00a0I wanted to be able to specify the email template by it&#8217;s name.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" alt=\"\" src=\"https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2013\/02\/enumerate1.jpg\" width=\"723\" height=\"152\" \/><\/p>\n<p>Clearly this was going to involve an object lookup as\u00c2\u00a0I need to specify the template&#8217;s ResourceID going in to the <a href=\"https:\/\/www.wapshere.com\/missmiis\/how-to-use-the-emailnotificationactivity-in-a-custom-workflow\">EmailNotificationActivity<\/a>. In this example the &#8220;searchEmailTemplate&#8221; activity is the EnumerateResourcesActivity, and I only expect it to return one object.<\/p>\n<p style=\"text-align: center;\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" alt=\"\" src=\"https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2013\/02\/enumerate2.jpg\" width=\"143\" height=\"247\" \/><\/p>\n<p>This is where all the pain started, and if it hadn&#8217;t been for the following blog posts I never would have worked this out:<\/p>\n<p><a href=\"http:\/\/c--shark.blogspot.com.au\/2009\/04\/how-to-use-enumerateresourcesactivity.html#comment-form\">http:\/\/c&#8211;shark.blogspot.com.au\/2009\/04\/how-to-use-enumerateresourcesactivity.html#comment-form<\/a>\u00c2\u00a0(including comments as it was Luka who had the crucial bit of advice that you need to set <em>all<\/em> the properties on the EnumerateResourcesActivity going in).<br \/>\n<a href=\"http:\/\/bennettadelson.wordpress.com\/2012\/02\/20\/fim-2010-user-rcdc-to-add-user-to-groups\/\">http:\/\/bennettadelson.wordpress.com\/2012\/02\/20\/fim-2010-user-rcdc-to-add-user-to-groups\/<\/a><br \/>\n<a href=\"http:\/\/blog.clauskonrad.net\/2011\/11\/fim-2010-how-to-use-std.html\">http:\/\/blog.clauskonrad.net\/2011\/11\/fim-2010-how-to-use-std.html<\/a><br \/>\n<a href=\"http:\/\/www.fimspecialist.com\/fim-portal\/custom-workflow-examples\/custom-workflow-example-enumerate-resources-activity\/\">http:\/\/www.fimspecialist.com\/fim-portal\/custom-workflow-examples\/custom-workflow-example-enumerate-resources-activity\/<\/a><\/p>\n<p>So thank you very much lovely bloggers for\u00c2\u00a0sharing your experiences on this!<\/p>\n<h2>The Code<\/h2>\n<p>The first totally weird thing you have to do is hack the Designer code for your custom workflow. I&#8217;m using VB.NET so mine is called &lt;ActivityName&gt;.Designer.vb. I found I had to click &#8220;Show All Files&#8221; to see it:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone\" alt=\"\" src=\"https:\/\/www.wapshere.com\/missmiis\/wp-content\/uploads\/2013\/02\/enumerate3.jpg\" width=\"323\" height=\"231\" \/><\/p>\n<p>We have to completely ignore the message that says we shouldn&#8217;t edit this code and do the following:<\/p>\n<ul>\n<li>Add a declaration for codeActivity1 at the end of the class, and<\/li>\n<li>Add the lines as shown before &#8220;Me.CanModifyActivities = False&#8221; inside the InitializeComponent Sub.<\/li>\n<\/ul>\n<pre>&lt;Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()&gt; _\r\nPartial Class AlternateEmailNotification\r\n\r\n    <span style=\"color: #339966;\">'NOTE: The following procedure is required by the Workflow Designer\r\n    'It can be modified using the Workflow Designer.  \r\n    'Do not modify it using the code editor.\r\n<\/span>    &lt;System.Diagnostics.DebuggerNonUserCode()&gt; _\r\n    &lt;System.CodeDom.Compiler.GeneratedCode(\"\", \"\")&gt; _\r\n    Private Sub InitializeComponent()\r\n\t   <em>... (pre-existing code) ...<\/em>\r\n        <span style=\"color: #339966;\">'\r\n        ' Manually added code for the EnumerateResourcesActivity\r\n        '\r\n<\/span>        Me.codeActivity1 = New System.Workflow.Activities.CodeActivity()\r\n        Me.codeActivity1.Name = \"codeActivity1\"\r\n        AddHandler Me.codeActivity1.ExecuteCode, AddressOf Me.GetEmailTemplate_ExecuteCode\r\n        'Make the codeActivity the child of the EnumerateResourcesActivity \r\n        Me.searchEmailTemplate.Activities.Add(Me.codeActivity1)\r\n\r\n        Me.CanModifyActivities = False\r\n    End Sub\r\n\r\n    <em>... (pre-existing declarations) ...<\/em>\r\n\r\n    <span style=\"color: #339966;\">'Manually added code activity that allows access to objects returned by EnumerateResourcesActivity<\/span>\r\n    Private codeActivity1 As CodeActivity\r\nEnd Class<\/pre>\n<p>In the code for the activity itself (AlternateEmailNotification.vb in the picture above) I add the following code which is being called by that handler we added above. This does actually manage to get you the first object returned by the enumeration. (I still don&#8217;t know how to get at all the objects found but don&#8217;t need to for this activity &#8211; leaving that for another day.)<\/p>\n<pre>    Private Sub GetEmailTemplate_ExecuteCode(ByVal sender As System.Object, ByVal e As System.EventArgs)\r\n        Dim currentItem As ResourceType = TryCast(EnumerateResourcesActivity.GetCurrentIterationItem(DirectCast(sender, CodeActivity)), ResourceType)\r\n        If currentItem Is Nothing Then\r\n            Throw New WorkflowTerminatedException(\"Unable to find email template \" &amp; Me.Template)\r\n        End If\r\n        <span style=\"color: #339966;\">'TemplateID is a System.Guid declared globally<\/span>\r\n        TemplateID = currentItem.ObjectID.GetGuid\r\n    End Sub<\/pre>\n<p>Now back on more familiar ground, here&#8217;s the code activity that runs just before the EnumerateResourcesActivity to set the scene. Remember as I&#8217;ve mentioned you have to set all the properties. If you don&#8217;t you wind up with &#8220;Nothing&#8221; instead of a resource.<\/p>\n<pre>    Private Sub initSearch_ExecuteCode(ByVal sender As System.Object, ByVal e As System.EventArgs)\r\n        searchEmailTemplate_ActorId1 = New Guid(FIMADMIN_GUID)\r\n        Dim filter As String = \"\/EmailTemplate[DisplayName ='\" &amp; Me.Template &amp; \"']\"\r\n        searchEmailTemplate_XPathFilter1 = filter\r\n        searchEmailTemplate_Selection1 = New String() {\"DisplayName\"}\r\n        searchEmailTemplate_PageSize1 = 100\r\n        searchEmailTemplate_SortingAttributes1 = Nothing\r\n        searchEmailTemplate_TotalResultsCount1 = 0\r\n    End Sub<\/pre>\n<p>Finally now I set up my EmailNotificationActivity, which was at least as simple as advertised.<\/p>\n<pre>    Private Sub initNotification_ExecuteCode(ByVal sender As System.Object, ByVal e As System.EventArgs)\r\n        emailNotificationActivity_To1 = EmailOther\r\n        emailNotificationActivity_EmailTemplate1 = TemplateID\r\n    End Sub<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I have just spent a very frustrating day trying to figure out how to get access to the resource found by the EnumerateResourcesActivity, not helped even slightly by the usual machine-generated &#8220;documentation&#8220;. I&#8217;m still not very happy with it, and can&#8217;t for the life of me understand why it can&#8217;t give me a straight\u00c2\u00a0object collection&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[42,60,30,45],"tags":[],"class_list":["post-2523","post","type-post","status-publish","format-standard","hentry","category-fim-2010","category-fim-2010-r2","category-vbnet","category-workflow"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pkp1o-EH","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/2523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/comments?post=2523"}],"version-history":[{"count":10,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/2523\/revisions"}],"predecessor-version":[{"id":2536,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/posts\/2523\/revisions\/2536"}],"wp:attachment":[{"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/media?parent=2523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/categories?post=2523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wapshere.com\/missmiis\/wp-json\/wp\/v2\/tags?post=2523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}