Creating a Graphical User Interface with PowerShell for SharePoint

In this blog post I will be covering 2 very important topics both related to developing cool PowerShell scripts for your SharePoint 2013 farm environments​. The first topic that will be covered by this post is the ability to create Graphical User Interfaces with Powershell. You need to understand that PowerShell is built on top of .NET, and that because of this, a Powershell script can import and use any .NET entity, including those contained in the System.Windows.Forms namespace. This then gives us the ability to create graphical user interfaces as if we were simply creating a new Windows Form Application.

The second topic I want to be covering, is the ability to use PowerShell to interact with SharePoint 2013 using the Client Side Object Model (CSOM). Just like for the graphical user interface, we can import assemblies from the SharePoint .NET CSOM and use them to interact with remote SharePoint 2013 environments. This is actually very Powerful, but requires you to understand how the SharePoint Client Side Object Model works. It does require some good development knowledge in order to use it. My book “Beginning PowerShell for SharePoint 2013” makes a brief reference to this, but doesn’t cover it in any real details. This method is especially useful when wanting to interact with Office 365 using PowerShell.

Alright, now that I’ve set the table, let’s dive into the content of this post. Assume that you want to create a new visual interface, that let’s users input the URL of a remote SharePoint 2013 farm, and that retrieves the name of all the lists contained at that web level. Using PowerShell, you could easily build a script that will automatically generate a simple  GUI, and make calls to the SharePoint CSOM. Figure 1 below shows the end result of my script. Users are asked to input the remote SharePoint environment’s URL in the text box at the top, and when they click on the Connect button, the PowerShell script will automatically connect and retrieve all the lists using the Client Side Object Model.

 

blogPowersharePointConsole.jpg

Figure 1 – Results of the PSGUI Powershell script

Below is the PowerShell code used to create this powerful tool. It can be run from any machine that has PowerShell v3 and higher, but requires that the user running it, as read access to the destination site specified in the URL box. Enjoy!

 

​​​[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client”)
[System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint.Client.Runtime”)

$form = New-Object system.windows.forms.Form
$form.Height = 360
$form.Width = 500
$form.Text = “Power SharePoint Console”

$btnConnect = New-Object System.Windows.Forms.Button
$btnConnect.Height = 25
$btnConnect.Width = 75
$btnConnect.Left = 35
$btnConnect.Top = 10
$btnConnect.Text = “Connect”

$txtUrl = New-Object System.Windows.Forms.TextBox
$txtUrl.Width = 200
$txtUrl.Top = 10
$txtUrl.Left = 120

$txtResults = New-Object System.Windows.Forms.TextBox
$txtResults.Multiline = $true
$txtResults.Width = 450
$txtResults.Height = 250
$txtResults.Left = 16
$txtResults.Top = 50
$txtResults.ScrollBars = [System.Windows.Forms.ScrollBars]::Vertical

# SharePoint Specific
Function GetInfo($url)
{
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$web = $ctx.Web
$ctx.Load($web)
$ctx.ExecuteQuery()
$lists = $web.Lists
$ctx.Load($lists)​
$ctx.ExecuteQuery()

$stringLists = “Lists in the specifiec web: `r`n”
for($i = 0; $i -lt $lists.Count; $i++)
{
$curList = $lists[$i]
$ctx.Load($curList)
$ctx.ExecuteQuery()
$stringLists += $curList.Title + “`r`n”
}
$txtResults.Text = $stringLists
[System.Windows.Forms.MessageBox]::Show(“Lists Acquired Successfully!”)
}

$btnConnect.Add_Click({GetInfo($txtUrl.Text)})
$form.Controls.Add($btnConnect)
$form.Controls.Add($txtUrl)
$form.Controls.Add($txtResults)
$form.ShowDialog()

Microsoft Premier Field Engineer – SharePoint

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*