Manually adding a new assembly for a WebPart 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 >
Last weekend I was able to present two sessions at the Twin Cities Code Camp. One on WebPart Development and the other on PowerShell.
I covered some common development scenarios: basic webparts, connectable webparts and using ASP User Controls with the SmartPart. I have all the code from the presentation.
This session was hard to prepare for because I wanted to have everyone leave understanding all the great functionality of PowerShell and able to use it for more than a basic shell. I think it turned out pretty well. I was able to cover a lot of different areas of PowerShell in a reasonably coherent manner. The group seemed pretty interested and asked some really great questions. One of those questions taught me a valuable lesson: “Don’t do a recurse get-content to the screen unless you’re sure there aren’t any binary files in the directories”. During the course of attempting to answer a question I accidentally did a ‘get-content’ against a large ISO file. My computer wouldn’t stop beeping, no amount of CTRL-C or Task Kill would make it stop. I ended up having to do the last 5 minutes without my deck..presentations are always fun!
Special thanks to Karl for providing me with some copies of PowerShell Analyzer to give away to the attendees.
Links from the session:
At the last Code Camp I presented on SharePoint, this time on WebParts and PowerShell…I think there’s a trend in me presenting on topics with BigLetters in their names. We’ll see what happens next time! Thanks again to Jason for putting everything together.
The PowerShell Remoting project now has a new home on CodePlex. Since GotDotNet is phasing out their workspaces, it was a good time to move the project over to CodePlex. I’m already enjoying the TFS based source control. There is also a good interface to use for bug tracking. Now is also a great time to request a new feature so we can get it into the next release.
Not familiar with the PowerShell Remoting project? PowerShell Remoting is a client/server app that allows you to use PowerShell to connect to another host and issue commands. Think of it as SSH for Windows, only instead of a something like an .rhosts file, you can log in with your domain credentials to skip entering a password. If you have to manage multiple machines, and you like PowerShell, this should probably be in your toolkit.
Believe it or not, PowerShell is more than just fun and games. You can even do productive things with it! A common scenario I run into is the need to create a new virtual machine. Usually I want to base it on one I’ve already created and I want it to join a domain. There are a couple issues that I run in to:
- Duplicate SID Error
- Too many clicks
The first problem can be solved with a free tool that Microsoft has released called NewSID. Running that will give my virtual a unique SID so that it can be added to the domain.
Now for the second problem..too many clicks! Renaming a machine and adding it to the domain takes almost 2 minutes and a nearly infinite number of clicks and keystrokes…or maybe not. At any rate, it’d be great to simplify this process. Enter PowerShell.
Rename a Computer and Join Domain
$comp = get-wmiobject Win32_ComputerSystem
$comp.Rename(“newComputerName”,”password”,”administrator”)
$comp.JoinDomainOrWorkGroup(“MYDOMAIN”,”domainPassword”,”MYDOMAINdomainAdmin”,$null,3)
Add a call to NewSID, wrap those delicious lines in a function and you are well on your way to renaming a virtual and joining a domain in fewer keystrokes. Its so easy you might even find yourself creating new virtuals just to run it again.
Here’s the presentation I did for the Twin Cities Code Camp around SharePoint as a Development Platform.
Welcome to those of you hitting here from the Twin Cities Code Camp. As you can probably see, there are a few cobwebs here.
I’ll be doing a presentation on using SharePoint as a Development Platform. With all the recent activity around the SharePoint 2007 release, developers (or at least their bosses) are more interested in how they can use SharePoint in their organization. I’ll be covering what things to consider when deciding whether or not to use SharePoint, and then how to actually develop againist it. There will be code examples for the three major approaches (object model, web services and RPC).
All the code and slides will be posted here for those who couldn’t make it, or really want to try out the code.
After three years of failing my resolution, I finally have a blog. My goal is to have a blog filled with technical content, not just links to ‘cool’ things. My interests in Tech are pretty diverse, and I realize not everyone who reads this blog will be as interested in all of those things as I am. To better facilitate readers that only enjoy certain topics, I’ll be using Categories and providing Category level RSS feeds. For those trying to find a specific article, hopefully the Search bar will help you out.
The guts behind this blog is Typo. Typo is a Ruby on Rails blogging application. Why’d I decide to use this? Well, I develop with MS products for my day job and I decided that doing something different for fun is always good. I’ve been following Rails pretty much since its inception. Why didn’t I choose Wordpress or Moveable Type/Typepad? They run on non-MS platforms too. I’ve looked at Moveable Type, and it just doesn’t feel like any fun. And I had been leaning toward using Wordpress (and even had it installed for one of the year’s resolutions), and having a strong PHP background it definitely made sense. But the big reason was I’m tired of patching. PHP needs patching, Wordpress needs patching…constantly. There’s only so much time in the day. Ruby and Typo have and will have similar problems, but the process for upgrading is simpler that I’d rather go with it. We’ll see how it works out.
Anyway, welcome. Take your shoes off, stay a while.