SharePoint comes with many web services that allow you to do many things from working with users to working with SharePoint sites and much more. In the past, if an administrator wanted to work with these web services from a scripting language, it was not that easy. Sure, you could create something with Visual Studio but that is not always that simple.
PowerShell, again, makes it easier to call a web service and discover what you can do with it. The steps are fully explained here by Keith Hill. The example uses a weather forecast web service. If you installed the SharePoint Community Extensions (for vs80vars.ps1) and Visual Studio .NET 2005 (for wsdl.exe and csc.exe), you are good to go.
To apply this to SharePoint, I will use an example that enumerates the users on a SharePoint site. First of all, you need to know how to get to the wsdl and that is explained in the WSS v3.0 SDK. It turns out that the Users and Groups Web Service is the one I need and that the address is http://<Site>/_vti_bin/usergroup.asmx. The commands:
wsdl http://moss.newtech.local/_vti_bin/usergroup.asmx?wsdl
csc /t:library usergroup.cs
[Reflection.Assembly]::LoadFrom(”usergroup.dll”)
$users=new-object usergroup
$users.Credentials=[System.Net.CredentialCache]::DefaultCredentials
$users.GetAllUserCollectionFromWeb()
The result of the last command is XML and you know what PowerShell will return: the root element (in this case Users). If you type “$users.GetAllUserCollectionFromWeb().users” you will get the collection of users:
User
—-
{Administrator, Geert Baeke, NEWTECH\domain users, NT AUTHORITY\local servic…
So to see the users and their properties type “$users.GetAllUserCollectionFromWeb().users.user”. You can then use formatting, selecting, sorting and grouping as usual. For example, to see only site admins and only some properties in a table:
$users.GetAllUserCollectionFromWeb().users.user | ?{$_.IsSiteAdmin -eq $true} | ft name, email
Of course, you are not restricted to just viewing things. What if you want to create a new site? Well, just use the WSS v3.0 SDK and check the Administration service. That one exposes a method called CreateSite that you can call like this (typed in multiple lines, PowerShell provides the >>):
$admin.CreateSite(”http://moss.newtech.local/sites/powershell”,
>> “PowerShell”,”A site about PowerShell”, 1033,”STS#0″,
>> “newtech\administrator”,”administrator”,”administrator@newtech.local”,
>> “”,”")
>>

2 Trackbacks/Pingbacks
Calling a Webservice from PowerShell…
…
[…] wrote about using the SharePoint web services from PowerShell before. In order to consume the web service, you needed wsdl.exe to generate a proxy class and csc.exe to […]
Post a Comment