SharePoint? Meet Python

August 12, 2007

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.


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



5 Responses to “SharePoint? Meet Python”

  1. Blaine Says:


    I am in _desperate_ need of interfacing python to SharePoint. Your tutorial looks promising, but I’m having trouble starting. This is what I get when I try to start off with Microsoft.SharePoint:

    IronPython 1.1 (1.1) on .NET 2.0.50727.832
    Copyright (c) Microsoft Corporation. All rights reserved.
    >>> import clr
    >>> clr.AddReference(“Microsoft.SharePoint”)
    Traceback (most recent call last):
    File , line 0, in ##10
    File , line 0, in AddReference##15
    IOError: Could not add reference to assembly Microsoft.SharePoint

    Any suggestions? I am running this on my desktop machine. I am currently a ‘user’ on our network, and do not have direct administrative access to the SharePoint Server. Am I able to run IronPython on my local PC?

    Any comments you have would be very helpful!

  2. glen Says:

    Two things.

    1) You need to have Microsoft.SharePoint.dll somewhere where it can be loaded by the clr.AddReference method. If you are running on a SharePoint server, the DLL is in the GAC so it will load fine. If you’re running on a desktop, you will probably need to get the DLL and make sure it’s accessible by Iron Python. See the Iron Python documentation for more details on how it loads assemblies with the AddReference method.

    2) You will need to run this on a SharePoint server to actually use the object model in any way. In this regard, Iron Python is just like building a custom console app in C#. You can script it on your machine, but you have to put it on the server to run it.

  3. Jared Says:

    hi i enjoyed the read

  4. […] SharePoint? Meet Python « Look alive. Here comes a buzzard. SharePoint? Meet Python « Look alive. Here comes a buzzard. […]

  5. Sudhakar Says:

    Nice article Glen.
    I have done couple of projects in Python and am charmed by the power and ease of use in python.
    I have been longing to work on that language.
    I will try to develop some sample apps when I find time.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: