{"id":1073,"date":"2011-07-05T18:44:28","date_gmt":"2011-07-05T23:44:28","guid":{"rendered":"http:\/\/blogs.devhorizon.com\/reza\/?p=1073"},"modified":"2011-07-05T18:56:21","modified_gmt":"2011-07-05T23:56:21","slug":"programmatically-associating-a-workflow-to-a-site","status":"publish","type":"post","link":"https:\/\/blogs.devhorizon.com\/reza\/2011\/07\/05\/programmatically-associating-a-workflow-to-a-site\/","title":{"rendered":"Programmatically Associating a Workflow to a Site"},"content":{"rendered":"<p>When you deploy a site-level workflow using Visual Studio 2010, Post-Deployment build configuration takes care of activating the workflow feature, and programmatically associates the workflow to the site.<\/p>\n<p>When you deploy the wsp package using PowerShell or stsadm , the magic won\u2019t happen! So, if you go to View All Site Content &gt; Site Workflows, the page is pretty empty with a message saying that \u201cthere are no workflows currently available to start on this site\u201d<\/p>\n<p><a href=\"https:\/\/blogs.devhorizon.com\/reza\/wp-content\/uploads\/2011\/07\/noworkflow.PNG\" title=\"noworkflow.PNG\"><img decoding=\"async\" src=\"https:\/\/blogs.devhorizon.com\/reza\/wp-content\/uploads\/2011\/07\/noworkflow.PNG\" alt=\"noworkflow.PNG\" \/><\/a><\/p>\n<p>What you need to do is to put the following code in your feature receiver:<\/p>\n<p>[CSharp]<br \/>\nprivate string workflowTemplateBaseGuid = &#8220;1e547b46-3d00-43b0-928d-4fe103b92fcb&#8221;;<br \/>\nprivate string workflowAssociationName  = &#8220;My Workflow&#8221;;<br \/>\nprivate string workFlowHistoryListName = &#8220;Workflow History&#8221;;<br \/>\nprivate string workFlowTaskListName = &#8220;Workflow Tasks&#8221;;<\/p>\n<p>public override void FeatureActivated(SPFeatureReceiverProperties properties)<br \/>\n{<br \/>\n    var site = properties.Feature.Parent as SPSite;<br \/>\n    var web = site.RootWeb;<br \/>\n    SPWorkflowTemplateCollection workflowTemplates = web.WorkflowTemplates;<br \/>\n    SPWorkflowTemplate workflowTemplate =<br \/>\n        workflowTemplates.GetTemplateByBaseID(new Guid(workflowTemplateBaseGuid));<\/p>\n<p>    if (workflowTemplate != null)<br \/>\n    {<br \/>\n        \/\/ Create the workflow association<br \/>\n        SPList taskList = web.Lists[workFlowTaskListName];<br \/>\n        SPList historyList = web.Lists[workFlowHistoryListName];<br \/>\n        SPWorkflowAssociation workflowAssociation =<br \/>\n            web.WorkflowAssociations.GetAssociationByName<br \/>\n            (workflowAssociationName, CultureInfo.InvariantCulture);<\/p>\n<p>        if(workflowAssociation ==null)<br \/>\n            {<br \/>\n                workflowAssociation = SPWorkflowAssociation.CreateWebAssociation<br \/>\n                    (workflowTemplate, workflowAssociationName, taskList, historyList);<br \/>\n                workflowAssociation.AllowManual = true;<br \/>\n                workflowAssociation.Enabled = true;<br \/>\n                web.WorkflowAssociations.Add(workflowAssociation);<br \/>\n            }<br \/>\n    }<br \/>\n}<br \/>\n[\/CSharp]<\/p>\n<p>One thing to note is that in sharePoint 2010, <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.workflow.spworkflowmanager.getworkflowtemplatesbycategory.aspx\">SPSite.SPWorkflowManager.GetWorkflowTemplatesByCategory<\/a>() doesn&#8217;t properly return the workflow template collection like SharePoint 2007. So, I had to fall back to the traditional way of using SPWeb.WorkflowTemplates to get my workflow template and create the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.spweb.workflowtemplates.aspx\">SPWorkflowAssociation<\/a> object.<\/p>\n<p>Obviously, in your FeatureDeactivating code, you need to add the necessary code to remove the current instances of the workflow; otherwise if you deactivate the feature and activate it, the workflow doesn&#8217;t show up again in View All Site Content&gt; Site Workflows again. A no-code workaround for this issue is to go to Site Settings &gt; Site Administration &gt; Workflow Settings; and remove the instance manually.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you deploy a site-level workflow using Visual Studio 2010, Post-Deployment build configuration takes care of activating the workflow feature, and programmatically associates the workflow to the site. When you deploy the wsp package using PowerShell or stsadm , the magic won\u2019t happen! So, if you go to View All Site Content &gt; Site Workflows, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-1073","post","type-post","status-publish","format-standard","hentry","category-sharepoint-2010"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Programmatically Associating a Workflow to a Site - Reza Alirezaei&#039;s Blog %<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blogs.devhorizon.com\/reza\/2011\/07\/05\/programmatically-associating-a-workflow-to-a-site\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Reza Alirezaei\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2011\\\/07\\\/05\\\/programmatically-associating-a-workflow-to-a-site\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2011\\\/07\\\/05\\\/programmatically-associating-a-workflow-to-a-site\\\/\"},\"author\":{\"name\":\"Reza Alirezaei\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"headline\":\"Programmatically Associating a Workflow to a Site\",\"datePublished\":\"2011-07-05T23:44:28+00:00\",\"dateModified\":\"2011-07-05T23:56:21+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2011\\\/07\\\/05\\\/programmatically-associating-a-workflow-to-a-site\\\/\"},\"wordCount\":314,\"commentCount\":7,\"image\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2011\\\/07\\\/05\\\/programmatically-associating-a-workflow-to-a-site\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/wp-content\\\/uploads\\\/2011\\\/07\\\/noworkflow.PNG\",\"articleSection\":[\"SharePoint 2010\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2011\\\/07\\\/05\\\/programmatically-associating-a-workflow-to-a-site\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2011\\\/07\\\/05\\\/programmatically-associating-a-workflow-to-a-site\\\/\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2011\\\/07\\\/05\\\/programmatically-associating-a-workflow-to-a-site\\\/\",\"name\":\"Programmatically Associating a Workflow to a Site - Reza Alirezaei's Blog %\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2011\\\/07\\\/05\\\/programmatically-associating-a-workflow-to-a-site\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2011\\\/07\\\/05\\\/programmatically-associating-a-workflow-to-a-site\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/wp-content\\\/uploads\\\/2011\\\/07\\\/noworkflow.PNG\",\"datePublished\":\"2011-07-05T23:44:28+00:00\",\"dateModified\":\"2011-07-05T23:56:21+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2011\\\/07\\\/05\\\/programmatically-associating-a-workflow-to-a-site\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2011\\\/07\\\/05\\\/programmatically-associating-a-workflow-to-a-site\\\/#primaryimage\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/wp-content\\\/uploads\\\/2011\\\/07\\\/noworkflow.PNG\",\"contentUrl\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/wp-content\\\/uploads\\\/2011\\\/07\\\/noworkflow.PNG\",\"width\":527,\"height\":102},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/\",\"name\":\"Reza Alirezaei's Blog\",\"description\":\"Blogging from the field!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\",\"name\":\"Reza Alirezaei\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"caption\":\"Reza Alirezaei\"},\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/author\\\/rezaa\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Programmatically Associating a Workflow to a Site - Reza Alirezaei's Blog %","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blogs.devhorizon.com\/reza\/2011\/07\/05\/programmatically-associating-a-workflow-to-a-site\/","twitter_misc":{"Written by":"Reza Alirezaei","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blogs.devhorizon.com\/reza\/2011\/07\/05\/programmatically-associating-a-workflow-to-a-site\/#article","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2011\/07\/05\/programmatically-associating-a-workflow-to-a-site\/"},"author":{"name":"Reza Alirezaei","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"headline":"Programmatically Associating a Workflow to a Site","datePublished":"2011-07-05T23:44:28+00:00","dateModified":"2011-07-05T23:56:21+00:00","mainEntityOfPage":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2011\/07\/05\/programmatically-associating-a-workflow-to-a-site\/"},"wordCount":314,"commentCount":7,"image":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2011\/07\/05\/programmatically-associating-a-workflow-to-a-site\/#primaryimage"},"thumbnailUrl":"https:\/\/blogs.devhorizon.com\/reza\/wp-content\/uploads\/2011\/07\/noworkflow.PNG","articleSection":["SharePoint 2010"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blogs.devhorizon.com\/reza\/2011\/07\/05\/programmatically-associating-a-workflow-to-a-site\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blogs.devhorizon.com\/reza\/2011\/07\/05\/programmatically-associating-a-workflow-to-a-site\/","url":"https:\/\/blogs.devhorizon.com\/reza\/2011\/07\/05\/programmatically-associating-a-workflow-to-a-site\/","name":"Programmatically Associating a Workflow to a Site - Reza Alirezaei's Blog %","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#website"},"primaryImageOfPage":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2011\/07\/05\/programmatically-associating-a-workflow-to-a-site\/#primaryimage"},"image":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2011\/07\/05\/programmatically-associating-a-workflow-to-a-site\/#primaryimage"},"thumbnailUrl":"https:\/\/blogs.devhorizon.com\/reza\/wp-content\/uploads\/2011\/07\/noworkflow.PNG","datePublished":"2011-07-05T23:44:28+00:00","dateModified":"2011-07-05T23:56:21+00:00","author":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blogs.devhorizon.com\/reza\/2011\/07\/05\/programmatically-associating-a-workflow-to-a-site\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blogs.devhorizon.com\/reza\/2011\/07\/05\/programmatically-associating-a-workflow-to-a-site\/#primaryimage","url":"https:\/\/blogs.devhorizon.com\/reza\/wp-content\/uploads\/2011\/07\/noworkflow.PNG","contentUrl":"https:\/\/blogs.devhorizon.com\/reza\/wp-content\/uploads\/2011\/07\/noworkflow.PNG","width":527,"height":102},{"@type":"WebSite","@id":"https:\/\/blogs.devhorizon.com\/reza\/#website","url":"https:\/\/blogs.devhorizon.com\/reza\/","name":"Reza Alirezaei's Blog","description":"Blogging from the field!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blogs.devhorizon.com\/reza\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938","name":"Reza Alirezaei","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","caption":"Reza Alirezaei"},"url":"https:\/\/blogs.devhorizon.com\/reza\/author\/rezaa\/"}]}},"_links":{"self":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/1073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/comments?post=1073"}],"version-history":[{"count":0,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/1073\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/media?parent=1073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/categories?post=1073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/tags?post=1073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}