Tag: sharepoint

Office 365 Advent Calendar – 18 Adding a Web Part to a SharePoint Online Page with PowerShell



You’ve got a Web Part which you want to add to one, but more likely multiple web part pages in a SharePoint Online Site. Or maybe even on multiple pages across multiple sites. You could do this manually, but adding the same web part with the same configuration over and over again may not be a productive use of it. Let’s make use of PowerShell to do this!


The code itself is quite simple, as the PnP PowerShell cmdlets contain a cmdlet named Add-PnPWebPartToWebPartPage. All you need is the page where you want to add the web part as well as the XML of your web part.

Let’s say you’ve got a “This SharePoint site will be archived on 31/12/2016” message which you want to add to the default.aspx page on a list of sites. You can create the Web Part with the desired message and formatting once, then export it:

Once exported, you can use the following script to add the same web part to all default.aspx pages on all sites listed in SitesToArchive.csv. The Web Part will be added to the Zone named “Left” with an index of 0, that is at the top of it:

$cred = Get-Credential
$websToUpdate = Import-Csv SitesToArchive.csv
foreach($web in $websToUpdate) {
	write-host "Connecting to $($web.Url)"
	Connect-PnPOnline -Url $web.Url -Credentials $cred
	Add-PnPWebPartToWebPartPage -ServerRelativePageUrl "default.aspx"`
	-Path "c:\users\rmodery\Desktop\ArchiveMessageWebPart.dwp" -ZoneId "Left" -ZoneIndex 0


How about wiki pages in SharePoint Online? Add-PnPWebPartToWikiPage may help you here!

Office 365 Advent Calendar – 17 Overview of the new OneDrive for Business Admin Portal


At the Microsoft Ignite conference in September, it was announced that OneDrive for Business would get its own Admin Portal in Office 365. Yesterday, Microsoft finally made it available, even though it’s in Preview at the moment. See the official announcement here, and access it at https://admin.onedrive.com.

So what does it include? Some existing settings, such as configuring your sharing settings, as well as new settings like the possibility to restrict access to certain IP addresses. Here’s a detailed overview of the current status:



In this section, you can control if sharing with externals is enabled, and if so, with which configuration. These are the same settings which currently exist in the SharePoint Admin Portal.



In the Sync section, you can define a few settings related to the OneDrive for Business client. First of all, whether user can install the client from the website. You can also define if syncing should be restricted to PCs that are joined to specific domains –  to retrieve the GUIDs you need to enter here follow these instructions. This setting is extremely helpful if you want to ensure that your users don’t use for example their personal PCs to synchronise content. Lastly, you can also restrict which file types can be synced.



This is a simple section – define how muich default storage is available for new users, and define how long you want to keep files in a user’s OneDrive after his account is scheduled for deletion. This setting could previously be set via PowerShell only, and allows for values between 30 and 3650 days (yes, the maximum retention period is 10 years).


Device access

If you want to control from which devices and locations content can be accessed, you can define specific IP addresses here or define your Intune settings.


Lastly, the Compliance section provides you with a range of links to existing pages where you can define auditing, DLP, retention, eDiscovery, and Alerts settings.

Office 365 Advent Calendar – 04 Moving/Copying files within SharePoint Online Libraries



You have a couple of files within a SharePoint Online document library which you want to move within the same library, for example to a new folder.


Note: The following steps are only available in the ‘Modern Experience’ for document libraries. If you’re still using the ‘Classic Experience’ (for example because your tenant settings have been configured to use Classic), then you can’t make use of this new functionality yet.

In my example here, I have a library with 4 reports as well as a newly created ‘Reports’ folder. I want to move the 4 reports into the new folder.move-copy-files-sharepoint-online-1


Select all files that you want to copy or move. The options for the library change and you can now see ‘Delete’, ‘Move to’, and ‘Copy to’. I’m going to use ‘Move to’
The next step now is to choose where to move the items to. Moving and Copying is only possible within the same library, at the moment you can’t move/copy documents to other libraries. You can also create a new Folder in the current location that you navigated to in the sidepane.move-copy-files-sharepoint-online-3


After I selected where I want to move my documents, I simply need to click the ‘Move here’ button.move-copy-files-sharepoint-online-4


And that’s it, my 4 reports are now in my newly created folder.



Office 365 Advent Calendar – 03 Programatically accessing a SharePoint Online Recycle Bin



You’ve got a big amount of items in the recycle bin of a site which you want to access. For example, a user accidentally deleted hundreds of files, and now you want to restore them back easily. Or you have a thousand items in the recycle bin and you want to export some information (which items are in the recycle bin, who deleted them and when) to Excel


Once again, we’re using the PnP PowerShell cmdlets here as a basis, but then also make use of the underlying CSOM calls to access the recycle bin.

$cred = Get-Credential
#Set URL to the web for which you want to retrieve the recycle bin
Connect-PnPOnline -Url https://mytenant.sharepoint.com/sites/departmentsite -Credentials $cred

$web = Get-PnPWeb
$ctx = Get-PnPContext

#Loading the Recycle Bin Items
$recycleBin = $web.RecycleBin

#We are now narrowing down the items. We only want to restore all Excel spreadsheets
$spreadsheets = $recycleBin | Where {$_.Title -like "*.xlsx"}

#Another example: select all items deleted by a specific user
#$deletedByUser = $recycleBin | Where {$_.DeletedByEmail -eq "rene@mytenant.onmicrosoft.com"}
#Restoring the spreadsheets
$spreadsheets | %{ $_.Restore() }

#Exporting all items to a CSV file
$recycleBin | Select Title, DeletedByEmail, DeletedDate | Export-Csv C:\RecycleBinItems.csv

Example of a CSV export:

Project_Template.pptxrene@mytenant.onmicrosoft.com12/3/2016 1:19
Azure_Information_Protection_licensing_datasheet_EN-US.pdfrene@mytenant.onmicrosoft.com12/3/2016 1:20
Security in Office 365 Whitepaper.docxrene@mytenant.onmicrosoft.com12/3/2016 1:20

If you are curious which methods and properties are available for the recycle bin in general as well as individual items, please review RecycleBinItemCollection and RecycleBinItem.

Office 365 Advent Calendar – 02 Retrieving SharePoint Online Site Collection Sizes



You want to get an overview of the storage usage of all site collections in your tenant, similar to what you can see in the “SharePoint Administration” section in Office 365. But as you may have hundreds of site collections, or as you want to pass on that information to someone else, you want to extract this information into a spreadsheet


$cred = Get-Credential
Connect-PnPOnline -Url "https://mytenant-admin.sharepoint.com" -Credentials $cred
$sites = Get-PnPTenantSite -Detailed | select Url, StorageUsage, StorageMaximumLevel, @{label="Usage in %";Expression={[math]::Round($_.StorageUsage*100/$_.StorageMaximumLevel,2)}}
$sites | Export-Csv C:\SiteStorage.csv


Here is a sample output:

UrlStorageUsageStorageMaximumLevelUsage in %

Note: StorageUsage and StorageMaximumLevel are shown in MB

Office 365 Advent Calendar – 01 Getting all Permissions Levels in SharePoint Online



You want to get an overview of all Permission Levels that are set up in your tenant’s SharePoint Online site collections. Not only the out-of-the-box permission such as “Full Control” or “Read”, but also any custom permission levels which may have been defined by your site collection’s administrators (“Add Only” is a common example).


A prerequisite for this code are the OfficeDev PnP PowerShell cmdlets. Ensure that you have a current version installed before proceeding.
The following code connects to your tenant and fetches all site collections (with a small where query, as I wanted to limit it to all sites in the /sites/ or /teams/ paths). It then loops through all site collections, retrieves the permission levels, and writes that information into a CSV file.

$cred = Get-Credential
Connect-PnPOnline -Url https://mytenant-admin.sharepoint.com -Credentials $cred
$sites = Get-PnPTenantSite | where {$_.Url -like "*mytenant.sharepoint.com/sites/*" -or $_.Url -like "*mytenant.sharepoint.com/teams/*"}
foreach($site in $sites) {
	Connect-PnPOnline -Url $site.Url -Credentials $cred
	$ctx = Get-PnPContext
	$roleDefs = (Get-PnPWeb).RoleDefinitions
	foreach($rd in $roleDefs) {
		Add-Content ".\permissionlevels.csv" "$($site.url), $($rd.Name)"

Office 365 Advent Calendar – 24 Days of Goodies


Christmas is approaching, and I’ve decided to spread some joy and share some knowledge  by running an Office 365 Advent Calendar on my blog from December 1 – December 24 2016! This means that on each of these days in December, I’ll publish a small blog post with some Office 365 goodies – PowerShell scripts, helpful advice, guidance, …. While the content will mainly be focused on SharePoint Online, the other Office 365 services may be referenced in some posts as well.

When will new content be posted?

A new blog post will be published every day from December 1 – December 24 at 8am GMT.

How can I stay up to date on this and get notified of new content?

There are a few ways how you can do this:

  1. Bookmark /office-365-advent-calendar/ and visit it on a daily basis
  2. Subscribe to this blog’s RSS feed
  3. Get notified via email, for example via https://blogtrottr.com
  4. Follow me on Twitter, I’ll tweet updates at least once a day

User Group presentation: OneDrive for Business – Current State and New Features

Yesterday, I talked at the SharePoint Community meetup about “OneDrive for Business – Current State and New Features”. Here are the slides that I used:

SharePoint Online vs On-premises vs Hybrid – 2016 edition

My presentation from yesterday’s Singapore SharePoint Community event: