SharePoint has a very rich object model. Each object has an extensive set of properties (data) and methods (actions) which gives you a lot of flexibility. A farm has multiple web applications, each web application has multiple site collections, each site collection has multiple sites and each site has multiple lists. There are many other primitive objects to support this as well: timer jobs, users, services, list items, etc.
This object model is the “what” of PowerShell for SharePoint. Let’s talk about the “how.”
PowerShell, being a scripting language, is very good with collections and arrays. A list is an object itself, but it’s also container for a collection of list items, that we can use individually or in groups.
Given this, if we can write scripts that act on groups of items – we can, but we’ll get to how in some upcoming posts – a little planning will allow us to do complex things easily, reliably and repeatedly. Here’s the pattern:
Build a set of objects, of the type you’re going to operate on later.
Filter the set by choosing common traits between objects.
Execute your operation on the entire set.
We’re going to follow this model a lot throughout this series. It’s robust and versatile, yet extremely simple to implement.
To recap our superhero – the task that we’re going to try to get through – let’s take a look at a screen shot. First we’ve got our document library with pictures in it:
The goal is to move the ones with spaces in their file names. You can see how that’s a reasonably small set here, but if there were many more pictures, or maybe we were trying to move the ones last modified between two dates or by a specific group of people, it would be more difficult by hand.
I’m going to give away the entire content of the next 10 tips right here and now. It’s something you’ve heard before. You can scroll just a few paragraphs up and read it again. Here’s what we’re going to do:
Build a set of objects. We want SPListItem objects, representing items in our document library. They’re called SPDocument derives from SPListItem, which means it has all the same properties as a list item, plus more that are specific to a document. To get to those, we’ll have to look at SPSite and SPWeb objects – where is our document library? – and SPList objects. We’ll pick the one we want.
Filter the set. Our filter is simple. We’re taking everything with a space in the file name. It just so happens that the file name is our title here, so we’re looking for objects with spaces in the titles.
Execute our operation. This sounds straightforward: move it from where it is to where we want it to go. We’ll discuss some of the complexities along the way, like how the move command just isn’t there and how we need to construct URLs to tell SharePoint what to work with.
That’s it, in a nutshell. Of course, the devil is in the details, so I’ll save the description for next post.