{"id":76,"date":"2010-10-26T19:34:55","date_gmt":"2010-10-26T18:34:55","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=76"},"modified":"2011-01-24T22:53:55","modified_gmt":"2011-01-24T22:53:55","slug":"add-authorizedtypes-through-code","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2010\/10\/26\/add-authorizedtypes-through-code\/","title":{"rendered":"Add AuthorizedTypes Through Code"},"content":{"rendered":"<p>If you&#8217;ve created your own workflow activities for SharePoint and want to make the deployment process as simple as possible, one thing you&#8217;ll need to do is automate the updating of Authorized Types in the configuration files for the farm.<\/p>\n<p>This thankfully is fairly straight forward and helps to remove a manual step from your deployment process.<\/p>\n<p>Best practice here is to create a feature to deploy the workflow that uses your new activities.  Or, if you aren&#8217;t creating a workflow, I recommend you deploy the actions file (for SharePoint designer) in a solution.  Just for reference, the actions file should be deployed to the templates\\[language code]\\workflow folder.  Whichever way you decide to deploy, you&#8217;ll need to create a feature and receiver, as it&#8217;s the FeatureActivated method that we&#8217;ll use to update the web config files in the farm.<br \/>\n<!--more--><\/p>\n<pre lang=\"csharp\">private SPWebConfigModification GetAuthorizedTypeConfigNode(string assembly, string assemblyNamespace)\r\n{\r\n    SPWebConfigModification newAuthorizedType = null;\r\n\r\n    try\r\n    {\r\n        newAuthorizedType = new SPWebConfigModification();\r\n\r\n        newAuthorizedType.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;\r\n        newAuthorizedType.Name = string.Format(\"authorizedType[@Assembly='{0}'][@Namespace='{1}'][@TypeName='*'][@Authorized='True']\", assembly, assemblyNamespace);\r\n        newAuthorizedType.Path = \"configuration\/System.Workflow.ComponentModel.WorkflowCompiler\/authorizedTypes\";\r\n        newAuthorizedType.Owner = assemblyNamespace;\r\n        newAuthorizedType.Sequence = 0;\r\n        newAuthorizedType.Value = string.Format(\"<authorizedType Assembly='{0}' Namespace='{1}' TypeName='*' Authorized='True' \/>\", assembly, assemblyNamespace);\r\n    }\r\n    catch (Exception ex)\r\n    {\r\n        \/\/ Log error\r\n    }\r\n\r\n    return newAuthorizedType;\r\n}<\/pre>\n<p>You&#8217;ll need to reference the <em>Microsoft.SharePoint.Administration<\/em> namespaces for the above code.<\/p>\n<p>The <em>SPWebConfigModification <\/em>class is used to maintain a list of changes to the web.config file, which in our case is the addition of one or more authorization types.<\/p>\n<p>The <em>Type <\/em>property of the <em>newAuthorizedType <\/em>object is set to <em>EnsureChildNode <\/em>as the configuration change we are making relates to a child node of the main <em>AuthorizedTypes<\/em> section.<\/p>\n<p>OK, now that we have a method to create a <em>SPWebConfigModification<\/em> object for authorization types, the next step is to call the method for each assembly and namespace used by your workflow activities from the <em>FeatureActivated<\/em> method.<\/p>\n<pre lang=\"csharp\">public override void FeatureActivated(SPFeatureReceiverProperties properties)\r\n{\r\n    SPSite site = properties.Feature.Parent as SPSite;\r\n    if (site == null)\r\n    {\r\n        SPWeb web = properties.Feature.Parent as SPWeb;\r\n        site = web.Site;\r\n    }\r\n    \r\n    SPWebApplication webApp = site.WebApplication;\r\n    SPWebConfigModification authorizedTypeActivity = GetAuthorizedTypeConfigNode(\"yournamespace.Workflow.Activities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=831e23a22e1a47c4\", \"yournamespace.Workflow.Activities\");\r\n\r\n    if (authorizedTypeActivity != null)\r\n    {\r\n        webApp.WebConfigModifications.Add(authorizedTypeActivity);\r\n        webApp.Update(true);\r\n        webApp.WebService.ApplyWebConfigModifications();\r\n    }\r\n}<\/pre>\n<p>To remove modifications on the <em>FeatureDeactivating<\/em>event, change the call to Add on the WebConfigModifications object to Remove.<\/p>\n<pre lang=\"csharp\">        webApp.WebConfigModifications.Remove(authorizedTypeActivity); \/\/ Remove from web.config\r\n        webApp.Update(true);\r\n        webApp.WebService.ApplyWebConfigModifications();\r\n<\/pre>\n<p>Setting the ensure parameter of the Update method to true will cause existing settings to be overwritten if they already exist.  Setting this to false or omitting it completely will cause an error to be thrown if any setting exists.<\/p>\n<p>The above code doesn&#8217;t have to be for updating authorized types and can be tailored to update any setting in the web.config file.  You just need to update the <em>SPWebConfigModification<\/em> object used in the <em>GetAuthorizedTypeConfigNode<\/em> method to your target setting.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;ve created your own workflow activities for SharePoint and want to make the deployment process as simple as possible, one thing you&#8217;ll need to do is automate the updating of Authorized Types in the configuration files for the farm. &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2010\/10\/26\/add-authorizedtypes-through-code\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[3],"tags":[81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-1e","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/76"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":17,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"predecessor-version":[{"id":91,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/76\/revisions\/91"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}