Follow Us:
One habit that prevails through any vertical is to be efficient at what you do – work smarter not harder right? In the land of PowerShell (or any development for that matter) that is a vital lesson to learn! It’s happened to many of us. Something comes up and you need to turn to code or PowerShell to accomplish said task and you need to take care of it quickly. Some of it you know how to do, some of it you may not. Ideally, unless you’re a master you wouldn’t write the whole script from scratch. So what do you do? You go searching to see if there are other examples out there you can pull from, right? There’s absolutely nothing wrong with using what others have done as long as they have published for reuse, and you don’t republish it claiming it as your own.
Let’s take that a step further. Let’s say you’re an IT Pro and PowerShell fanatic but have some developers on hand. Recently they wrote a web service that automates some site creation tasks in SharePoint Online like creating the site, setting some permissions, updating some list settings, etc. Your first inclination might be, "Oh I can do the same thing in PowerShell." Not so fast! That’s the whole point of a web service, to be called from other things! Whether you’re doing some customization in the SharePoint interface, integration with CRM Online, whatever the case, that web service was meant to be reused. There's no need to struggle with writing a ton of complex PowerShell CSOM to because another developer has already provided it! Below is one example of how you can put that existing web service to WORK with little effort on your part.
Let me start with a caveat – this is not the ONLY way to do this, and the syntax will differ depending on a few things like if whether you’re web service is SOAP or REST, the platform, etc. But in C5 Insight's case, we have a web service hosted in Azure that has methods to create SharePoint Online sites based on some input parameters. Originally this is coming from Dynamics CRM Online which calls the Azure web service. But I needed to run this adhoc to backfill some sites. Once the web service is called, it creates the site and performs actions such as setting permissions on multiple libraries and updating other settings. We use PowerShell to call the web service and pass the necessary parameters: things like the URL, Title, the user who has rights to create the site, etc. The main “secret sauce” to do this is New-WebServiceProxy. In this case, I’m doing a little more than a single one time web service call, so I’ll walk you through my script.
First I am setting some variables that don’t change (inlcuding the URL of the web service) and getting a secure password for the user who needs to create the sites:
$url = http://<webapp>.azurewebsites.net/CreateSite.asmx
$proxy = New-WebServiceProxy $url
$spAccount = "<username>"
$spPassword = Read-Host -Prompt "Enter password" –AsSecureString
$projectGuid = ""
$createOneNote = $false
Now that we have that, you can use a command like this to look at all the available methods:
$proxy | gm -memberType Method
GM is shorthand from get-method where the type is Method and not a Property. If you have at least PowerShell 3.0, you can do IntelliSense with this as well:
I’m bulk-creating sites, and I’m reading them in from a CSV:
$csv = Import-Csv "C:\powershell\input.csv"
$create = $csv | where {$_.Create -eq "yes"}
Write-host "About to create" @($create).count "site(s)" -ForegroundColor Green
I have a column in the CSV called "Create," and in the column is “Yes” or “No”. That way, I can do testing with just 1 line enabled, or mark them complete as I go if I am batching them. So first, I read in the CSV and put that in a variable $csv. Then, I filter that list to only include rows whose Create = Yes which I store in a new variable called $create. I also output the number of sites I am going to create by dumping $create with a count. To do that easily, write it as @($create).count. Then, I output that to the screen. Now the fun part…
foreach ($row in $create) {
#set variables from CSV $accountURL = $row.accountURL $accountAcronym = $row.accountAcronym $title = $row.title $prjCleansedName = $row.prjCleansedName $projectClass = $row.projectClass Write-Host "Creating new site for" $accountAcronym -ForegroundColor Yellow #Call webservice to create site [string]$response = $proxy.CreateSite($spAccount,$spPassword,$accountURL,$accountAcronym, $title,$prjCleansedName,$projectGuid,$projectClass,$createOneNote) if ($response -like "https*") {write-host "Site created successfully at" $response -ForegroundColor Green} elseif ($response -like "Error:*") {write-host $response -ForegroundColor Red} }
#set variables from CSV
$accountURL = $row.accountURL
$accountAcronym = $row.accountAcronym
$title = $row.title
$prjCleansedName = $row.prjCleansedName
$projectClass = $row.projectClass
Write-Host "Creating new site for" $accountAcronym -ForegroundColor Yellow
#Call webservice to create site
[string]$response = $proxy.CreateSite($spAccount,$spPassword,$accountURL,$accountAcronym,
$title,$prjCleansedName,$projectGuid,$projectClass,$createOneNote)
if ($response -like "https*") {write-host "Site created successfully at" $response -ForegroundColor Green}
elseif ($response -like "Error:*") {write-host $response -ForegroundColor Red}
}
So now for every row in $create, we set the properties from the CSV of that row to variables, and pass that to the web service call. Obviously this syntax will be specific to your web service, parameters, etc. You can get all that information like I showed you above with the methods. I call the web service with in the needed parameters and dump that in a variable called $response. I know the web service will return either any error exception message, or if successful it will return the URL of the site created. So I prefix the call with [string] to ensure I store that as text.
Then finally, I wanted to make this is easier to read so I just some simple if / elseif statements to look at the text that came back from the web service and style the output. If it was an error, I say Error: <error returned text> in red, or if it was good I just say site was successful with the URL in green. You, could wrap all this in a function if you wanted, but I didn’t for this instance.
If you need help with either writing web services, PowerShell, or anything you’ve seen on our site please Contact Us!
The complementary paper includes over 12 years of research, recent survey results, and CRM turnaround success stories.
Request Download
This 60-second assessment is designed to evaluate your organization's collaboration readiness.
Learn how you rank compared to organizations typically in years 1 to 5 of implementation - and which areas to focus on to improve.
This is a sandbox solution which can be activated per site collection to allow you to easily collect feedback from users into a custom Feedback list.
Whether you are upgrading to SharePoint Online, 2010, 2013 or the latest 2016, this checklist contains everything you need to know for a successful transition.