Just Add Code

Creating SharePoint’s SafeControl Entries in PowerShell

Manually adding a new assembly for anWebPart to SharePoint can be a hassle. I always hate tracking down the various parts of the SafeControl line to add to the web.config. If you’ve ever done this manually before, you know that the most time consuming part is finding the public key token My preferred way had been to use ‘sn.exe -T someAssembly.txt’, but in .NET 2.0 sn.exe was no longer included with the default install. Once again, PowerShell gave me a simple solution:

1
2
3
$assembly = [System.Reflection.Assembly]::LoadFile("c:\temp\TestWebPartLibrary.dll") 
$name = $assembly.GetName() 
"<SafeControl Assembly=""{0}"" Namespace=""{1}"" TypeName=""*"" />" -f $assembly.FullName,$assembly.GetName().Name

The first line loads a dll into PowerShell and stores the resulting System.Reflection.Assembly in a variable. The next line stores the results of Assembly.GetName() into a local variable we can use later. The final line similar to String.Format in .NET. $assembly.FullName outputs the entire value for Assembly we need, including that pesky public key token!  For the Namespace attribute, I’m just using the name of the assembly. This isn’t necessarily true in all cases, but I usually have the dll and namespace sharing the same name.

When running these three lines I get the following familiar result:

<SafeControl Assembly="TestWebPartLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=82a1533dcdde9e6a" Namespace="TestWebPartLibrary" TypeName="*" />

Since we’re now able to create the line for a single .dll, lets wrap it up in a function so we can get a bit more mileage out of it.

function get-safecontrolline($assemblyLocation) 
{
   $assembly = [System.Reflection.Assembly]::LoadFile($assemblyLocation)
   $name = $assembly.GetName()
   "<SafeControl Assembly=""{0}"" Namespace=""{1}"" TypeName=""*"" />" -f $assembly.FullName,$assembly.GetName().Name 
}

Now we can call it and pass in the file as an argument. Now its a snap to generate the SafeControl entries for an entire directory!

C:Temp > dir *.dll|%{get-safecontrolline($_) } 
<SafeControl Assembly="TestWebPartLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=82a1533dcdde9e6a" Namespace="TestWebPartLibrary" TypeName="*" /> 
<SafeControl Assembly="AnotherLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=82a1533dcdde9e6a" Namespace="AnotherLibrary" TypeName="*" />        

C:Temp >
3 comments Digg this

3 Comments so far

  1. [...] Creating SharePoint’s SafeControl Entries in PowerShell [...]

  2. mike November 20th, 2009 2:36 am

    I cant seem to get this to run… any idea, I just keep getting the error that it cannot find the get-safecontrolline function

  3. neil December 30th, 2009 10:12 am

    The get-safecontrolline function is the code in the body of the post. If you paste that into PowerShell, you should be able to run it. If you’d like to always have it around, add it to your profile and it’ll always be available to you to use.

Leave a reply