Add AuthorizedTypes Through Code

If you’ve created your own workflow activities for SharePoint and want to make the deployment process as simple as possible, one thing you’ll need to do is automate the updating of Authorized Types in the configuration files for the farm.

This thankfully is fairly straight forward and helps to remove a manual step from your deployment process.

Best practice here is to create a feature to deploy the workflow that uses your new activities. Or, if you aren’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’ll need to create a feature and receiver, as it’s the FeatureActivated method that we’ll use to update the web config files in the farm.

private SPWebConfigModification GetAuthorizedTypeConfigNode(string assembly, string assemblyNamespace)
{
    SPWebConfigModification newAuthorizedType = null;
 
    try
    {
        newAuthorizedType = new SPWebConfigModification();
 
        newAuthorizedType.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
        newAuthorizedType.Name = string.Format("authorizedType[@Assembly='{0}'][@Namespace='{1}'][@TypeName='*'][@Authorized='True']", assembly, assemblyNamespace);
        newAuthorizedType.Path = "configuration/System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes";
        newAuthorizedType.Owner = assemblyNamespace;
        newAuthorizedType.Sequence = 0;
        newAuthorizedType.Value = string.Format("<authorizedType Assembly='{0}' Namespace='{1}' TypeName='*' Authorized='True' />", assembly, assemblyNamespace);
    }
    catch (Exception ex)
    {
        // Log error
    }
 
    return newAuthorizedType;
}

You’ll need to reference the Microsoft.SharePoint.Administration namespaces for the above code.

The SPWebConfigModification 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.

The Type property of the newAuthorizedType object is set to EnsureChildNode as the configuration change we are making relates to a child node of the main AuthorizedTypes section.

OK, now that we have a method to create a SPWebConfigModification object for authorization types, the next step is to call the method for each assembly and namespace used by your workflow activities from the FeatureActivated method.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    SPSite site = properties.Feature.Parent as SPSite;
    if (site == null)
    {
        SPWeb web = properties.Feature.Parent as SPWeb;
        site = web.Site;
    }
 
    SPWebApplication webApp = site.WebApplication;
    SPWebConfigModification authorizedTypeActivity = GetAuthorizedTypeConfigNode("yournamespace.Workflow.Activities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=831e23a22e1a47c4", "yournamespace.Workflow.Activities");
 
    if (authorizedTypeActivity != null)
    {
        webApp.WebConfigModifications.Add(authorizedTypeActivity);
        webApp.Update(true);
        webApp.WebService.ApplyWebConfigModifications();
    }
}

To remove modifications on the FeatureDeactivatingevent, change the call to Add on the WebConfigModifications object to Remove.

        webApp.WebConfigModifications.Remove(authorizedTypeActivity); // Remove from web.config
        webApp.Update(true);
        webApp.WebService.ApplyWebConfigModifications();

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.

The above code doesn’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 SPWebConfigModification object used in the GetAuthorizedTypeConfigNode method to your target setting.

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve the maths problem shown below before posting: *

Follow

Get every new post delivered to your Inbox

Join other followers: