Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I need to add a line in the web.config under authorizedtypes, I want to do it with a wsp solution so that sharepoint takes care of syncrhonizing it with other front end servers.

I just want to put this line:

<authorizedType Assembly="MyCompany.CustomActivities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=207090b9b3f674c8" Namespace="MyCompany.CustomActivities" TypeName="*" Authorized="True" />
share|improve this question
    
I found this site, msdn.microsoft.com/en-us/library/ff798302.aspx, but my dll is already deployed in the gac and I already know the public key.\ –  L.V. Sharepoint Architect Mar 23 '12 at 10:39
    
The important part of the msdn article isn't just building the right string. But more the use of SPWebConfigModification which will make the change to web.config on all servers (including future) –  Per Jakobsen Mar 23 '12 at 10:59

2 Answers 2

up vote 5 down vote accepted

I've implemented this method in the past, so I just share my code with you:
add:

private void AddAuthorizedType(SPWebApplication webApplication, string assembly, string namespace_)
{
    SPSecurity.RunWithElevatedPrivileges(delegate
     { 
         var modification = new SPWebConfigModification();
         modification.Path = "configuration/System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes";
         modification.Name = string.Format("authorizedType[@Assembly='{0}'][@Namespace='{1}'][@TypeName='*'][@Authorized='True']", assembly, namespace_);
         modification.Sequence = 0;
         modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
         modification.Value = string.Format("<authorizedType Assembly='{0}' Namespace='{1}' TypeName='*' Authorized='True'/>", assembly, namespace_);
         webApplication.WebConfigModifications.Add(modification);
         webApplication.Update();
         webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
     });
}

and remove:

private void RemoveAuthorizedType(SPWebApplication webApplication, string assembly, string namespace_)
{
    SPSecurity.RunWithElevatedPrivileges(delegate
    {
        SPWebConfigModification configMod = null;
        var modsCollection = webApplication.WebConfigModifications;

        // Find the most recent modification of a specified owner
        foreach (SPWebConfigModification modification in modsCollection)
        {
            if (modification.Name.Equals(string.Format("authorizedType[@Assembly='{0}'][@Namespace='{1}'][@TypeName='*'][@Authorized='True']", assembly, namespace_), StringComparison.OrdinalIgnoreCase))
            {
                configMod = modification;
            }
        }

        if (configMod != null)
        {
            // Remove it and save the change to the configuration database  
            modsCollection.Remove(configMod);
            webApplication.Update();

            // Reapply all the configuration modifications
            webApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
        }
    });
}

NOTE: make sure this feature has web application scope, because if not, your code may not work, due to RemoteAdministratorAccessDenied settings.

share|improve this answer
    
That last bit about access and scope is very important :) –  Rex M Mar 23 '12 at 12:32
    
@Kai, in my first link they say to make it farm scoped, :(, strange, I cant make it work!! see my other question here if you can help me. sharepoint.stackexchange.com/questions/32328/… –  L.V. Sharepoint Architect Mar 23 '12 at 12:52
    
@superxman, you can make it farm scope, but you should to know what web application's config you are going to modify. If you make web application scope, you can simply use functions like this - AddAuthorizedType(feature.Parent as SPWebApplication, ...) –  Kai Mar 23 '12 at 12:57

We've found it's much easier to just add a webconfig.something.xml file to the Config folder. So for your one entry you can create an xml file with these contents:

<?xml version="1.0" encoding="utf-8" ?>
<actions>
  <remove path="configuration/System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes/authorizedType[@Namespace=&quot;MyCompany.CustomActivities&quot;]" />
  <add path="configuration/System.Workflow.ComponentModel.WorkflowCompiler/authorizedTypes" >
    <authorizedType Assembly="MyCompany.CustomActivities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=207090b9b3f674c8" Namespace="MyCompany.CustomActivities" TypeName="*" Authorized="True" />
  </add>
</actions>

Name this file in the form webconfig.something.xml replacing something with your Solution Name or something else to distinguish it.

This xml file can be part of your solution set to copy itself to [HivePath]\CONFIG folder. If you are creating your solution in Visual Studio just add a new Root File (SharePoint file-system template file). Add a CONFIG folder under the RootFiles folder and place your new xml file there.

Then whenever web.config rebuilds itself it will do so with your actions specified above. The first time you deploy the solution you will need to add an extra step of calling:

stsadm -o copyappbincontent

This is true for both SharePoint 2007 and SharePoint 2010.

share|improve this answer
1  
interesting solution, and of course faster. thanks for sharing this. –  Kai Mar 23 '12 at 13:22
2  
Nice solution. If it's of interest, here is the MSDN documentation on this msdn.microsoft.com/en-us/library/ms439965.aspx –  Kirk Jul 24 '13 at 20:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.