Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 7025

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 2162

Deprecated: Hook imagify_allow_picture_tags_for_webp is deprecated since version 2.2! Use imagify_allow_picture_tags_for_nextgen instead. in /www/collab365_296/public/wp-includes/functions.php on line 5758
Creating a Graphical User Interface with PowerShell for SharePoint - Collab365
Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Warning: Undefined array key "url" in /www/collab365_296/public/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/smart-site/classes/class-tvd-smart-shortcodes.php on line 85

2013-12-23

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 “” 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.

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()

 

(Visited 343 times, 1 visits today)

About the author 

Nik Charlebois

Summit Bundle

Get 200+ hours of Microsoft 365 Training for 27$!

Master Office 365, Power Platform & SharePoint & Teams With 200+ Hours Of Training Videos and 108 Ebooks in the Collab365 Academy. This offer is insane and is only available for a limited period.