{"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"],"_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}]}}