Tag Archives: PowerShell

Fastest way to delete all items in a large SPO list: PnP.PowerShell batches

Scenario: You have a large (>5k items) list in SharePoint Online.
You need to delete this list. “Remove-PnPList” fails with a message “The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator“. Deleting with UI fails too.

Try this PowerShell command with ScriptBlock:

Get-PnPListItem -List $list -Fields "ID" -PageSize 100 -ScriptBlock { Param($items) $items | Sort-Object -Property Id -Descending | ForEach-Object{ $_.DeleteObject() } } 

or this PowerShell with batches:

$batch = New-PnPBatch
1..12000 | Foreach-Object { Remove-PnPListItem -List $list -Identity $_ -Batch $batch }
Invoke-PnPBatch -Batch $batch

for me both methods gave same good result: ~17 items per second ( ~7 times faster than regular).

PnP.PowerShell batches

With a new PnP.PowerShell we can perform some operations against an SPO list with batches!
How fast PnP batches are? My measurements:

Time elapsed, secondswith batcheswith scriptBlockwithout batches
Add-PnPListItem (100 items)4.33 seconds42 seconds
Add-PnPListItem (500 items)21 seconds234 seconds
Add-PnPListItem (7000 items)314 seconds
Remove-PnPListItem (1000 items)58 seconds58 seconds429 seconds
Remove-PnPListItem (7000 items)395 seconds397 seconds

i.e. with batches your pnp.powershell code runs 7-10 times faster!

References:

How to create a Sub-Site if subsites creation is disabled

Microsoft recommend “flat structure”, i.e. no subsites. So SPO admins are disabling subsites creation at tenant level. Did you know that you still can create subsite? Let me explain how it is done.

If creation subsites is allowed, you should be able to see it like this:

But actually subsites are not always best practice. Microsoft recommend “flat structure”, i.e. instead of subsite you should have a separate site collection, and if you need a hierarchy and navigation – use Hub sites. So, in Office 365 SharePoint admins usually “disable” SubSites creation:

Now, you see, SubSites are not really disabled, but only the button to create subsites is hidden: “This controls whether the Subsite command appears on the New menu on the Site contents page”.

Anyway, the result is: you are not able to create a SubSite (web) in SharePoint Online:

Actually there are at least 3 options to create a SubSite:

Option 1. Create a SubSite in Classic mode.

Step 1: Select “Site Contents” page
Step 2: Click “Return to classic SharePoint”
Step 3: Create SubSite

Option 2. Create a SubSite from “Sites and Workspaces” page

Step 1: Go to “Site Settings”
Step 2: Select “Sites and Workspaces” page (site/_layouts/15/mngsubwebs.aspx)
Step 3: Create a SubSite

Option 3: use PowerShell PnP

Step 1: Install PowerShell PnP
Step 2: Connect to your site with PnP
Step 3: create a SubSite

Install-Module SharePointPnPPowerShellOnline
Connect-PnPOnline -Url <your site Url> -UseWebLogin
New-PnPWeb -Url "<new Web Url>" -Title "MySubSite" -Template "STS#3"

References:

See also: How to create a SharePoint Site in Office 365 if site creation is disabled

Allow commenting on modern pages in SharePoint Online

Microsoft: “You can also select to allow or prevent commenting on modern pages. If you allow commenting, it can be turned on or off at the page level

By default both
– Allow users to create new modern pages
– Allow commenting on modern pages
are turned on (enabled)

Tenant or SharePoint admin can find settings under
SharePoint Admin Center -> Settings -> Pages

When you are creating a modern site page – there is an option “Comments” turned On by default:

And page with comments looks like:

Comments on site pages (aka modern pages) can be enabled or disabled at each of the levels:
– Tenant level
– Site (aka site collection) level
– Web (aka subsite ) level
– Page level
Here is how it is done:

LevelHow it’s doneWho can do it
TenantGUI ( SharePoint Administration)
or PowerShell
Global Administrator or
SharePoint Administrator
Site (Site Collection)PowerShellGlobal Administrator or
SharePoint Administrator
Web (Subsite)PowerShellSite Collection Administrator or Owner
(Full Control rights to web)
PageGUI (Page Editing screen)Site Member
(Edit right to page)

If commenting on modern pages disabled at higher level – lower level settings do not work. E.g. If you disable “Allow commenting on modern pages” at tenant level (it takes minutes) – the functionality will gone from all modern pages of all sites.

When you switch page comments Off – all existing comments will be hidden (but not deleted).
If you later turn comments On – comments will reappear, including Likes.

If “Allow commenting on modern pages” disabled at tenant or web level – you will not see “Comments On/Off” switch while editing page.
If “Allow commenting on modern pages” disabled at site collection level – you will see “Comments On/Off” switch while editing page, but you will not be able to turn it ON.

PowerShell

When you disable “Allow commenting on modern pages” at tenant level –
PowerShell Object (site/web) property “CommentsOnSitePagesDisabled” will not be changed for any site/web.
You can still with PowerShell set it to True/False:
“Set-PnPWeb -CommentsOnSitePagesDisabled:$false”
but it does not take any effect.

If you enable “Allow commenting on modern pages” at tenant level (it takes ~10 minutes) – the functionality will return to all modern pages and
all webs and sites properties “CommentsOnSitePagesDisabled” will ???.
You can change it with PowerShell:
“Set-PnPWeb -CommentsOnSitePagesDisabled:$false”.

# having Site Collection Admin Permissions:
# disable Comments On Site Pages for a subsite:
$webName = "SubSite_02"
Set-PnPWeb -Web $webName -CommentsOnSitePagesDisabled:$true 
# enable Comments On Site Pages for a subsite 
# (only if comments enabled at tenant level):
Set-PnPWeb -Web $webName -CommentsOnSitePagesDisabled:$false


# having global admin or SharePoint admin permissions:
# site collection:
Set-PnPTenantSite -Url $siteUrl -CommentsOnSitePagesDisabled:$true
# tenant-level Comments:
Set-PnPTenant -CommentsOnSitePagesDisabled:$true # disable
 comments
Set-PnPTenant -CommentsOnSitePagesDisabled:$false # enable comments

# does not work:
Set-PnPSite -CommentsOnSitePagesDisabled:$true

How do I know if if the page is modern page or classic page (PowerShell)?

$list = Get-PnPList "Site Pages" -Includes ContentTypes, Fields
$list.ContentTypes | ft -a
$cType = Get-PnPContentType -List $list | ?{$_.Name -eq 'Site Page'};
$cType.id
$queryString = "<View><Query><Where><Eq><FieldRef Name='ContentTypeId'/><Value Type='Text'>" + $cType.Id.StringValue + "</Value></Eq></Where></Query></View>"  
$modernPages = Get-PnPListItem -List $list -Query $queryString
$modernPages.count
$modernPages | ft -a

How to I know if the page is a Home Page (PowerShell)?

# web object contains relative link to the web's Home Page:
$web = Get-PnPWeb -Connection $siteConnection -includes WelcomePage
$web.WelcomePage

References

– Microsoft “Allow users to create and comment modern pages

See also:
Allow users to create modern pages

Note:
We did not discuss “Wiki Pages” or “Web part Pages”, we discussed only “Modern Pages” (aka Site Pages).
I have tested it all personally using Communication sites.
MS-Team (group-based) and standalone SharePoint (no-group) sites – TBP.