Skip to content

Instantly share code, notes, and snippets.

@OlafD
OlafD / SPOWithRest.ps1
Created May 8, 2020 13:15
Short example on how to use an app registration to call rest services for SharePoint Online from PowerShell.
# set variables
$clientId = "{id of your registered app}"
$clientSecret = "{secret of your registered app}"
$realm = "{id of the tenant}" # could be found in the registered app in Azure Active Directory
$principal = "00000003-0000-0ff1-ce00-000000000000" # this is the SharePoint resource
$targetHost = "{SharePoint root of the tenant}" # example: mytenant.sharepoint.com
# url used for the rest call later to test the access
@OlafD
OlafD / SharePointMakeNewsPage.ps1
Created April 17, 2020 12:23
Change the site type of a page in the Site Pages library in SharePoint Online from an Article page to an unpublished News page.
$item = Get-PnPListItem -List "Site Pages" -Id 11 # use the id of your page
$item["PromotedState"] = 1
$item.Update()
Invoke-PnPQuery
@OlafD
OlafD / SharePointMakeArticlePage.ps1
Created April 17, 2020 12:21
Change the site type of a page in the Site Pages library in SharePoint Online from a News page to an Article page.
$item = Get-PnPListItem -List "Site Pages" -Id 11 # use the id of your page
$item["PromotedState"] = 0
$item.Update()
Invoke-PnPQuery
@OlafD
OlafD / SetTokenLifetimeInAzure.ps1
Last active April 15, 2020 11:26
Set the access token lifetime in Azure AD for the whole organization. In the script it will be set to 10min, the minimum possible. Needs the module Azure AD PowerShell 2.0, To run connect with Connect-AzureAD to your directory.
$policy = New-AzureADPolicy `
-Definition @('{"TokenLifetimePolicy":{"Version":1,"AccessTokenLifetime":"00:10:00","MaxAgeSessionSingleFactor":"00:10:00"}}') `
-DisplayName "WebPolicyScenario" `
-IsOrganizationDefault $true `
-Type "TokenLifetimePolicy"
@OlafD
OlafD / IndexOfNth.psq1
Created January 17, 2020 13:08
Find the index of the n-th occurence of a pattern (or a single character) in a string.
<#
Find the n-th occurence of a pattern in a string
#>
function IndexOfNth
{
param (
[string]$String,
[string]$Pattern,
[int]$Occurence
@OlafD
OlafD / GetNameOfReaderRole.ps1
Created October 25, 2019 08:58
For multilingual environments, a short version to get the name of the reader role in SharePoint Online. PowerShell PnP is needed for this approach.
$web = Get-PnPWeb -Includes RoleDefinitions
$readerRole = $roleDefinitions.GetByType([Microsoft.SharePoint.Client.RoleType]::Reader)
(Get-PnPContext).Load($readerRole)
Invoke-PnPQuery
$readerRole.Name
@OlafD
OlafD / FormatMapImage.html
Created August 29, 2019 06:56
Use an image from formatmap32x32.png
<span id="csvViewsMonthsForPagesInSiteUrl" class="ms-cui-img-32by32 ms-cui-img-cont-float" onclick='triggerCsvSearch("ViewsMonthsSitePages");'>
<img src="/_layouts/15/1033/images/formatmap32x32.png" style="top: -69px; left: -511px" />
</span>
@OlafD
OlafD / GetGroupsAndMemberships.ps1
Created August 21, 2019 11:33
When connected to a SharePoint site with PowerShell PnP, list all groups and the group members. The script will not show any kind of permissions.
Write-Host
Write-Host "Groups Overview"
Write-Host "==============="
Write-Host
$groups = Get-PnPGroup
foreach ($group in $groups)
{
@OlafD
OlafD / SharePointCSOMQueryLargeLists.cs
Created July 31, 2019 06:09
Simple sample for looping a list with a caml query for a list with more than 5000 items.
List list = _ctx.Web.Lists.GetByTitle(Constants.Listnames.DocumentHistoryList);
_ctx.Load(list);
_ctx.ExecuteQueryRetry();
ListItemCollectionPosition collectionPosition = null;
CamlQuery query = new CamlQuery();
query.AllowIncrementalResults = true;
@OlafD
OlafD / ChangeFieldType.ps1
Created May 28, 2019 12:27
Change the FieldTypeKind of a field using PowerShell PnP.
$field = Get-PnPField -List "Documents" -Identity "Users" -Includes FieldTypeKind
$field.FieldTypeKind = [Microsoft.SharePoint.Client.FieldType]::User
$field.Update()
Invoke-PnPQuery