Category: SharePoint

  • How to connect to SharePoint Online with SharePointOnlinePowerShell

    Besides the possibility to manage your SharePoint Online in the SharePoint admin center, you have the option to do it with PowerShell. For this tasks Microsoft has published the PowerShell module SharePointOnlinePowerShell. It’s focus is more administrative tasks, than process automation. You can e.g. change the SharePoints Tenants policies, but you cannot create list items with this PowerShell module. The module is constantly developed by Microsoft it bears 223 cmdlets (16.08.2021).

    Scope of SharePointOnlinePowerShell

    I recommend checking the scope with following cmdlet:

    Get-Command -Module Microsoft.Online.SharePoint.PowerShell | out-gridview -passthru

    Prerequisites

    • If you want to connect to SharePoint Online with SharePointOnlinePowerShell, you need a user with the SharePoint Administrator role.
    • You need Windows PowerShell 2.0 or higher to run the module

    Installation of SharePointOnlinePowerShell

    The name for the installation is different then in the documentation. The technical name is Microsoft.Online.SharePoint.PowerShell. You can install the module Microsoft.Online.SharePoint.PowerShell with following PowerShell cmdlet:

    Install-Module -Name Microsoft.Online.SharePoint.PowerShell

    If you have not trusted PSGallery yet, you will be prompted if you trust this repository. You can confirm it with “y”.

    Screenshot of the installation of SharePointOnlinePowerShell

    If you encounter this issue, start the PowerShell Session as an administrator:

    PS C:\Users\Serkar> Install-Module -Name Microsoft.Online.SharePoint.PowerShell
    Install-Module : Administrator rights are required to install modules in 'C:\Program Files\WindowsPowerShell\Modules'.
    Log on to the computer with an account that has Administrator rights, and then try again, or install
    'C:\Users\Serkar\Documents\WindowsPowerShell\Modules' by adding "-Scope CurrentUser" to your command. You can also try
    running the Windows PowerShell session with elevated rights (Run as Administrator).
    At line:1 char:1
    + Install-Module -Name Microsoft.Online.SharePoint.PowerShell
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Install-Module], ArgumentException
        + FullyQualifiedErrorId : InstallModuleNeedsCurrentUserScopeParameterForNonAdminUser,Install-Module

    You can start PowerShell as an administrator like this:

    Start-Process powershell -Verb runAs

    You can also download it manually from PSGallery: PowerShell Gallery | Microsoft.Online.SharePoint.PowerShell 16.0.21513.12000

    Connect to SharePoint Online with SharePointOnlinePowerShell

    You can connect to SharePoint Online with an credential object or interactively. In this post I am describing both scenarios. Currently there is now way to do it with an Azure enterprise application, so you need a user. If you want to automate processes, I highly recommend a service user for this, since you don’t want your automation to crash, when you leave the company.

    Connect to SharePoint Online with SharePointOnlinePowerShell with Credential

    Note: You can only connect with an user with the SharePoint administrator role. It also won’t function, if you have multi factor authentication (MFA) enabled. For MFA check out the interactive instruction of this post.

    In the first step create an credential object:

    $Credential = Get-Credential
    credential object for connecting to SharePoint Online

    After doing this, replace the adminurl and connect to SharePoint Online:

    Connect-SPOService -Url "ADMINURL" -Credential $Credential

    In my case it looks like this:

    Connect-SPOService -Url "https://devmodernworkplace-admin.sharepoint.com/" -Credential $Credential

    If you got no error, you have established the connection successfully.

    Connect to SharePoint Online with SharePointOnlinePowerShell interactively

    You can connect interactively with following cmdlet:

    Connect-SPOService -Url "ADMINNURL"

    In my case it is:

    Connect-SPOService -Url "https://devmodernworkplace-admin.sharepoint.com/"

    You will see, that a login prompt will pop up:

    Login prompt, when logging in interactively

    Disconnect from SharePoint Online

    Disconnecting from SharePoint Online can be done like this. If you close your PowerShell Sesssion, you don’t have to do it. The connection is dropping automatically.

    Disconnect-SPOService

    Conclusio

    As you can see, there are at least two PowerShell modules – PNP.PowerShell and SharePointOnlinePowerShell to manage and automate your SharePoint tenant. With this module you can approach your Sharepoint Tenant more on as an administrator, than as somebody, who wants to automate business processes. I would not miss this module out of sight, when automating processes, because some cmdlets might be missing in the PNP PowerShell module or they might not work as expected.

    Further Reading

    If you want to focus more on automating processes, than on administering SharePoint, you should definitely check out how to connect with PNP.Powershell: Connect to SharePoint with PowerShell | SharePoint Online (workplace-automation.com/)

    If you want to see the original docs of Microsoft check out this article: Erste Schritte mit der SharePoint Online-Verwaltungsshell. | Microsoft Docs

  • How I easily add Webparts to SharePoint Pages by PowerShell

    How I easily add Webparts to SharePoint Pages by PowerShell

    A page consists of multiple webparts. With the webparts you can refer to other lists, libraries and apps within a page. You can add webparts SharePoint Pages manually and programmatically to a page. Adding webparts to single sites, can be done straight forwards in the edit section of the pages. If you want to do it in multiple sites and want to ensure, that the pages do look identical in terms of the structure, add webparts to SharePoint Pages with PowerShell! In this post I will show you how to do add webparts to pages manually and with PowerShell.

    Add Webparts to SharePoint Pages manually

    If you want to add webparts to SharePoint pages manually, you have to edit the page by clicking on edit.

    webpart page edit

    Now you can add the webpart to areas, where you get displayed a red line with a plus:

    Like here

    Add webpart to homepage

    or here:

    Add webpart to homepage

    After clicing on the red cross, you have to choose the webpart and can add. In my example, I want to display a user.

    Add people webpart
    Add the webpart name and set a person.
    set webpart properties

    If you want to display the changes to all users, click on publish (1) , otherwise click save as draft (2), so only you can see the changes.

    save or publish the page

    I have published the page, so every user can now see my change:

    published webpart page

    Add Webparts to SharePoint pages with PowerShell

    Recommendations

    • Use a code editor, which can format JSON properly – otherwise your code will look like a mess. I would recommend Visual Studio Code
    • Refresh the $Page variable fore each change you make on your page – Otherwise you will experience, that the homepage will be messed up

    Building webpart Components

    In the first step, I would recommend to design the webpart in the worbench page. You can find the workbench page of your site, by calling following URL:

    https://DOMAIN.sharepoint.com/sites/SITE/_layouts/15/workbench.aspx

    For my demo page it is:

    https://devmodernworkplace.sharepoint.com/sites/Sales/_layouts/15/workbench.aspx

    You can ignore the warning:

    warning, which you can ignore in workbench

    Now add the webpart, which you want manually.

    demo webpart in workbench

    Now click on “Web part data” and copy the yellow marked contents

    webpart code from web part data

    Put it in an editor like visual studio code and I would recommend changing to language to JSON.

    visual studio code changing language

    After doing that, make a right click and click on format document.

    Formatting document in visual studio code

    Now copy the content of WebPartData into a new tab (CTRL + N).

    Add { as the first character and } as the last character. After doing this, format the document.

    formatting document in visual studio code

    Your JSON should look like this:

    {
        "webPartData": {
            "id": "7f718435-ee4d-431c-bdbf-9c4ff326f46e",
            "instanceId": "ad75d0d7-81be-4271-b809-405a30d161d2",
            "title": "People",
            "description": "Display selected people and their profiles",
            "audiences": [],
            "serverProcessedContent": {
                "htmlStrings": {},
                "searchablePlainTexts": {
                    "title": "Added by PowerShell",
                    "persons[0].name": "Serkar Aydin",
                    "persons[0].email": "Serkar@devmodernworkplace.onmicrosoft.com"
                },
                "imageSources": {},
                "links": {}
            },
            "dataVersion": "1.3",
            "properties": {
                "layout": 1,
                "persons": [
                    {
                        "id": "Serkar@devmodernworkplace.onmicrosoft.com",
                        "upn": "",
                        "role": "",
                        "department": "",
                        "phone": "",
                        "sip": ""
                    }
                ]
            }
        }
    }

    Adding Webpart to Page with PowerShell

    In order to add webparts to SharePoint pages with PowerShell, we have to connect to SharePoint. If you are doing this the first time, check out the post: Connect to SharePoint Online with PowerShell (workplace-automation.com/)

    $Credential = Get-Credential
    $SiteUrl = "https://devmodernworkplace.sharepoint.com/sites/sales"
    Connect-PnPOnline -Url $SiteUrl -Credential $Credential

    Get the page, where you want to add the webparts to:

    $Page = Get-PnPPage -Identity "Home"

    After connecting, you can add the webpart by the previously created JSON content:

    $PersonJSON = @"
    {
        "webPartData": {
            "id": "7f718435-ee4d-431c-bdbf-9c4ff326f46e",
            "instanceId": "ad75d0d7-81be-4271-b809-405a30d161d2",
            "title": "People",
            "description": "Display selected people and their profiles",
            "audiences": [],
            "serverProcessedContent": {
                "htmlStrings": {},
                "searchablePlainTexts": {
                    "title": "Added by PowerShell",
                    "persons[0].name": "Serkar Aydin",
                    "persons[0].email": "Serkar@devmodernworkplace.onmicrosoft.com"
                },
                "imageSources": {},
                "links": {}
            },
            "dataVersion": "1.3",
            "properties": {
                "layout": 1,
                "persons": [
                    {
                        "id": "Serkar@devmodernworkplace.onmicrosoft.com",
                        "upn": "",
                        "role": "",
                        "department": "",
                        "phone": "",
                        "sip": ""
                    }
                ]
            }
        }
    }
    "@
    
    Add-PnPPageWebPart -Page $Page -DefaultWebPartType People -WebPartProperties $PersonJSON -Section 1 -Column 1 -Order 3

    The result of the added webpart looks like this:

    added webpart with powershell

    Bonus: Ready-to-use script

    The ready to use script looks like this:

    $Credential = Get-Credential
    $SiteUrl = "https://devmodernworkplace.sharepoint.com/sites/sales"
    Connect-PnPOnline -Url $SiteUrl -Credential $Credential
    
    
    $Page = Get-PnPPage -Identity "Home"
    
    $PersonJSON = @"
    {
        "webPartData": {
            "id": "7f718435-ee4d-431c-bdbf-9c4ff326f46e",
            "instanceId": "ad75d0d7-81be-4271-b809-405a30d161d2",
            "title": "People",
            "description": "Display selected people and their profiles",
            "audiences": [],
            "serverProcessedContent": {
                "htmlStrings": {},
                "searchablePlainTexts": {
                    "title": "Added by PowerShell",
                    "persons[0].name": "Serkar Aydin",
                    "persons[0].email": "Serkar@devmodernworkplace.onmicrosoft.com"
                },
                "imageSources": {},
                "links": {}
            },
            "dataVersion": "1.3",
            "properties": {
                "layout": 1,
                "persons": [
                    {
                        "id": "Serkar@devmodernworkplace.onmicrosoft.com",
                        "upn": "",
                        "role": "",
                        "department": "",
                        "phone": "",
                        "sip": ""
                    }
                ]
            }
        }
    }
    "@
    
    Add-PnPPageWebPart -Page $Page -DefaultWebPartType People -WebPartProperties $PersonJSON -Section 1 -Column 1 -Order 3

    Further Documentation

    If you are curious about webpart development and the workbench page check: Build your first SharePoint client-side web part (Hello World part 1) | Microsoft Docs

    Alternative to Visual Studio Code, you can use following link: JSON Formatter & Validator (curiousconcept.com)

  • Access SharePoint via Graph API in PowerShell

    Access SharePoint via Graph API in PowerShell

    Sometimes the use of PNP.PowerShell might not be sufficient. I encountered this experience, when I wanted to find out the usage of all sites. The Graph API provides methods, which you can use in your PowerShell Scripts. So in my example I wanted to get unused Sites with PowerShell. If you want to make use of it, you have to register an enterprise application and afterwards you can retrieve the information with an HTTP-Webrequest. In the following I will show you step by step how to access your SharePoint tenant with Graph API in PowerShell.


    Considerations – Find the right Graph API Method

    The Graph API has multiple methods, which we can use to analyze and change the content of our M365 services. In order to find the right method for your plan, check folllowing resources to see what the Graph API is capable of Microsoft Graph REST API v1.0 reference – Microsoft Graph v1.0 | Microsoft Docs. Based on the needed methods, you have to set up your enterprise application.

    Let’s assume, that you want to see the site usage of all sites in your tenant. In order to do this, you have to make use of following API method:

    GET /reports/getSharePointSiteUsageDetail(period='{period_value}’)
    GET /reports/getSharePointSiteUsageDetail(date={date_value})

    This API requires following permissions. We will consider them in this article. I want to analyze the sharepoint usage and want to update it to a list afterwards, that’s why I will make use of Application – Reports.Read.All

    Permission typePermissions (from least to most privileged)
    Delegated (work or school account)Reports.Read.All
    Delegated (personal Microsoft account)Not supported.
    ApplicationReports.Read.All
    SharePointSiteUsage Method Screenshot Graph API
    reportRoot: getSharePointSiteUsageDetail – Microsoft Graph v1.0 | Microsoft Docs

    Register the Enterprise Application

    After we figured out what permissions we need, we register the app.

    Prerequisites

    You have to have the role ‘Global Administrator’ to grant the permissions for an Enterprise Application.

    Registration

    Visit the Azure Portal URL and switdh to the app registrations sites. Directlink: App registrations – Microsoft Azure

    Click on new registration

    Register New App for Graph Api

    Give your application a name, click on Accounts in this organizational directory only, select mobile as platform, after that click on register.

    Application Registration for Graph Api

    Take a note of the Application (client) ID, you will need it to authenticate against the Graph API.

    Enterprise Application

    Grant API Permissions for App Registration

    After creating the app, we have to give it the permissions, which we have defined in the first step.

    Enterprise Application API permission

    Click on Microsoft Graph.

    Graph API screenshot

    Grant it Application permissions

    Application Permissions

    Now you have to select the permissions, for which you want to use the Graph API. I just need the information for Reports.Read.All. If you don’t know which permission to take, check the considerations part of this post.

    reports.read.all permission for Graph API

    As you can see, the permission is not granted for this tenant.

    not granted permissions screenshot for Graph API

    Create Client Secret for App Registration

    In order to authenticate to the Graph API in PowerShell, you have to create a client secret.

    Click on Certificates & secrets and then on New client secret

    Create client secret for Graph API enterprise application

    Set a Description and define when it will be expiring. I would recommend to give it a description, which you can recognize, for what it will be used in future. I have set 24 months, because I want to make use it in an automation, which should run for a long term. When finished, click Add.

    Usage Scripts client secret for Graph API

    Take Note of the value! You wont see it again, if you leave the site.

    Client Secret for Graph API obfuscated

    Consent the Requested permissions for App Registration

    Caution: You have to consent the created application with the global administrator role.

    https://login.microsoftonline.com/TENANTDomain/adminconsent?client_id=CLIENTID
    

    The URL for my dev tenant is like:

    https://login.microsoftonline.com/devmodernworkplace.onmicrosoft.com/adminconsent?client_id=949710fd-8d80-48ee-8c1b-a6f5e9e32be3

    Choose an account with global administrator role.

    Global administrator account login to grant permission for Graph API

    As you can see the permissions, which we have configured, are showing up:

    permission grant for created app for Graph API

    Since you have not set a redirect url, you will encounter this issue, which you can ignore.

    this ocurs, since we have not configured a redirect url

    Check Permission consent

    You can check that the permission is granted, if you see the green check marks.

    granted permission for enterprise application for Graph API

    Script To Acess SharePoint via the Graph API (PowerShell)

    The script contains two parts. The first part is about authentication and the second is about getting the data provided.

    Authentication

    I am making use of a credential export to be sure, that nobody steals the credentials, when it is in plain text. If you don’t know how to, check out: Use credentials in PowerShell – SPO Scripts

    Function Export-CredentialFile 
    {
        param(
        [Parameter(Mandatory=$true,Position=0)]
        $Username,
        [Parameter(Mandatory=$true,Position=1)] 
        $Path
        )
        
        
        While ($Path -eq "")
        {
            $Path = Read-Host "The path does not exist. Where should the credentials be exported to?"
        }
        $ParentPath = Split-Path $Path
        If ((Test-Path $ParentPath) -eq $false)
        {
            New-Item -ItemType Directory -Path $ParentPath
        }
        $Credential = Get-Credential($Username)
        $Credential | Export-Clixml -Path $Path
        Return $Credential
    }
    Function Import-CredentialFile ($Path)
    {
        if (! (Test-Path $Path))
        {
            Write-Host "Could not find the credential object at $Path. Please export your credentials first"
            Export-CredentialFile
        }
        Import-Clixml -Path $Path
    }
    $AppId = '949710fd-8d80-48ee-8c1b-a6f5e9e32be3'
    $CredentialPath = "C:\temp\$AppId.key"
    Export-CredentialFile -Username $AppId -Path $CredentialPath

    After doing this, we notice, that the file with the app id as name, has an encrypted password. So we splitted credentials from script to increase the security. This credential file can only be used on the machine and with the user, who has created it.

    PowerShell credential object

    If we run follwing script afterwards, we will notice, that the $AuthorizationRequest will show us a token with an bearer token.

    $AppId = '949710fd-8d80-48ee-8c1b-a6f5e9e32be3'
    $CredentialPath = "C:\temp\$AppId.key"
    $AppCredential = Import-CredentialFile -Path $CredentialPath
    
    $Scope = "https://graph.microsoft.com/.default"
    $Url = "https://login.microsoftonline.com/devmodernworkplace.onmicrosoft.com/oauth2/v2.0/token"
    
    $Body = @{
        client_id = $AppCredential.UserName
        client_secret = $AppCredential.GetNetworkCredential().password
        scope = $Scope
        grant_type = 'client_credentials'
    }
    
    $AuthorizationRequest = Invoke-RestMethod -Uri $Url -Method 'post' -Body $Body
    $AuthorizationRequest
    
    answer to the authorization request

    Access SharePoint Online with Authorization Token

    Now that we got the access token, we can connect to SharePoint Online with following script. You can use the uris (methods), defined in Microsoft docs.

    $Uri = "YOURURI"
    
    $Header = @{Authorization = "$($AuthorizationRequest.token_type) $($AuthorizationRequest.access_token)"}
    $SitesRequest = Invoke-RestMethod -Uri $Uri -Method 'Get'  -Headers $Header

    Get Site Usage Details

    You can get the site usage with following uri “https://graph.microsoft.com/beta/reports/getSharePointSiteUsageDetail(period='{D90}’)?`$format=application/json”. The number next to the D means the amount of days. So for my example it shows the usage of all sites for the last 90 days. You can replace D90 with D7, D30, and D180.

    With this script you can get the site usage for the last 90 days:

    $Uri = "https://graph.microsoft.com/beta/reports/getSharePointSiteUsageDetail(period='{D90}')?`$format=application/json"
    
    $Header = @{Authorization = "$($AuthorizationRequest.token_type) $($AuthorizationRequest.access_token)"}
    $SitesRequest = Invoke-RestMethod -Uri $Uri -Method 'get'  -Headers $Header 
    
    $Sites.value | Out-GridView -PassThru

    Bonus: Ready-to-Use Script

    If you want to make use of the script, you have to change the parameters $GraphUrl and $AppID.

    Param(
        $AppId = '949710fd-8d80-48ee-8c1b-a6f5e9e32be3',
        $GraphUrl = "https://login.microsoftonline.com/devmodernworkplace.onmicrosoft.com/oauth2/v2.0/token",
        $Scope = "https://graph.microsoft.com/.default",
        $Uri = "https://graph.microsoft.com/beta/reports/getOffice365GroupsActivityDetail`(`period=`'`D90`'`)?`$format=application/json",
    )
    
    Function Export-CredentialFile 
    {
        param(
        [Parameter(Mandatory=$true,Position=0)]
        $Username,
        [Parameter(Mandatory=$true,Position=1)] 
        $Path
        )
        
        
        While ($Path -eq "")
        {
            $Path = Read-Host "The path does not exist. Where should the credentials be exported to?"
        }
        $ParentPath = Split-Path $Path
        If ((Test-Path $ParentPath) -eq $false)
        {
            New-Item -ItemType Directory -Path $ParentPath
        }
        $Credential = Get-Credential($Username)
        $Credential | Export-Clixml -Path $Path
        Return $Credential
    }
    Function Import-CredentialFile ($Path)
    {
        if (! (Test-Path $Path))
        {
            Write-Host "Could not find the credential object at $Path. Please export your credentials first"
            Export-CredentialFile
        }
        Import-Clixml -Path $Path
    }
    
    $CredentialPath = "C:\temp\$AppId.key"
    Export-CredentialFile -Username $AppId -Path $CredentialPath
    
    $AppCredential = Import-CredentialFile -Path $CredentialPath
    
    $Body = @{
        client_id = $AppCredential.UserName
        client_secret = $AppCredential.GetNetworkCredential().password
        scope = $Scope
        grant_type = 'client_credentials'
    }
    
    $AuthorizationRequest = Invoke-RestMethod -Uri $GraphUrl -Method 'post' -Body $Body
    
    $Header = @{Authorization = "$($AuthorizationRequest.token_type) $($AuthorizationRequest.access_token)"}
    $SitesRequest = Invoke-RestMethod -Uri $Uri -Method 'get'  -Headers $Header 
    
    $SitesRequest.value | Out-GridView -PassThru

    Conclusio

    In this article you saw how to find the right permission for the enterprise application, which you need to access the SharePoint via the Graph API with PowerShell. After doing this, you can authenticate and analyze the data.

    Further Docs

    reportRoot: getSharePointSiteUsageDetail – Microsoft Graph v1.0 | Microsoft Docs

  • Add-PNPField: Add SharePoint columns with PowerShell

    Add-PNPField: Add SharePoint columns with PowerShell

    Sometimes it’s hard enough to map the exact business requirements on the intranet. Implementing them manually afterwards can lead to the requirements not being implemented exactly in the further course. Also doing it manually is very time consuming and frustrating, when mistakes are made. This is where automation comes in handy. If you are looking forward to automate your intranet, it is crucial to design lists by adding them programtically. When you add SharePoint columns with PowerShell, you can be sure, that mistakes are not made like choosing the wrong column type or wrong internal name. You basically ensure that your intranet follows the standards, which have been defined in the beginning by your stakeholders. Thus I thought it might be interesting to share my experiences with you. All my descriptions have been tested and can be used on lists and libraries.

    Prerequisites

    You have to have access to the sites, where you want to add the SharePoint columns with PowerShell. Check out the article of Microsoft, if you are not sure, how to customize the permissions: Customize permissions for a SharePoint list or library – SharePoint (microsoft.com).

    To have a understanding, how lists / libraries can be designed, will help you to automate the procedures. If you are not familiar with this, try out creating and defining a list manually, to know what options are available.

    Scenario

    Lets assume, we are looking forward, to create a list with various column types.

    List scenario

    The beginning will be this plank list:

    Plank list

    Description

    In the following, I will show you step by step how to add the SharePoint columns with PowerShell.


    Step 1 – Connect to SharePoint

    In order to add SharePoint Columns with PowerShell, you have to connect to SharePoint Online with PNP. PowerShell. If you are not sure about how to, check out: Connect to SharePoint Online with PowerShell (workplace-automation.com/)

    $Credential = Get-Credential
    Connect-PnPOnline -Url "https://devmodernworkplace.sharepoint.com/sites/sales" -Credential $Credential

    Step 2 – Get the List

    Get the SharePoint list or library with Get-PNPList

    $List = Get-PnPList -Identity "ID of the list"
    $List = Get-PnPList -Identity "978f0ca5-7cc9-4151-8ba0-2fc45d736723"

    You can find the internal name of the list by running Get-PNPList.

    Internal Name of Opportunities2

    Step 3 – Add SharePoint Columns

    After we have connected to the site and got the list/ library, we can add the desired fields. If you don’t want to add the fields to the default view, remove the paramter -AddToDefaultView.

    I have used $DisplayName also for the paremter InternalName, since we cannot ensure, that the name for the internal name will be set, as you can see in the screenshot:

    Screenshot of differing internal name

    Add String Column

    $DisplayName = "Delivery address (Customer)"
    
    Add-PnPField -List $List -DisplayName $DisplayName -InternalName $DisplayName -Type Text -AddToDefaultView

    The result of the string column looks like this:

    added string column

    Add Date Colum with Time

    If you want to add the date column, without time, you can do it like this:

    $DisplayName = "Date"
    Add-PnPField -List $List -DisplayName $DisplayName -InternalName $DisplayName -Type DateTime -AddToDefaultView

    The result of the date column with time looks like this:

    Result of Date column with time

    Add Date Column without Time

    If you want to add the date column without time, you can do it like this:

    $DisplayName = "Date without time"
    $PNPField = Add-PnPField -List $List -DisplayName $DisplayName -InternalName $DisplayName -Type DateTime -AddToDefaultView
    [XML]$SchemaXml = $PNPField.SchemaXml
    $SchemaXml.Field.SetAttribute("Format","DateOnly")
    
    Set-PnPField -List $List -Identity $PNPField.Id -Values @{SchemaXml =$SchemaXml.OuterXml} -UpdateExistingLists

    The result of the date column looks like this:

    Result of Date Column without TIme

    Add Lookup Column

    If you want to add lookup columns, you have to know the ID of the list and the Internal Name of the column, which you want to look up.

    Let’s assume, that we will lookup the email address from the list Contacts.

    Lookup column of contacts

    The ID of the list contact is: a66ff40f-ceca-4f6b-a523-7fe32a97ea11

    ID of contacts list

    For the email column, you other can do it with GUI: Determine internal name of SharePoint Columns with GUI – SPO Scripts or with PowerShell:

    Get-PnPField -List $List
    
    #Or
    
    Get-PNPField -List "INTERNALNAME OFLIST"
    Lookup Field of Email

    Now you can add the column email address to the opportunity list.

    $DisplayName = "Email"
    $PNPField = Add-PnPField -List $List -DisplayName $DisplayName -InternalName $DisplayName -Type Lookup  -AddToDefaultView
    Set-PnPField -List $List -Identity $PNPField.Id -Values @{LookupList= "a66ff40f-ceca-4f6b-a523-7fe32a97ea11"; LookupField="Email"}

    The result of the lookup column looks like this:

    Result of lookup field

    Add Choice Column

    First you define the choices, than you add the column.

    $Choices = (
    "Choice 1",
    "Choice 2",
    "Choice 3"
    )
    
    $DisplayName = "Choice"
    $PNPField = Add-PnPField -List $List -DisplayName $DisplayName -InternalName $DisplayName -Type Choice -AddToDefaultView -Choices $Choices

    Result:

    Result of choice column

    Add Boolean Column

    $DisplayName = "Win"
    $PNPField = Add-PnPField -List $List -DisplayName $DisplayName -InternalName $DisplayName -Type Boolean -AddToDefaultView

    The result of the boolean column looks like this:

    Result of boolean value

    Add Number Value

    $DisplayName = "Number of Stakeholder"
    $PNPField = Add-PnPField -List $List -DisplayName $DisplayName -InternalName $DisplayName -Type Number -AddToDefaultView

    The result of the number column looks like this:

    Result of number column

    Add Currency Column

    $DisplayName = "Deal Size"
    $PNPField = Add-PnPField -List $List -DisplayName $DisplayName -InternalName $DisplayName -Type Currency -AddToDefaultView

    The result of the currency column looks like this:

    Result of currency column

    Add Notes Column

    $DisplayName = "Notes"
    $PNPField = Add-PnPField -List $List -DisplayName $DisplayName -InternalName $DisplayName -Type Note -AddToDefaultView

    Result:

    Result of note columns

    Add Location Column

    $DisplayName = "Location"
    $PNPField = Add-PnPField -List $List -DisplayName $DisplayName -InternalName $DisplayName -Type Location -AddToDefaultView

    The result of the location column looks like this:

    Result of location column

    Add Person / User Column (single person)

    $DisplayName = "Sales Manager"
    $PNPField = Add-PnPField -List $List -DisplayName $DisplayName -InternalName $DisplayName -Type User -AddToDefaultView

    The result of the single people column looks like this:

    Result of Person Column

    Add Person / User Column (multiple persons)

    $DisplayName = "MultiPerson"
    $PNPField = Add-PnPField -List $List -InternalName $DisplayName -DisplayName $DisplayName -Type User -AddToDefaultView 
    [XML]$SchemaXml = $PNPField.SchemaXml
    
    $SchemaXml.Field.SetAttribute("Mult","TRUE")
    $OuterXML = $SchemaXml.OuterXml.Replace('Field Type="User"','Field Type="UserMulti"')
    Set-PnPField -List $List -Identity $PNPField.Id -Values @{SchemaXml =$OuterXML} -UpdateExistingLists
    

    The result of the multi people column looks like this:

    multi user column

    Add Hyperlink Column

    $DisplayName = "Url to Invoice"
    $PNPField = Add-PnPField -List $List -DisplayName $DisplayName -InternalName $DisplayName -Type  -AddToDefaultView

    The result of the hyperlink column looks like this:

    Result of URL Column

    Add Managed Metadata Column

    Retrieve the Unique Identifier of the term set, which you want to add as a metadata column.

    Visit admin center url and follow the numbers in the screenshot:

    https://tenant-admin.sharepoint.com

    TaxonomyItemID

    Now add it to TaxononmyItemID and run the cmdlets

    $DisplayName = "Departments"
    Add-PnPTaxonomyField -List $List -DisplayName $DisplayName -InternalName $DisplayName -TaxonomyItemId f076462d-bde7-4fa4-aa7b-4409a769fcd1

    The result is, that the department column, pops up as a managed metadata column.

    result of managed metadata column

    Add Calculated Column

    You can add a calculated column by defining a formular per each column. Check the reference, which shows you, how to define a formular: Examples of common formulas in lists – SharePoint (microsoft.com)

    Note: When you define the formular, you have to take the displayname. Internal names do not work currently.

    Otherwise, you see this error:

    Error when using internal name of column
    $DisplayName = "Revenue per stakeholder"
    $PNPField = Add-PnPField -List $List -DisplayName $DisplayName -InternalName $DisplayName -Type Calculated -AddToDefaultView -Formula ="[Deal Size]/[Number of Stakeholder]"
    

    The result of the calculated column looks like this:

    Result of calculated column

    Add Image Column

    $DisplayName = "Logo of customer"
    $PNPField = Add-PnPField -List $List -DisplayName $DisplayName -InternalName $DisplayName -Type Thumbnail -AddToDefaultView

    The result of the image column, looks like this:

    Result of image column

    Add Task Outcome Column

    The task outcome column is designed to define the outcome of tasks like approvals. So basically it is a choice column, but you cannot allow fill-in choices and it is a single selection choice.

    I will define following choices:

    approved
    rejected
    to be reviewed by supervisor

    The default is empty. I want the people to make conscious decisions.

    [array]$Choices = (
    "approved",
    "rejected",
    "to be reviewed by supervisor"
    )
    $DefaultChoice = ""
    
    $Choices = $Choices |ConvertTo-Xml -NoTypeInformation
    $Choices = $Choices.Objects.OuterXml
    $Choices =  $Choices -replace "Objects", "CHOICES"
    $Choices =  $Choices -replace "Object", "CHOICE"
    
    
    
    $FieldXML = @"
    <Field RowOrdinal="0" ColName="nvarchar15" Name="$DisplayName" StaticName="$DisplayName" SourceID="{$($List.Id)}" ID="{$([guid]::NewGuid())}" Indexed="FALSE" Viewable="TRUE" EnforceUniqueValues="FALSE" Required="FALSE" DisplayName="$DisplayName" Type="OutcomeChoice">
        <Default>$DefaultChoice</Default>
        $($Choices)
    </Field>
    "@
    $PNPField = Add-PnPFieldFromXml -List $List -FieldXml $FieldXML 

    If you want to make the field visible in the default view, you have to run following cmdlets:

    $PNPView = Get-PnPView -List $List | Where-Object {$_.DefaultView -eq $true}
    $PNPView.ViewFields.Add($PNPField.InternalName)
    $PNPView.Update()

    The result of our task outcome column looks like this:

    result of task outcome column

    Bonus: Complete Script

    Like everytime, I have provided you the whole script to add a column:

    $Credential = Get-Credential
    Connect-PnPOnline -Url "https://devmodernworkplace.sharepoint.com/sites/sales" -Credential $Credential
    $List = Get-PnPList -Identity 978f0ca5-7cc9-4151-8ba0-2fc45d736723
    
    
    $DisplayName = "Departments"
    $PNPField = Add-PnPField -List $List -DisplayName $DisplayName -InternalName $DisplayName -Type  -AddToDefaultView

    Conclusio

    As you can see, adding SharePoint Columns with PowerShell can save you a lot time, especially if you have to do it on multiple lists/ libraries and sites. I hope to save you a ton of work with this cheat sheet.

    References

    GitHub – pnp/powershell: PnP PowerShell

    FieldCollection.AddFieldAsXml-Methode (Microsoft.SharePoint.Client) | Microsoft Docs

    Image by StartupStockPhotos from Pixabay

  • How to use Word and Excel templates in SharePoint

    How to use Word and Excel templates in SharePoint

    If you are using SharePoint and want to ensure, that your end users make use of your templates in SharePoint, you can make use of content types, to use word and excel templates in SharePoint.


    Prerequisites

    If you want to create use templates in your across multiple SharePoint sites, you need access to the SharePoint Admin center otherwise you just can create it for single sites.

    You can use templates for word and excel files, which don’t have macros. If you have macros in your templates, you cannot use them as template for your content type.


    What are content types?

    You can imagine a content type as an object with meta data, which describes an object.

    Example: invoice.

    Meta data for an invoice are e.g. invoice date and due date.

    Invoice

    I will show you how to configure content types in your content type hub, so you can make use of word and excel templates in SharePoint!


    Create a template

    Before we start to use templates in SharePoint, we have to define a template. For our invoice, I took a template of Microsoft.

    Invoice template
    Invoice template

    Create content type in SharePoint Admin Center

    To create a content type visit the admin center

    https://yourdomain-admin.sharepoint.com

    for my domain it is

    https://devmodernworkplace-admin.sharepoint.com/

    Click on Content services -> Content type gallery

    SharePoint admin center

    Click on create a content type

    Create content type

    Give it a name, optionally a description define a category and set document content types as parent category and document as content type.

    I chose to locate my content type invoice in the new category “DevModernWorkplace” category. You can also use the exisiting category document content types category. I would recommend a new category, so you see all your custom content types at one place.

    Conent type formular

    After your content type is created, I would recommend to add site columns. These site columns will show up in the library, where you will use this content type.

    Create new site column

    Add site columns to content type

    I have added the meta data due date from the existing site columns and added invoice date as new site columns and added the new site column to the new dedicated category “Invoice Columns”.

    Add existing site columns
    Add existing column
    Create new site column
    Create new site column

    Add template to content type

    Now you can add your previously created template to your content type. Click on Settings and then on Advanced settings.

    Advanced settings

    Click on upload a new document template and browse to your template. After selecting it, save it.

    Upload document template

    Publish content type

    After adding the template, publish your content type.

    Publish content type
    Publish content types

    Republish content type

    If you already published your content type, click on Publish.

    Click on Republish

    Switch to the site, where you have added the content type and visit the site settings. I am switching back to the sales site.

    Click on Content type publishing.

    Check Refresh all published content types on next update and click on OK.

    Add content type to your list

    I have added a list invoices to the demo site and as you can see the default content types are configured.

    invoice library

    In order to add the invoice template, click on the gear -> Library settings.

    Library settings navigation

    Click on Advanced settings

    Library settings

    Set Allow management of content types? to Yes, scroll down to “Ok” to save your settings.

    Add your content type by clicking on Add from existing site content types

    If you don’t see your content type, ensure that you have published the content type and if you have published it, wait 5-10 minutes.

    Add content type to library

    After confirming with OK, you can see the invoice content type


    Bonus: Adding Metadata to your template

    If you want to add metadata to your Word/ Excel template, create a document with your configured content type.

    Add invoice content type

    Click on editing -> Open in the Desktop App

    Mark the area, where you want to add the metadata.

    Click on tab Insert -> Quick Parts -> Document Property -> “Your Metadata”

    I chose Invoice date

    I have added invoice date and due date

    Save the file locally.

    Now republish the template in your content type.

    there we are!

    If you change the metadata in SharePoint, the document gets updated automatically.

    Before:

    Previous metadata in invoice

    After:

    After metadata in invoice

    Date fields in the Library and in Word app are different

    Check out, the regional setting of your site. You might have different time zones configured for your client and your site.

    Conclusio

    Ensuring that Word and Excel templates are used in SharePoint online can be achieved by content types, published in the content type hub.

  • Remove SharePoint Sites fastly

    Remove SharePoint Sites fastly

    Howdy guys, sometimes we create ton of sites just for testing. A clean tenant is a must for an efficient management. Have you ever removed sites by hand? It took me at least 18 seconds to remove ONE site restless by hand. Come on, there MUST be a way to do it faster. In this article I want to show you how you can remove SharePoint sites fastly by using Out-GridView.

    Step 1 – Connect to SharePoint Online with PowerShell

    In order to remove SharePoint Sites fastly, we connect to our admin Site with PNP PowerShell. Basically we make use of cmdlets handled in this article:

    Connect to SharePoint Online with PowerShell (workplace-automation.com/)

    Connect-PnPOnline https://devmodernworkplace-admin.sharepoint.com/ -Interactive

    Step 2 – Remove all sites, which you don’t need

    After connecting, I basically make use of Out-GridView -PassThru, to pass my selected sites for deletion. If you are not familiar with Out-GridView check following article. It will help definitelly:

    Get Items / Files interactively – SPO Scripts

    Get-PnPTenantSite -IncludeOneDriveSites -Detailed  | Out-GridView -PassThru | ForEach-Object { 
    
        if ($_.Template -eq "GROUP#0") 
        {
            try
            {
                Remove-PnPMicrosoft365Group -Identity $_.GroupID 
                Write-Host "Removed M365 Group $($_.GroupID)" -ForegroundColor Green
            }
            catch
            {
                Write-Error "Could not remove M365 Group $($_.GroupID) $($Error[0].ErrorDetails.Message)"
            }
            
        }
        else
        {
            try
            {
                Remove-PnPTenantSite -Url $_.Url  -Force -ErrorAction Stop
                Write-Host "Removed Site $($_.URL)" -ForegroundColor Green
            }
            catch
            {
                Write-Error "Could not remove Site $($Error[0].ErrorDetails.Message)"
            } 
        }
    }
    

    So what will happen now? A popup will show up, where you can select the sites, which you don’t want to use. As you can see, I marked 3 sites. After clicking okay, it will be removed, but you still we see them in the recycle bin – so no need for panic ;).

    Choice of sites, which have to be deleted

    If you want to remove the sites  without residue, you have to make use of following cmdlets. Sites, which belong to the a M365 Group will be put in recycle bin anyways.

    Get-PnPTenantSite -IncludeOneDriveSites -Detailed  | Out-GridView -PassThru | ForEach-Object { 
    
        if ($_.Template -eq "GROUP#0") 
        {
            try
            {
                Remove-PnPMicrosoft365Group -Identity $_.GroupID 
                Write-Host "Removed M365 Group $($_.GroupID)" -ForegroundColor Green
            }
            catch
            {
                Write-Error "Could not remove M365 Group $($_.GroupID) $($Error[0].ErrorDetails.Message)"
            }
            
        }
        else
        {
            try
            {
                Remove-PnPTenantSite -Url $_.Url  -Force -ErrorAction Stop -SkipRecycleBin
                Write-Host "Removed Site $($_.URL)" -ForegroundColor Green
            }
            catch
            {
                Write-Error "Could not remove Site $($Error[0].ErrorDetails.Message)"
            } 
        }
    }

    Thats it! The three sites I have marked, are deleted now. In my case even restless.

    Screenshots of removal

    Step 3 – Controll your Action

    Controlling this, can be done by following cmdlet:

    Get-PnPTenantSite -IncludeOneDriveSites  | Out-GridView -PassThru 

    You might see, that there are still sites, which are related to M365 groups. This sites wil be removed by a job afterwards, so no need to worry.

    Screenshot of controll action

    BONUS 1: Wrapped all in functions

    I wrapped all the stuff in functions, so you don’t have to do it and proceed to remove SharePoint Sites fastly.

    Function Invoke-SPOSiteRemoval
    {
        Param
        (
            $Site,
            [Switch]$SkipRecycleBin
        )
    
        if ($_.Template -eq "GROUP#0") 
        {
            try
            {
                Remove-PnPMicrosoft365Group -Identity $_.GroupID 
                Write-Host "Removed M365 Group $($_.GroupID)" -ForegroundColor Green
            }
            catch
            {
                Write-Error "Could not remove M365 Group $($_.GroupID) $($Error[0].ErrorDetails.Message)"
            }
            
        }
        else
        {
            try
            {
                if ($SkipRecycleBin)
                {
                    Remove-PnPTenantSite -Url $_.Url  -Force -ErrorAction Stop -SkipRecycleBin
                }
                else
                {
                    Remove-PnPTenantSite -Url $_.Url  -Force -ErrorAction Stop
                }
                
                Write-Host "Removed Site $($_.URL)" -ForegroundColor Green
            }
            catch
            {
                Write-Error "Could not remove Site $($Error[0].ErrorDetails.Message)"
            } 
        }
    
    }
    
    Get-PnPTenantSite -IncludeOneDriveSites -Detailed  | Out-GridView -PassThru | ForEach-Object { Invoke-SPOSiteRemoval -Site $_ -SkipRecycleBin }

    BONUS 2: Ready-To-Use Script

    $TenantAdminUrl = "https://devmodernworkplace-admin.sharepoint.com/"
    Connect-PnPOnline $TenantAdminUrl -Interactive
    
    Function Invoke-SPOSiteRemoval
    {
        Param
        (
            $Site,
            [Switch]$SkipRecycleBin
        )
    
        if ($_.Template -eq "GROUP#0") 
        {
            try
            {
                Remove-PnPMicrosoft365Group -Identity $_.GroupID 
                Write-Host "Removed M365 Group $($_.GroupID)" -ForegroundColor Green
            }
            catch
            {
                Write-Error "Could not remove M365 Group $($_.GroupID) $($Error[0].ErrorDetails.Message)"
            }
            
        }
        else
        {
            try
            {
                if ($SkipRecycleBin)
                {
                    Remove-PnPTenantSite -Url $_.Url  -Force -ErrorAction Stop -SkipRecycleBin
                }
                else
                {
                    Remove-PnPTenantSite -Url $_.Url  -Force -ErrorAction Stop
                }
                
                Write-Host "Removed Site $($_.URL)" -ForegroundColor Green
            }
            catch
            {
                Write-Error "Could not remove Site $($Error[0].ErrorDetails.Message)"
            } 
        }
    
    }
    
    Get-PnPTenantSite -IncludeOneDriveSites -Detailed  | Out-GridView -PassThru | ForEach-Object { Invoke-SPOSiteRemoval -Site $_ -SkipRecycleBin }
    
    #Controlling
    Get-PnPTenantSite -IncludeOneDriveSites -Detailed  | Out-GridView -PassThru

    Conclusio

    Using Out-GridView saves you a ton of time, when you want to remove sites in SharePoint fastly.

    Further links

    Microsoft Docs for the removal cmdlet Remove-PnPTenantSite (PnP.Powershell) | Microsoft Docs

    Microsoft Docs for Out-Gridview Out-GridView (Microsoft.PowerShell.Utility) – PowerShell | Microsoft Docs


  • Term label propagation in SharePoint Online

    Term label propagation in SharePoint Online

    Rome was not build on a day so isn‘t our taxonomy. Business requirements change frequently, so we have to adapt our systems to support these changes. This article will show you how to change term labels ins SharePoint Online and what to consider.

    You can change term labels in SharePoint Online easily. You just have to consider the term set propagation procedure. I‘ve made some interesting experience, which I would share with you.

    Problem

    Following situation occurs: the term label of a customer is changing from “Quality Assurance” to “Quality Management Systems”. I changed the term label in the term store, but noticed, that the items and file metadata did not change in the SharePoint List.

    Organization list with department metadata
    Screenshot of the term group Quality assurance in the term store before changing it

    I changed the term “Quality Assurance” to “Quality Management Systems”.

    Screenshot of the term group Quality assurance in the term store after changing it

    As you can see, the term is not updating in the list ‘Organization’

    Even when editing the item, the term label stays the old one. Neither my customer, nor did understand, why it did not change.

    Editing the value for department
    Organization list with department terms

    How terms are propagated to sites

    Everytime you using terms by adding a termset to a column, the terms are getting stored in the hidden taxonomy list of a site. You can find the list under following URL:

    https://yourtenant.SharePoint/lists/TaxonomyHiddenList/AllItems.aspx

    In my case it is

    https://devmodernworkplace.sharepoint.com/sites/Sales/Lists/TaxonomyHiddenList/AllItems.aspx

    TaxonomyHiddenList

    As you can see, the term is still “Quality Assurance”. The Taxonomy Update Scheduler timer job will update the taxonomy list and also the term within one hour.


    If you change one term label, the procedure is like this:

    1. You change the term in the term store
    2. The Taxonomy Update Scheduler timer job will update the taxonomy list and also the term within one hour.
    3. Your items and files are getting are up to date with the newest term label

    Workaround

    Caution: This workaround should only be used if the change have to be fulfilled urgently.

    If you have to change the term label urgently, you can remove the item from the TaxonomyHiddenList.

    After removing the item, in the taxonomy hidden list, you can see the change of the term label took effect in the list:

    Organization list after the update of the taxonomyhiddenlist

    Conclusio

    When you change term labels in SharePoint, you have to be patient. The timer job will do it’s work and if not, make use of the workaround.

    Further Documentation

    If you don’t know what terms are, consider reading the doc of Microsoft: Introduction to managed metadata – SharePoint in Microsoft 365 | Microsoft Docs

  • Dealing with existing SharePoint connections

    Dealing with existing SharePoint connections

    We all can Imagine the scenario. You create sites in sharepoint and now you want to edit multiple sites afterwards with PowerShell. In order to be safe, we have to check, wether an connection exists and if yes to disconnect the current connection to have a clean processing of the sites. In this article I want to show you how can achieve dealing with existing SharePoint connections. If you don’t know how to connect to SharePoint Online, check the article.


    Symptoms – How I tried it first

    Multiple paths as needles

    I tried to check the connection by a normal if query, but as you can see it throws everytime an error, so the script will be halted under normal circumstances. Changing the ErroActionPreference is something you could do for sure, but I would not recommend it, if you want to handle other upcomming potential errors of the API. So as you can see dealing with existing SharePoint connections in terms of checking, wether an connection exists, is not that easy.

    if ((Get-PnPConnection) )
    {
        Write-Host "Connection found"
    }
    Get-PnPConnection : The current connection holds no SharePoint context. Please use one of the Connect-PnPOnline commands which uses the -Url argument to connect.
    In Zeile:2 Zeichen:6
    + if ((Get-PnPConnection) )
    +      ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-PnPConnection], InvalidOperationException
        + FullyQualifiedErrorId : System.InvalidOperationException,PnP.PowerShell.Commands.Base.GetPnPConnection
    PS H:>> 
    if ((Get-PnPConnection) -ne $Null )
    {
        Write-Host "Connection found"
    }
    Get-PnPConnection : The current connection holds no SharePoint context. Please use one of the Connect-PnPOnline commands which uses the -Url argument to connect.
    In Zeile:2 Zeichen:6
    + if ((Get-PnPConnection) -ne $Null )
    +      ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-PnPConnection], InvalidOperationException
        + FullyQualifiedErrorId : System.InvalidOperationException,PnP.PowerShell.Commands.Base.GetPnPConnection
    PS H:>> 
    if ((Get-PnPConnection|out-null) -ne $Null )
    {
        Write-Host "Connection found"
    }
    Get-PnPConnection : The current connection holds no SharePoint context. Please use one of the Connect-PnPOnline commands which uses the -Url argument to connect.
    In Zeile:2 Zeichen:6
    + if ((Get-PnPConnection|out-null) -ne $Null )
    +      ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-PnPConnection], InvalidOperationException
        + FullyQualifiedErrorId : System.InvalidOperationException,PnP.PowerShell.Commands.Base.GetPnPConnection
    PS H:>> 
    if ((Get-PnPConnection -ErrorAction SilentlyContinue) -ne $Null )
    {
        Write-Host "Connection found"
    }
    Get-PnPConnection : The current connection holds no SharePoint context. Please use one of the Connect-PnPOnline commands which uses the -Url argument to connect.
    In Zeile:2 Zeichen:6
    + if ((Get-PnPConnection -ErrorAction SilentlyContinue) -ne $Null )
    +      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [Get-PnPConnection], InvalidOperationException
        + FullyQualifiedErrorId : System.InvalidOperationException,PnP.PowerShell.Commands.Base.GetPnPConnection

    Solutions

    sewing kit

    Try Catch Solution

    In order to handle this situation, you have to catchup the error.

    The snippet tries to check wether there is an connection and if there is one, it will proceed and disconnect it. After disconnecting it I have set the variable $connection to $null, so I can process it later on.

    try 
    {
        Get-PnPConnection -ErrorAction Stop
        Disconnect-PnPOnline
        $Connection = $null
    }
    catch 
    {
        $Connection = $null
    }

    BONUS 1: Invoke-PNPConnection with Credential object (No MFA enforcement)

    A function which handles the whole procedure of cutting of the connection and reconnecting, makes the handling easier. In this case I have added an additional check of the contents of lists, because sometimes you do connect, but experience that the webserver is not ready yet – basically you get an 403 FORBIDDEN message in PowerShell.

    NOTE: This will only work If your user has no MFA enforcement. If you have MFA enabled, I have another function for you.

    Function Invoke-PNPConnection ($Url, $Cred)
    {
        try 
        {
            Get-PnPConnection -ErrorAction Stop
            Disconnect-PnPOnline
            $Connection  = $null
        }
        catch
        {
            $Connection  = $null
        }
        $i = 1
        while ($null -eq $Connection -and $i -le 6 -and $null -eq $Lists)
        {
            Write-Verbose "Trying to connect to $Url for the $i time" 
            $Lists = $null
            Connect-PnPOnline -Url $Url -Credentials $Cred
            $Lists = Get-PnPList -ErrorAction SilentlyContinue
            $i++
            if ($i -ne 1 -and $null -eq $Lists)
            {
                Start-Sleep -Seconds 30
                Write-Verbose "Wait 30 Seconds"
            }
        }
        Write-Verbose "Connection to $Url established"
    }

    You can call the function like this

    $Cred = get-credential
    $Url = "https://contoso.sharepoint.com/sites/Sales"
    Invoke-PNPConnection -Url $Url -Cred $Cred

    Bonus 2: Invoke-PNPConnection interactively (MFA enforced)

    So if you use the scripts interactively (with MFA enforced users), you can make use of this function

    Function Invoke-PNPConnection ($Url)
    {
        try 
        {
            Get-PnPConnection -ErrorAction Stop
            Disconnect-PnPOnline
            $Connection  = $null
        }
        catch
        {
            $Connection  = $null
        }
        $i = 1
        while ($null -eq $Connection -and $i -le 6 -and $null -eq $Lists)
        {
            Write-Verbose"Trying to connect to $Url for the $i time" 
            $Lists = $null
            Connect-PnPOnline -Url $Url -Interactive
            $Lists = Get-PnPList -ErrorAction SilentlyContinue
            $i++
            if ($i -ne 1 -and $null -eq $Lists)
            {
                Start-Sleep -Seconds 30
                Write-Verbose "Wait 30 Seconds"
            }
        }
        Write-Verbose "Connection to $Url established"
    }

    Start the function like this


    $Url = "https://contoso.sharepoint.com/sites/Sales"
    Invoke-PNPConnection -Url $Url

    Conclusio

    There are ways to deal with the connections, you just have to think a bit OOTB 🙂

    patched teddy

    You might find intersting

    If you are not familiar with connecting to SharePoint, check out this Post: Connect to SharePoint Online with PowerShell

    Original article of PNP Connecting with PnP PowerShell | PnP PowerShell




    Images:
    Bild von Meine Reise geht hier leider zu Ende. Märchen beginnen mit auf Pixabay

    Bild von vargazs auf Pixabay

    Bild von Ina Hoekstra auf Pixabay

    Bild von saulhm auf Pixabay

  • Filtering for SharePoint items with CAML Queries

    Filtering for SharePoint items with CAML Queries

    Most of our times, we just need just a bunch of items, to export them or to change their values. This post should help you to show, how to handle filtering for SharePoint items. Besides filtering for SharePoint items with Where-Object, you can also make use of CAML (Collaborative Application Markup Language), which lets you get only the items, you need. It might increase the performance of your queries, when you are dealing with large amounts of data.

    Where are the items, which I am looking for?

    Preqrequistes

    If we want to achieve filtering for SharePoint items, with a CAML query, we have to fulfill following prerequisites:

    1. Permissions to access the list
    2. Installed Module PNP.Powershell. If you don’t know how to, check the post.
    3. Connection to the site via PNP.PowerShell. If you don’t know how to, check the post.

    Considerations

    1. You should take care of the case sensitivity of operands and column names
    2. You should take care of the <view> part. Sometimes it is needed, sometimes not – so I would rely on the examples.

    Query Schema

    A query is structured like this

     "<View><Query><Where><LOGICAL OPERATOR><FieldRef Name='INTERNAL NAME OF COLUMN'/><Value Type='VALUE TYPE'>VALUE</Value></LOGICAL OPERATOR></Where></Query></View>"

    You can find the internal name of columns in two ways:

    PowerShell or GUI.

    Explanation for PowerShell: Getting FieldValues of Items | SPO Scripts
    Explanation for GUI: Determine internal name of SharePoint Columns with GUI (workplace-automation.com/)

    Value types

    TypeMeaningExamples
    BooleanIt means true or false. You can find this in yes/no checkboxestrue, false
    true reflects 1
    false reflects 0
    ChoiceIt reflects the choices in your sharepoint listapple, banana
    CurrencyIt reflects the amount of an defined currency5$
    DateTimeIt reflects a timestamp23.06.2021 15:30
    GUIDGlobally Unique Identifier (GUID)6154ff96-8209-457b-86dd-ee7dcd80b584
    IntegerIt reflects a number as an integer 10
    LookupLinks to another list: for example an Orders list may have a lookup field that links to customers in a Customer list;Füller AG
    NoteReflects a multi line text field. Not sortable or groupableLorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio.

    Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.
    TextReflects a single line text field. Sortable and groupable. Corresponds to the nvarchar SQL data type and represented by the SPFieldText class.Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
    UserA Lookup field that references the UserInfo database table.Email TypeId LookupId LookupValue
    —– —— ——– ———–
    Serkar@devmodernworkplace.onmicrosoft.com {c956ab54-16bd-4c18-89d2-996f57282a6f} 11 Serkar Aydin
    Source: Field element (Field) | Microsoft Docs

    Logical Comparison Operators

    In this case X means your entry

    OperatorMeaning
    BeginsWithThe existing value begins with X
    ContainsThe existing value contains x
    DateRangesOverlapThe existing date overlaps the date range defined in x
    |---- 01.01.-07.01 ------|
    |---02.01-09.01 -----|
    EqThe existing value equals x
    GeqThe existing value is greater or equal x
    GtThe existing value is greater than x
    InX is one of the existing values
    Includeschecks, whether x is in the defined values
    NotIncluseschecks, whether x is not in the defined values
    IsNotNullChecks wheter the existing value is not null
    IsNullChecks wheter the existing value is null
    LeqThe existing value is lower equal x
    LtThe existing value is lower than x
    Source: Query schema in CAML | Microsoft Docs

    In order to filter by query paramter, you have to define a filter query, depending on your datatype (string, integer, boolean..) you have to choose a different query value type.

    Logical Joins

    OperatorMeaning
    AndBoth query operations have to be fulfilled
    OrOnly one query operation have to be fulfilled
    Source: Query schema in CAML | Microsoft Docs

    Query Examples

    My blog would not keep it’s promise, If you would not find examples, which give you a fast way to adapt the scripts, so here we go!

    In my example, I am using my demo opportunities list. I have marked the names of the columns, the value types, the operands and the actual values bold. Mostly I am using the logical operator “eq”, but I think if you got the basic concept of this, you can adapt it to your solution easily and if not, we will find a way together.

    Example for boolean

    If you want to find items with TRUE values, you have to enter 1. For FALSE values, you have to make use of 0.

    If boolean should be true:

    Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Eq><FieldRef Name='Win'/><Value Type='Boolean'>1</Value></Eq></Where></Query></View>"

    If boolean should be false:

    Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Eq><FieldRef Name='Win'/><Value Type='Boolean'>0</Value></Eq></Where></Query></View>" 

    Example for choice

    If you want to filter for values choice values, you have to make use of a query like this:

    Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Eq><FieldRef Name='Product'/><Value Type='Choice'>SAP</Value></Eq></Where></Query></View>"

    Example for currency

    You have to enter the value of the amount without the currency sign.

    Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Eq><FieldRef Name='DealSize'/><Value Type='Currency'>40000</Value></Eq></Where></Query></View>"

    Example for DateTime

    You have to format date times according to this format (ISO8601).

    yyyy-MM-ddTHH:mm:ssZ

    You can do this by appending -Format s, when creating the variable

    $CreationDate = Get-Date "16.06.2021 20:04" -Format s

    If DateTime should exactly match a specific date

    $CreationDate = Get-Date "16.06.2021 20:04" -Format s
    Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Eq><FieldRef Name='Created'/><Value Type='DateTime' IncludeTimeValue='FALSE'>$CreationDate</Value></Eq></Where></Query></View>"

    If DateTime should be after a specific date

    Example: I want to find all items, created after 15.06.2021.

    $CreationDate = Get-Date "15.06.2021" -Format s
    Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Gt><FieldRef Name='Created'/><Value Type='DateTime' IncludeTimeValue='FALSE'>$CreationDate</Value></Gt></Where></Query></View>"

    If DateTime should be before a specific date

    Example: I want to find all items, created before 15.06.2021.

    Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Lt><FieldRef Name='Created'/><Value Type='DateTime' IncludeTimeValue='FALSE'>$CreationDate</Value></Lt></Where></Query></View>"

    Example for GUID

    [GUID]$UniqueID= "b4ae9e9f-7103-459a-acb2-73573d035b36"
    Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Eq><FieldRef Name='UniqueId'/><Value Type='GUID'>$UniqueID</Value></Eq></Where></Query></View>"

    Example for integer

    In this case I want to find all opportunites with 2 stakeholders.

    Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Eq><FieldRef Name='Stakeholder'/><Value Type='Integer'>2</Value></Eq></Where></Query></View>"

    Example for lookup

    Get-PnPListItem -List "Opportunities" -Query "<Query><Where><Eq><FieldRef Name='Contact'/><Value Type='Lookup'>Sus Spicious</Value></Eq></Where></Query>"

    Example for Note aka multi line text

    Get-PnPListItem -List "Opportunities" -Query "<Query><Where><Eq><FieldRef Name='Notes'/><Value Type='Note'>He was really curious.</Value></Eq></Where></Query>"

    Example for text aka string

    In this Query, I am looking for items, where the title equals Opp 3.

    Get-PnPListItem -List "Opportunities" -Query "<Query><Where><Eq><FieldRef Name='Title'/><Value Type='Text'>Opp 3</Value></Eq></Where></Query>"

    Example for user

    In this query, I am looking for items, where the authors UPN is Serkar@devmodernworkplace.onmicrosoft.com.

    Get-PnPListItem -List "Opportunities" -Query "<Query><Where><Eq><FieldRef Name='Author' /><Value Type='User'>Serkar@devmodernworkplace.onmicrosoft.com</Value></Eq></Where></Query>"

    Example for OR

    In this query, I am looking for items, where the value for Stakeholder is 1 or the value Win is yes.

    Get-PnPListItem -List $ListName -Query "<View><Query><Where><Or><Eq><FieldRef Name='Stakeholder'/><Value Type='Integer'>1</Value></Eq><Eq><FieldRef Name='Win'/><Value Type='Boolean'>1</Value></Eq></Or></Where></Query></View>"

    Example for AND

    In this query, I am looking for items, where the value for Stakeholder is 1 and the value Win is yes.

    Get-PnPListItem -List $ListName -Query "<View><Query><Where><And><Eq><FieldRef Name='Stakeholder'/><Value Type='Integer'>1</Value></Eq><Eq><FieldRef Name='Win'/><Value Type='Boolean'>1</Value></Eq></And></Where></Query></View>"

    Complete example

    $Url = "https://devmodernworkplace.sharepoint.com/sites/Sales" 
    $ListName = "Opportunities"
    
    Connect-PnPOnline -Url $Url -Interactive
    
    $AmountOfStakeholders = 2
    $ColumName = "Stakeholder"
    
    Get-PnPListItem -List $ListName -Query "<View><Query><Where><Eq><FieldRef Name='$ColumName'/><Value Type='Integer'>$AmountOfStakeholders</Value></Eq></Where></Query></View>"

    Troubleshooting

    I am getting to many items

    Error

    You get nearly every item in the list, but you are filtering for specific SharePoint items

    Cause

    Maybe you forgot the <View> part?

    Resolution

    Without view:

    With view:

    Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Eq><FieldRef Name='Stakeholder'/><Value Type='Integer'>2</Value></Eq></Where></Query></View>"

    Exception from HRESULT: 0x80131904

    Error message:

    Get-PnPListItem : Exception from HRESULT: 0x80131904
    In Zeile:1 Zeichen:1
    + Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><gt ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (:) [Get-PnPListItem], ServerException
        + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Lists.GetListItem

    Cause:

    You did not care about the case sensitivity of the logical operands

    Resolution:

    Wrong:

    Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><gt><FieldRef Name='Created'/><Value Type='DateTime' IncludeTimeValue='FALSE'>$CreationDate</Value></gt></Where></Query></View>"

    Right:

    Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Gt><FieldRef Name='Created'/><Value Type='DateTime' IncludeTimeValue='FALSE'>$CreationDate</Value></Gt></Where></Query></View>"

    Field types are not installed properly

    Error message in german

    Get-PnPListItem : Mindestens ein Feld ist nicht richtig installiert. Wechseln Sie zur Listeneinstellungsseite, um diese Felder zu löschen.
    In Zeile:1 Zeichen:1
    + Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Gt ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (:) [Get-PnPListItem], ServerException
        + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Lists.GetListItem

    Error message in english

    Get-PnPListItem : One or more field types are not installed properly. Go to the list settings page to delete these fields.
    In Zeile:1 Zeichen:1
    + Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Gt ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (:) [Get-PnPListItem], ServerException
        + FullyQualifiedErrorId : EXCEPTION,PnP.PowerShell.Commands.Lists.GetListItem

    Cause

    You did not care of the case sensitivity of the column name

    Resolution

    Wrong:

    Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Gt><FieldRef Name='created'/><Value Type='DateTime' IncludeTimeValue='FALSE'>$CreationDate</Value></Gt></Where></Query></View>"

    Right:

    Get-PnPListItem -List "Opportunities" -Query "<View><Query><Where><Gt><FieldRef Name='Created'/><Value Type='DateTime' IncludeTimeValue='FALSE'>$CreationDate</Value></Gt></Where></Query></View>"

    Bild von Deedee86 auf Pixabay