Home >Tips >PowerShell with SharePoint from Scratch >Create PowerShell function for all app types kalmstrom.com site map icon Site map  

Create PowerShell Function for All App Types

A PowerShell with SharePoint tutorial

PowerShell iconIn earlier articles, we have been working with a function that creates generic lists. With just a few changes, this function can be used for creating all kinds of apps.

In the demo below, Peter Kalmström first explains how to change the list function to a function for SharePoint document libraries.

After that, Peter writes a general function that creates any SharePoint apps: Create-MyApp. For that, he adds an AppType parameter with the variable type Microsoft.SharePoint.Client.ListTemplateType to the function.

function Create-MyApp([string] $ListName, [string] $ListURL, [Microsoft.SharePoint.Client.ListTemplateType] $AppType){
    if($ListURL -eq ""){
        $ListURL =$ListName.Replace(" ","").ToLower()
    }

Finally, Peter changes the list and library functions so that they call the general app function when you run them. That way he keeps dedicated functions for lists and libraries, but any changes in the function parameters only need to be made in the Create-MyApp function.

function Create-MyList([string] $ListName, [string] $ListURL){
    Create-MyApp -ListName $ListName -ListURL $ListURL -AppType GenericList
}

function Create-MyLibrary([string] $ListName, [string] $ListURL){
    Create-MyApp -ListName $ListName -ListURL $ListURL -AppType DocumentLibrary
}

When Peter runs these scripts he uses the array and the forEach loop that he created in earlier demos, but you can of course also try them with a simple function call. In any case, the app type must be specified in the call, as we are no longer doing it in the function.

foreach($Dep in $Departments){ PowerShell with SharePoint from Scratch cover
    Create-MyApp -ListName $Dep -AppType Tasks
}


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