Get Team sites & Create Document Library in SharePoint with PowerShell PnP

I needed to create a new document library to all our project related SharePoint Team sites. We use Office 365 Teams for both business units as for our projects, so first we need to get a list of all Team sites within the tenant, then check if the site is a project related Team site to finally add a new document library to it.

To get the job done we are going to use PowerShell PnP : more info about PnP

Getting all Team sites using PowerShell

To get all the team sites we need to connect to the SharePoint Admin Center to retrieve a list called : DO_NOT_DELETE_SPLIST_TENANTADMIN_AGGREGATED_SITECOLLECTIONS

Connecting to the admin center

You can use one of mine connector scripts for it here at Technet. Or connect to SharePoint with:

#Create credential object
$cred = Get-Credential

#SharePoint admin center url (note the -admin)
$siteUrl = 'https://contoso-admin.sharepoint.com'
 
#Import the Skype for Business Online PS session
Connect-PnPOnline -url $siteUrl -Credentials $cred

Get the Team sites

The SharePoint Online site collection is stored in a hidden list, called DO_NOT_DELETE_SPLIST_TENANTADMIN_AGGREGATED_SITECOLLECTIONS .  To retrieve the content of this list we first get all the available list items

$site = Get-PnPSite
$site.Context.Load( $site.RootWeb)
$site.Context.ExecuteQuery()
 
$web = $site.RootWeb
 
$site.Context.Load( $web.Lists)
$site.Context.ExecuteQuery()

$web.lists

get sharepoint teams lists

So in this case it’s the third item, so we use $web.lists[2] to get the teams sites

$lists = $web.lists[2]

Check if it’s a project related Team sites

Now I wanted to now it the Team sites in the list is project related. All our project sites have the same document libraries, so the easiest way is to check if a particular document library exist:

for ($x = 0; $x -lt $list.ItemCount; $x++ )
{
$item = $list.GetItemById($x)

$site.Context.Load( $item)
$site.Context.ExecuteQuery()

	if($item.FieldValues.SiteUrl -ne $null)
	{
		connect-pnponline -url $item.FieldValues.SiteUrl -cred $cred
		$getReferenceLibrary = get-pnplist -Identity 'Document Library Name'

		if($getReferenceLibrary -ne $null)
		{
			#Add new document library
		}
	}
}

We walk trough the list items, check if it as has SiteUrl, connect to the site with pnponline and stored credentials. Get the pnplist with the name ‘Document Library Name’, if it returns not null, then we can add the new document library.

Creating new Document Library

The last part is pretty easy, all we need to do is add a new Document Library to the Teams site. Just to be sure, we check first if the document library not already exists.

for ($x = 0; $x -lt $list.ItemCount; $x++ )
{
$item = $list.GetItemById($x)

$site.Context.Load( $item)
$site.Context.ExecuteQuery()

	if($item.FieldValues.SiteUrl -ne $null)
	{
		connect-pnponline -url $item.FieldValues.SiteUrl -cred $cred
		
		#Get reference library
		$getReferenceLibrary = get-pnplist -Identity 'Document Library Name'
		
		#Check if new library not already exists
		$checkNewLibrary = get-pnplist -Identity 'New Document Library Name'

		if($getReferenceLibrary -ne $null -and $checkNewLibrary -eq $null)
		{
			#Add new document library
			new-pnplist -Title 'New Document Library Name' -Template DocumentLibrary -OnQuickLaunch
		}
	}
}

Thanks to Pieter Veenstra for retrieving the Team Sites 

And to Veronique Lengelle for the Create Document Library part

2 thoughts on “Get Team sites & Create Document Library in SharePoint with PowerShell PnP”

  1. I was following your example, and the PowerShell commands, to create a document library for a number of SharePoint Team sites in SharePoint Online though I do not see a reference to DO_NOT_DELETE_SPLIST_TENANTADMIN_AGGREGATED_SITECOLLECTIONS in my list that is generated. It is possible that it has been renamed? Or am I missing something?

    This is the list I received back from the PowerShell script:
    Title Id Url
    —– — —
    Access Requests 56124706-43d7-4c81-a5b0-9e6fecea8349
    appdata 7fb0fdc2-0674-46db-bea6-858fba9490b3
    appfiles c197452b-8095-401e-b772-9e5b12507002
    Cache Profiles f011646c-0c13-4ebc-8115-2f63198e3357
    Comments.30017244-8B12-4607-9D41-89A859BD352D f68fc53e-18f7-4860-80fd-b58df5a047a5
    Composed Looks 967e9474-e69f-4b26-a534-8fa66815501c
    Content and Structure Reports 0def3959-d97d-4def-af4f-73293e07a489
    Content type publishing error log 588859fd-a7ef-493c-8cb7-1ef2b4dcd34b
    Converted Forms 2059e8b7-dfbd-4b79-91cc-70a4cd09b2a7
    Device Channels 6495730d-9d0e-4520-a7af-82666ae2edea
    Documents fae4474e-02df-4f21-8007-b326323857e9
    Events bd5ce8dd-4a18-40d5-99d5-d6644b38e190
    Form Templates f52abce5-4b80-4224-b36f-4bb842c14955
    Forms e9834dfd-e176-48a8-8f73-1fc43731a9e0
    Generic Forms 67ffb463-6091-4b29-a3ab-9521002e6341
    List Template Gallery 245229e7-59ab-49bc-8452-3d3e9257709f
    Long Running Operation Status 66c295f7-b116-489b-afef-c024195df283
    Maintenance Log Library ecb2626a-c153-4ee2-85ec-4b81b90515a3
    Master Page Gallery 43e642b0-e34e-4641-81e7-40ecc6f6beab
    MicroFeed 4bf2d5e0-9fed-4d6a-9ff9-36ea63e62218
    Project Policy Item List b3591c80-f6ab-4d70-a598-8910133c9f1a
    Relationships List 0f0250f4-6064-4d3e-8032-94560abd0de8
    Reusable Content 46f335f6-73d3-43db-ad99-5b2132abc0bc
    SharePointHomeOrgLinks c2813ba7-9188-416d-9f49-8c50941f99cd
    Sharing Links a3b3786c-b3a2-458e-b03e-366d8f105f13
    Site Assets 29cecb71-418e-4e88-8432-239eea236c2d
    Site Pages ed741304-f508-4f87-8a0c-27febb95cecd
    Solution Gallery 94273ff9-9e04-4e44-a58f-50e34901ad37
    Style Library db21086f-e3d0-4bc0-83ee-a057bb1fd3f6
    Suggested Content Browser Locations 18c8f77c-b11f-4e5f-9eb9-c795b9351f42
    TaxonomyHiddenList 22e03727-fde0-4f07-b6fa-65c36179bf3a
    Team Announcements d99a0fc0-09c8-4169-9f4b-6757e22e7d1e
    Team Calendar 9f8662a3-0ad7-46c6-9e59-ee098413e12f
    Theme Gallery 0d30f720-0cdf-4238-893e-e25831cb8212
    Translation Packages 303977da-3dcd-4bba-b3d3-2da6771cc67e
    Translation Status ecd2a3da-df76-4e34-94bc-3c7bd5bd3a1c
    User Information List 7cf795e9-818d-4eb7-997a-4685015d3324
    Web Part Gallery 2cc0d7b9-94e8-46ea-a558-efc591e2b30a
    wfpub cfbad76d-626f-4cf6-a0fd-6ed9408f4494
    Workflow Tasks 7f4da08c-1457-4c8f-a668-e3bb32a631aa

    Regards,
    John

    • Well, I have to look into the script because I noticed this week that my document libraries are not listed anymore in the menu. They are visible in Site-Content though.

Leave a Comment

0 Shares
Tweet
Pin
Share
Share