Home >Tips >PowerShell with SharePoint from Scratch >Enhance SharePoint URL with If Statement kalmstrom.com site map icon Site map  

Type Variables and Create If Statement

A PowerShell with SharePoint tutorial

PowerShell icon In the demo below, Peter Kalmström shows how to enhance the app generation function he created in an earlier Tips article, so that the URL for each app always is written without spaces and always has lower case letters.

With this enhancement, an app with the name Hello World will have helloworld in the URL.

Create-MyList "Hello World"

URL

Peter first sets the data type of the variables ListName and ListURL to string.

Then he creates an If statement that checks if the ListURL variable has a value or not. If the value is empty, we will set the ListURL to the same value as the ListName.

As Peter has set the ListURL and ListName values to be of the string data type, he can continue the If statement with string functions to remove any spaces in the list name and make the URL use lower case.

function Create-MyList([string] $ListName, [string] $ListURL){
    #Always versioning on
    if($ListURL -eq ""){
        $ListURL =$ListName.Replace(" ","").ToLower()
    }
    New-PnPList -Title $ListName -Template GenericList -EnableVersioning -OnQuickLaunch -Url $ListURL
    Write-Host ("List " + $ListName + " has been created!") -ForegroundColor Green    
}

In the next demo, Peter will  build on the earlier App Management
articles in this PowerShell series PowerShell with SharePoint from Scratch cover to create a general function that cannot only create generic lists but also document libraries or any other kind of SharePoint apps.


You will find more detailed explanations in the book.


back icon next icon
Products Buy FAQ Services Tips Books Contact About Us Tools

Security and integrity

Copyright  Kalmstrom Enterprises AB  All rights reserved