Skip to content

Instantly share code, notes, and snippets.

View aflyen's full-sized avatar

Are Flyen aflyen

View GitHub Profile
@aflyen
aflyen / DisableTemplateGalleryDialog.ps1
Last active May 25, 2022 13:41
Hide the template gallery from modern team and communication sites in SharePoint Online using PowerShell and PnP.Framework
Connect-PnPOnline -Url https://contoso.sharepoint.com/sites/project101
$Web = Get-PnPWeb -Includes WebTemplatesGalleryFirstRunEnabled
$Web.WebTemplatesGalleryFirstRunEnabled = $false
$Web.Update()
Invoke-PnPQuery
@aflyen
aflyen / CustomService.ts
Created March 29, 2022 19:10
Baseline service for SharePoint Framework projects based on the service locator pattern
import { ServiceKey, ServiceScope } from '@microsoft/sp-core-library';
import { SPHttpClient, MSGraphClientFactory, MSGraphClient } from '@microsoft/sp-http';
import { PageContext } from '@microsoft/sp-page-context';
export interface ICustomService {
getMyProfile(): Promise<IGraphUser>;
}
export interface IGraphUser {
businessPhones?: string[];
@aflyen
aflyen / GetOldFilesFromSiteCamlQuery.xml
Last active March 25, 2022 15:12
CAML Query for use with Get-PnPListItem to find all files older than a specific date in a library (https://pnp.github.io/powershell/cmdlets/Get-PnPListItem.html)
<View Scope='RecursiveAll'>
<ViewFields>
<FieldRef Name='FileLeafRef'/>
<FieldRef Name='Modified'/>
</ViewFields>
<Query>
<Where>
<Leq>
<FieldRef Name='Modified'/>
<Value Type='DateTime'>2022-03-25</Value>
@aflyen
aflyen / DisableFirstRunDialog.cs
Created March 8, 2022 08:51
Hide the first run dialog from modern team and communication sites in SharePoint Online using C# and PnP.Framework
public async Task DisableTemplateGalleryDialog(SharePointClientContext context)
{
Web web = context.Web;
context.Load(web);
await context.ExecuteQueryRetryAsync();
web.NextStepsFirstRunEnabled = false;
web.Update();
await context.ExecuteQueryRetryAsync();
}
@aflyen
aflyen / DisableTemplateGalleryDialog.cs
Last active March 8, 2022 08:50
Hide the template gallery from modern team and communication sites in SharePoint Online using C# and PnP.Framework
public async Task DisableTemplateGalleryDialog(SharePointClientContext context)
{
Web web = context.Web;
context.Load(web);
await context.ExecuteQueryRetryAsync();
web.WebTemplatesGalleryFirstRunEnabled = false;
web.Update();
await context.ExecuteQueryRetryAsync();
}
@aflyen
aflyen / SetHideFromAddressLists.cs
Last active March 8, 2022 08:41
Set the Microsoft 365 group to not be visible in the Global Address List in Exchange with C# and Microsoft Graph SDK. Ref: https://docs.microsoft.com/en-us/graph/api/group-update?view=graph-rest-1.0&tabs=http
private async Task SetHideFromAddressLists(GraphServiceClient client, string groupId)
{
var retry = true;
var retryCount = 0;
var body = new
{
hideFromAddressLists = true
};
@aflyen
aflyen / BulkDeleteSites.ps1
Last active March 8, 2022 08:34
Bulk delete sites from CSV file in SharePoint Online with PowerShell
# Read CSV file with a column "URL" including the full URL to the sites to be deleted
$Sites = Import-Csv .\sites.csv
# Connect to admin site
Connect-PnPOnline -Url https://contoso-admin.sharepoint.com
# Delete all sites to recycle bin
$Sites | ForEach-Object {
Write-Host "Remove: $($_.URL)"
Remove-PnPTenantSite -Url $_.URL -Force
@aflyen
aflyen / BulkDeleteModernTeamSites.ps1
Last active March 8, 2022 08:34
Bulk delete modern team sites from SharePoint Online with PowerShell
# Read CSV file with a column "URL" including the full URL to the sites to be deleted
$Sites = Import-Csv .\sites.csv
# Connect to admin site
Connect-PnPOnline -Url https://contoso-admin.sharepoint.com
# Delete all sites to recycle bin
$Sites | ForEach-Object {
Write-Host "Remove: $($_.URL)"
$Site = Get-PnPTenantSite -Identity $_.URL
@aflyen
aflyen / UserSharePointApiWithUserContextFromPowerShell.ps1
Created March 4, 2022 14:44
Access the SharePoint API using a delegated context (username and password) for those functions not supported with application based authentication in PowerShell
function Send-CorpSharePointGetRequest
{
Param(
[Parameter(Mandatory=$true)]
[string]$Url,
[Parameter(Mandatory=$true)]
[string]$UserName,
[Parameter(Mandatory=$true)]
[System.Security.SecureString]$Password,
[Parameter(Mandatory=$true)]
@aflyen
aflyen / GetAccessTokenFromAad.ps1
Created March 4, 2022 14:41
Get access token from Azure Active Directory with ClientId and ClienctSecret in PowerShell
function Get-CorpMicrosoftGraphAccessToken
{
Param(
[Parameter(Mandatory=$true)]
[string]$TenantId,
[Parameter(Mandatory=$true)]
[string]$ClientId,
[Parameter(Mandatory=$true)]
[string]$ClientSecret
)