Just Add Code

Extending PowerShell – Code Camp IV

For the fourth Twin Cities Code Camp I gave a presentation on Extending PowerShell for developers.  Turnout was great yet again at Code Camp, pretty much every session filled up.  The enthusiasm of the people who attend these sessions really make it a fun place to present.  I had a great group in my session, they helped make it into a very interactive presentation.

I started with scripting in PowerShell, then moved into dynamically adding members (properties and functions) to objects.  The PS1XML file is available for download.  Additionally, if you were interested in another example of this, the PowerShell for SharePoint Developers session I did at DevConnections has another good example of where a PS1XML can be used.

A Cmdlet and SnapIn were created using David Aiken‘s great PowerShell Visual Studio templates.  It demo’d passing parameters to a Cmdlet and having it return an array of items back to the Pipeline.  I also showed two implementations of hosting PowerShell; one for running a command from your app, and the other for replacing the UI entirely.

Slides
Code

No comments Digg this

Deleting SharePoint Site Alerts

A question came up on the #sharepoint Freenode IRC channel (join us) about how to easily delete all the alerts for a site.  He had come across this post about creating a web part to do exactly what he wanted.  However, since he is an administrator and the post requires creating and deploying a web part it seemed like a lot of extra work. Enter PowerShell.

The solution took the form of a simple 4 line PowerShell script:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = new-object Microsoft.SharePoint.SPSite("http://mydomain.com/someweb")
$web = $site.OpenWeb()
"I'd delete {0} alerts" -f $web.Alerts.Count #Only warns about deleting

If you pasted the code above into your environment (replacing the web URL with your own) it will tell you how many Alerts are associated with the site. If you really want to DELETE all of them, you can add the following line of code:

$web.Alerts|%{$web.Alerts.Delete($_.Id)} #Actually Deletes them!

Since you are already in PowerShell, there are a couple other interesting things you can do. You could filter for only the alerts in a single list:

$web.Alerts|?{$_.ListUrl -like "Documents"} #assumes http://mydomain.com/someweb/documents

Or you could find all the Alerts for a specific user across the web:

$web.Alerts|?{$_.UserId -like "DOMAIN\username"}

But wait, there’s more! If you wanted to see all the alerts for a user across the entire site collection:

$site.AllWebs|select -expand Alerts|?{$_.UserId -like "DOMAIN\username"}
No comments Digg this

Announcing Minneapolis Office Developer Interest Group (MODIG)

A new group is starting up in the Twin Cities for developers on the Office platform. This is a great place to go to learn more about developing for both the Office Servers (Sharepoint) and the Office Clients (Word, Excel, Outlook, …) and to network with people working on similar projects. As is important with all groups, it needs a catchy name. So the obvious choice was ‘Minneapolis Office Developer Interest Group’, or maybe not. At least it shortens to the more-easily-pronounceable MODIG.

As a sort of kickoff for the group, this month’s MNSPUG is on understanding what customizations and development opportunities SharePoint 2007 allows. It’ll provide a high level overview of the sorts of things that can be done on the SharePoint platform.

Our first MODIG meeting will cover creating Solutions and Features in SharePoint, discussing some approaches to use them successfully, and plenty of ways to use them unsuccessfully. The meeting will be held at Microsoft’s office in Bloomington at 5:30pm on Feb 21. We’ve got a temporary MODIG site up right now that will be hosting the updated information for the group.


Update:

Slides
Code

2 comments Digg this

DevConnections – PowerShell for SharePoint Developers

My final presentation at SharePoint Connections was the one I’d been looking forward to the most.  I’m a big fan of using PowerShell and SharePoint together, it was great to show a crowd some of the thing they could do.  I’d done a couple hour PowerShell presentations (that always seemed to run long) before, but never with SharePoint.  The challenge this time was to do them both together…in an hour.  I was able to get through everything I’d planned and now I have some more detailed content that didn’t fit into the presentation that I can put into some future posts.

The deck is now uploaded, and as promised, you didn’t miss out if you couldn’t write down all my commands.  Nearly all of the commands are here, incorrect statements and all.  I also have my profile, so you can see how I cheated in my other presentations.

I also made a basic PowerShell Types (ps1xml) file to demonstrate the how to extend familiar types to make them easier to work with.  You can grab the file and use it in your own development environment, though I probably wouldn’t rely on it in production quite yet.  Remember to get it started you need to:

Update-TypeData -prepend JustAddCode.Types.ps1xml

3 comments Digg this

DevConnections – Customizing the SharePoint Mobile Experience

Here is the deck and code I presented at last week’s SharePoint Connections conference in Las Vegas.  The deck stands alone pretty well as an overview to what you can do with SharePoint’s mobile experience.  The code gets much deeper and actually implements a SharePoint aware mobile control that you can put in one of your customized templates, and a custom field with different support for rendering on a mobile device.

While putting this presentation together I looked for other examples of people actively utilizing and customizing SharePoint’s mobile abilities and came up empty.  If you are already doing something mobile with SharePoint or you check out the code and start going down that path, let me know.  I’m interested in seeing who else out there is looking into this stuff.

6 comments Digg this

DevConnections – Advanced Feature Development

One session down, two to go.  I got my first DevConnections session under my belt.  One of the things I’ll try to do is turn some of my slides and discussion points into charts/datasheets.  The feature ‘gotchas’ and ActivationDependency restrictions are excellent candidates for this.

As promised, here is the slide deck and code samples.  Enjoy!

2 comments Digg this

DevConnections – Day 1

Today was my first full day at DevConnections in Vegas.  I spent part of the day going to sessions, and squeezed some time in for some last minute work on my sessions.  Its been great to be here with some of my coworkers, including Mike who’s been posting some great updates of the conference.

My Speaking Schedule:

  • Wednesday
    • Advanced Feature Development
      • When: 2:30pm
      • Where: Mandalay Bay B
  • Thursday
    • Customizing the SharePoint Mobile Experience
      • When: 11:15am
      • Where: Mandalay Bay B
    • PowerShell for SharePoint Developers
      • When: 2pm
      • Where Mandalay Bay B

I’ll be posting the slides and code for the sessions after I give them.

If you’re in town for DevConnections, drop me a line!

No comments Digg this

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

Code Camp II Followup

Last weekend I was able to present two sessions at the Twin Cities Code Camp. One on WebPart Development and the other on PowerShell.

WebPart Development Chalk Talk

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.

Introduction to PowerShell

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.

No comments Digg this

PowerShell Remoting has Moved

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.

No comments Digg this

« Previous PageNext Page »