When you are learning to work with the SharePoint object model, it can be handy to use PowerShell as a discovery tool to see what’s available and how it works. Several posts on this blog have already dealt with the SharePoint object model. This post should be seen as an introduction and an how to.
First of all, you need to run these PowerShell scripts and commands on the SharePoint server because the object model cannot be used remotely. So go ahead and install PowerShell on your SharePoint development box if needed. Then start a PowerShell console and use the following commands:
- $sitepath=”<url to your site collection>”
- [System.Reflection.Assembly]::LoadWithPartialName(”Microsoft.SharePoint”)
- $site=new-object Microsoft.SharePoint.SPSite($sitepath)
- $web=$site.Rootweb
The above commands do the following:
- Store the URL to your site collection in the $sitepath variable.
- Load the Microsoft.SharePoint.dll assembly.
- Get the site collection in an SPSite object and store it in the $site variable.
- Get the root web (a SharePoint site like a team site) in the $web variable.
If you want you can put those four commands in a .ps1 file (PowerShell script) and then dot source it when needed. For example: . getsite.ps1.
Now you can use PowerShell’s features to do some discovery about the $site and $web variables. Try this at the command prompt:
$site | more
You will get the properties of the site collection like its Id, owner, url, zone, content database, etc… Now try the following:
$site | gm | more
With the above command, you get the site object’s members. This includes the site object’s methods like GetRecycleBinItems. But how do you know how to call that method? Just type the following:
$site.GetRecycleBinItems
If you type a method without () you will get more info including how to call the method. It this case, you will see that the method requires a parameter of type SPRecycleBinQuery.
The $web variable is more interesting because it allows you to work with a SharePoint site like a team site. Just type $web | more to see the properties of the site and $web | gm | more to find out more about methods.
A SharePoint site usually contains lists and you can get to those lists with the Lists property. Now, if you just type $web.lists you will get a lot of info. Use something like this to get exactly what you need:
$web.lists | ft -property Title
This formats the ouput of the lists property in a table (ft or format-table) and only gets the Title property of each list.
An interesting object is the SPFile object. You can use it to “grab” a file in your site such as default.aspx. For example:
$homepage=$web.GetFile(”default.aspx”)
$homepage
When you type the above two commands you will get the properties of default.aspx. A useful property is CustomizedPageStatus. It allows you to see if the page has been customized and is in an unghosted state. Just type $homepage.customizedpagestatus to see the property.
Note that SPFile has useful methods like CopyTo, MoveTo, Delete and so on… For example, just try $homepage.copyto(”other.aspx”). This will create a copy of the page. Just see if it worked by going to http://<url>/other.aspx.
I have used SPFile before, for example to upload files. Just check this previous post for more info. But uploading can be as simple as $web.files.add(”filename”, (get-content <file> -encoding byte)). By the way, to see files use the Files property as in $web.FILES | ft url. Note that you only get the files in the current folder (root of the website). You can get to files in other folders using SPFolder objects. For example: $web.Getfolder(”foldername”).Files | ft URL. To see all the files, you will need to use something recursive.
This post only touched on the basics. If I have some time I will post some other scripts that allow you to make bulk changes to sites and pages.