SharePoint? Meet Python

Ever have a need to quickly script some administrative task relating to SharePoint? Ever wish there was more you could do with STSADM? Ever wish you had an interactive console for exploring the object model and testing various methods or properties? Well Iron Python is your answer. In this short article I’ll show you how to get up and running with Iron Python and SharePoint. I’ll also show you a few scripts that will give you some insight into the potential power of this extremely useful combination.

About a year and a half ago I was talking with an associate and he mentioned Iron Python and how it could apply to SharePoint. At the time I was working on other projects but I filed it away as something to play with when I had time. Once I finally got around to it I started to realize just how useful this combination is. I won’t go into the details of what Iron Python is specifically, you can read about it on the site. But essentially you get a powerful scripting language with full access to the SharePoint object model. On top of that, Python has an interactive console so it’s easy to load up an object, poke around its properties, and inspect exactly what each object does.

Here’s a quick example of a Python script enumerating all sites in a site collection and printing out the URL and root web name:

webapp = SPWebApplication.Lookup(Uri("http://mysite"))
for site in webapp.Sites:
  print '%-30s - %s' % (site.Url, site.RootWeb.Title)

Ever need to quickly get the internal name of a particular field?

list = SPSite("http://mysite").RootWeb.Lists["Documents"]
for field in list.Fields:
  print field.InternalName

Okay – these two examples might be fairly simplistic, but the point is Iron Python can be a much quicker and easier way of doing things with the SharePoint object model that you would normally have to write a custom console application for.

Getting Started

First, download Iron Python and install it on your SharePoint server. Next, fire up a console and type “ipy“. This will launch the Iron Python interactive console.

Next, type the following lines:

>>> import clr
>>> clr.AddReference("Microsoft.SharePoint")
>>> from Microsoft.SharePoint import *

Let’s look at what we’ve done. In Python the “import” statement is like the “using” statement in C#. It tells the interpreter that we want to use a particular module. In this case, we’re importing the interface to the .NET Common Language Runtime.

Next, clr.AddReference() is used to tell Iron Python that we want to load the Microsoft.SharePoint assembly. This same approach can be used to load any SharePoint assembly (or any .NET assembly for that matter). Finally, the last line imports all objects in the Microsoft.SharePoint namespace. This only imports objects in the Microsoft.SharePoint namespace. If you want to load objects from another namespace, you’ll need to import those as well.

At this point, you’re ready to go. Try out these lines to explore the object model.

>>> site = SPSite("http://myserver")
>>> for web in site.AllWebs:
. . .     print web.Title

>>> list = site.RootWeb.Lists["Documents"]
>>> list.Title = "Python Documents"
>>> list.Update()
>>> print list.Title

Unit Testing and Debugging

The interactive console can be extremely useful for unit testing your code as well as debugging. Since I’ve started using Iron Python I’ve often kept a console up while working on whatever application I’m building. It’s come in extremely handy when you just want to look up a particular method or see exactly what the format is of a particular piece of data. But another useful aspect is that it can be used to quickly test out the code that you write.

Iron Python can load any .NET assembly. So if you want to test out a method you’ve just written, load it up in the Iron Python interactive console and test it out. Building a real unit testing environment can be difficult in SharePoint because of the long setup and teardown times. This can be a happy compromise between a full on unit testing setup.

Cooking with Gas

One thing I hate is tech sites that show elementary examples and don’t really get into the complexity that is sure to arise once you move past “Hello World”. So I’ve uploaded a sample script that can be used to back up all sites in a given web application. This example combines a number of concepts such as:

  • running stsadm commands from python
  • importing parts of the .NET framework into your scripts
  • functions as objects in python

Download the script here.

Conclusion

I’ve been very impressed by the power of Iron Python and what means for us SharePoint developers. My prediction is that by using scripting tools like Iron Python, developers and administrators will begin to write scripts to automate a number of common SharePoint activities. Personally I’d really like to see some of the following:

  • Mass check-in/publish scripts
  • Scripts to upload a given web part to every page in a site
  • Data/content migration

Reference