Powershell for sharepoint developers
Download
1 / 32

- PowerPoint PPT Presentation


  • 293 Views
  • Updated On :

PowerShell For SharePoint Developers. Neil Iversen Inetium http://justaddcode.com. The Plan. PowerShell Introduction Basic Syntax Not-So-Basic Syntax SharePoint Using the SharePoint Object Model Debugging Code using PowerShell Showing Off SharePoint Specific? (Providers,…) Questions.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about '' - michon


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Powershell for sharepoint developers l.jpg

PowerShell For SharePoint Developers

Neil Iversen

Inetium

http://justaddcode.com


The plan l.jpg
The Plan

  • PowerShell

    • Introduction

    • Basic Syntax

    • Not-So-Basic Syntax

  • SharePoint

    • Using the SharePoint Object Model

    • Debugging Code using PowerShell

    • Showing Off

    • SharePoint Specific? (Providers,…)

  • Questions


Why powershell for developers l.jpg
Why PowerShell for Developers?

  • Better visibility into the Object Model

    • Find all the instances of a content type in a Site

  • Automated or repetitive tasks

    • Add a web part to a page on 500 sites

  • Faster Development Cycle


Developing faster l.jpg
Developing Faster

  • Traditional SharePoint Development

    • Time wasted during Testing

  • PowerShell / .NET Hybrid Development

    • ‘Risky’ development done in PoSH

    • Code converted to .NET (C#/VB)

    • Shorter Deploy/Test Cycle


What is powershell l.jpg
What is PowerShell?

  • Different things to different people

  • It’s a

    • Shell

    • Cool .bat file

    • VBScript replacement

    • Admin’s Best Friend

    • Developer’s Best Friend



Slide7 l.jpg

+



Slide9 l.jpg

+



Slide11 l.jpg

=



Getting around l.jpg
Getting Around

  • ls

  • cd

  • rm

  • mkdir

  • man

  • dir

  • cd

  • del

  • Mkdir

  • help


Core components l.jpg
Core Components

  • Alias

    • cd = set-location

    • Dir = get-childitem

  • Cmdlet

    • Workhorse of PowerShell

  • Function

    • Block of script

  • Provider

    • Adds alternate ‘directory structure’

    • Think ‘/proc’ only niftier

    • Get-PSdrive

  • Snapin

    • Group of PowerShell functionality



Conditions and flow control l.jpg
Conditions and Flow Control

  • Variables

    • $foo = “bar” #implicitly typed as System.String

    • $ary = 4,2,5,2 #typed as object[]

    • [xml]$xdoc = “<a><b>b1</b><b>b2</b></a>”

  • Some Operators

    • -eq

    • -lt / -gt

    • -le / -ge

    • -like / -notlike

    • -match / -imatch

  • Control

    • If

    • switch

  • help about_comparison_operators


The pipeline l.jpg
The Pipeline

  • Everything is a System.Object

    • Unless its something more useful

  • dir | where-object {$_.Length –gt 5} | select-object –last 2 | sort-object -prop Extension –desc

  • $anArray | sort


Most useful commands l.jpg
Most Useful Commands

  • Foreach-object

    • dir | foreach-object { $_.Name }

    • Alias: dir | % { $_.Name }

  • Where-object

    • dir | where-object {$_.Length –gt 10}

    • Alias: dir | ? {$_.Length –gt 10}

  • Select-object

    • dir | select-object –first 5

    • Alias: none

  • Honorable Mentions: Sort-Object, Group-Object



Linqish powershell syntax l.jpg
LINQish PowerShell Syntax

LINQ

PowerShell

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

varlowNums =

from n in numbers

where n < 5

select n;

Console.WriteLine("Numbers < 5:");

foreach (var x in lowNums) {

Console.WriteLine(x);

}

$numbers = 5, 4, 1, 3, 9, 8, 6, 7, 2, 0

Write-host “Numbers < 5:”

$numbers | ?{$_ -lt 5}


Isn t this for sharepoint developers l.jpg
Isn’t this for SharePoint Developers?

  • SharePoint has a rich .NET Object Model

  • PowerShell interacts natively with .NET

  • SharePoint’s internal values are hidden

    • Lots of functionality not accessible via the UI

    • Validation of custom code

  • Debugging is Difficult

    • Especially on a remote machine

    • Code/Compile/Deploy/Test/Repeat Cycle


Loading the object model l.jpg
Loading the Object Model

  • [System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)

  • $site = new-object Microsoft.SharePoint.SPSite(http://thesite/site)

  • $web = $site.OpenWeb()

  • $web.Title


Some useful commands l.jpg
Some Useful Commands

  • Display all the Lists

    • $web.Lists

  • Display Properties Sorted by Name

    • $item.Properties | sort –prop Name

  • Set an item’s properties

    • $item.Properties[“SomeKey”] = “value”

    • $item.Properties.Update()



Exploring sharepoint development l.jpg
Exploring SharePoint Development

  • Not Everything is Intuitive

    • Object Model Gives Hints

  • Compile->Deploy->Test Loop is Long

    • Makes failed attempts expensive

  • Using PowerShell Lowers (some) Pain

    • No Compile/Deploy Loop

    • Get Immediate Feedback



Extending types l.jpg
Extending Types

  • Command Line version of Mashups

  • Add Properties and Methods to ANY type

  • Simplifies potentially tedious commands

  • On The Fly Magic

    • Add-Member

  • The (SemiPermanent) Magic

    • Ps1xml

    • Update-type –pre/postpend my.ps1xml



Taking it further l.jpg
Taking it Further

  • PowerShell Community Extensions (PSX)

    • http://www.codeplex.com/PowerShellCX

  • SharePoint Provider (v2 only)

    • http://www.codeplex.com/PSSharePoint

  • PowerTab

    • http://thepowershellguy.com/blogs/posh/pages/powertab.aspx


Your feedback is important l.jpg
Your Feedback is Important

Please fill out a session evaluation form and either put them in the basket near the exit or drop them off at the conference registration desk.

Thank you!



Thanks l.jpg
Thanks!

Neil Iversen

Inetium

http://justaddcode.com


ad