<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Look alive. Here comes a buzzard. &#187; Python</title>
	<atom:link href="http://blog.glenc.net/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.glenc.net</link>
	<description>Treading water in a sea of man-made confusion.</description>
	<lastBuildDate>Wed, 01 Sep 2010 02:34:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>sp.py &#8211; SharePoint Scripting with Python</title>
		<link>http://blog.glenc.net/2008/05/18/sppy-sharepoint-scripting-with-python/</link>
		<comments>http://blog.glenc.net/2008/05/18/sppy-sharepoint-scripting-with-python/#comments</comments>
		<pubDate>Mon, 19 May 2008 06:23:32 +0000</pubDate>
		<dc:creator>glenc</dc:creator>
				<category><![CDATA[Office System]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.glenc.net/?p=70</guid>
		<description><![CDATA[I wrote a small introduction to SharePoint scripting with Python a while back and this weekend I didn&#8217;t have anything better to do (well actually I did, but I&#8217;m an addict &#8211; what can I say) so I decided to expand on it.  I&#8217;ve started putting together a small Python library for working with SharePoint [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a small <a href="http://blog.glenc.net/2007/08/12/sharepoint-meet-python/" target="_blank">introduction</a> to SharePoint scripting with Python a while back and this weekend I didn&#8217;t have anything better to do (well actually I did, but I&#8217;m an addict &#8211; what can I say) so I decided to expand on it.  I&#8217;ve started putting together a small Python library for working with SharePoint using <a href="http://www.codeplex.com/IronPython" target="_blank">IronPython</a>.  It&#8217;s called &#8211; amazingly &#8211; <a href="http://github.com/glenc/sp.py/tree/master/README" target="_blank">sp.py</a>.</p>
<p>So far the library if pretty small &#8211; although it does include a nice little module for workign with stsadm commands.  I&#8217;ll be adding to it as necessary.  I&#8217;ve decided that although adding <a href="http://stsadm.blogspot.com/" target="_blank">custom stsadm extensions</a> is nice and does fit into the &#8220;way to do things in SharePoint&#8221;, it&#8217;s just too much overhead when you want to do something simple.  So at this point when things come up that need automation, I&#8217;ll be adding new sp.py scripts.</p>
<p>In addition to the sp.py library I&#8217;ve added three scripts:</p>
<ul>
<li style="text-align: left;"><a href="http://github.com/glenc/sp.py/tree/master/src/applytheme.py" target="_blank">applytheme.py</a> &#8211; apply a theme to all webs in a web application</li>
<li><a href="http://github.com/glenc/sp.py/tree/master/src/backupsites.py" target="_blank">backupsites.py</a> &#8211; backup all site collections in a web application</li>
<li><a href="http://github.com/glenc/sp.py/tree/master/src/setprofileprop.py" target="_blank">setprofileprop.py</a> &#8211; set a property on all user profiles based on a dynamic pattern</li>
</ul>
<p>So where can you get this awesomeness?  Well, actually you can get it on <a href="http://github.com/glenc/sp.py/" target="_blank">github</a>.  Github???  You mean that totally web 2.0 site for hosting Git repositories?  Yeah &#8211; that&#8217;s the place.  If you&#8217;re wondering why I&#8217;m using git for source control, well I guess Subversion just wasn&#8217;t &#8220;edge&#8221; enough anymore.  Git&#8217;s actually pretty cool &#8211; and if you need to use Windows you can <a href="http://git.or.cz/#download" target="_blank">download a Windows version here</a>.</p>
<p>At some point I&#8217;ll figure out how to post it as a zip or something instead of a tarball.  But until that point, you can grab the <a href="http://github.com/glenc/sp.py/tarball/master">tarball here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.glenc.net/2008/05/18/sppy-sharepoint-scripting-with-python/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SharePoint?  Meet Python</title>
		<link>http://blog.glenc.net/2007/08/12/sharepoint-meet-python/</link>
		<comments>http://blog.glenc.net/2007/08/12/sharepoint-meet-python/#comments</comments>
		<pubDate>Mon, 13 Aug 2007 03:57:12 +0000</pubDate>
		<dc:creator>glenc</dc:creator>
				<category><![CDATA[2007 Office]]></category>
		<category><![CDATA[Articles]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[SharePoint]]></category>

		<guid isPermaLink="false">http://blogtmp.glenc.net/2007/08/12/sharepoint-meet-python/</guid>
		<description><![CDATA[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 [...]]]></description>
			<content:encoded><![CDATA[<p>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 <a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython" target="_blank">Iron Python </a>is your answer.  In this short article I&#8217;ll show you how to get up and running with Iron Python and SharePoint.  I&#8217;ll also show you a few scripts that will give you some insight into the potential power of this extremely useful combination.<span id="more-66"></span></p>
<p>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&#8217;t go into the details of what Iron Python is specifically, you can read about it on <a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython" target="_blank">the site</a>.  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&#8217;s easy to load up an object, poke around its properties, and inspect exactly what each object does.</p>
<p>Here&#8217;s a quick example of a Python script enumerating all sites in a site collection and printing out the URL and root web name:</p>
<pre name="code" class="py">webapp = SPWebApplication.Lookup(Uri("http://mysite"))
for site in webapp.Sites:
  print '%-30s - %s' % (site.Url, site.RootWeb.Title)
</pre>
<p>Ever need to quickly get the internal name of a particular field?</p>
<pre name="code" class="py">list = SPSite("http://mysite").RootWeb.Lists["Documents"]
for field in list.Fields:
  print field.InternalName
</pre>
<p>Okay &#8211; 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.</p>
<h2>Getting Started</h2>
<p>First, download Iron Python and install it on your SharePoint server.  Next, fire up a console and type &#8220;<strong>ipy</strong>&#8220;.  This will launch the Iron Python interactive console.</p>
<p>Next, type the following lines:</p>
<pre name="code" class="py">>>> import clr
>>> clr.AddReference("Microsoft.SharePoint")
>>> from Microsoft.SharePoint import *
</pre>
<p>Let&#8217;s look at what we&#8217;ve done.  In Python the &#8220;import&#8221; statement is like the &#8220;using&#8221; statement in C#.  It tells the interpreter that we want to use a particular module.  In this case, we&#8217;re importing the interface to the .NET Common Language Runtime.</p>
<p>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&#8217;ll need to import those as well.</p>
<p>At this point, you&#8217;re ready to go.  Try out these lines to explore the object model.</p>
<pre name="code" class="python">>>> 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
</pre>
<h2>Unit Testing and Debugging</h2>
<p>The interactive console can be extremely useful for unit testing your code as well as debugging.  Since I&#8217;ve started using Iron Python I&#8217;ve often kept a console up while working on whatever application I&#8217;m building.  It&#8217;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.</p>
<p>Iron Python can load any .NET assembly.  So if you want to test out a method you&#8217;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.</p>
<h2>Cooking with Gas</h2>
<p>One thing I hate is tech sites that show elementary examples and don&#8217;t really get into the complexity that is sure to arise once you move past &#8220;Hello World&#8221;.  So I&#8217;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:</p>
<ul>
<li>running stsadm commands from python</li>
<li>importing parts of the .NET framework into your scripts</li>
<li>functions as objects in python</li>
</ul>
<p><a href="http://files.glenc.net/articles/python/backupsites.py">Download the script here.</a></p>
<h2>Conclusion</h2>
<p>I&#8217;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&#8217;d really like to see some of the following:</p>
<ul>
<li>Mass check-in/publish scripts</li>
<li>Scripts to upload a given web part to every page in a site</li>
<li>Data/content migration</li>
</ul>
<h2>Reference</h2>
<ul>
<li><a href="http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython" target="_blank">Iron Python website</a> &#8211; get the code and learn how to use it</li>
<li><a href="http://docs.python.org/tut/tut.html" target="_blank">Python Tutorial</a> &#8211; very valuable introduction to the Python language</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.glenc.net/2007/08/12/sharepoint-meet-python/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
