Not a long blog post, just straight to the point.
I have made a script that creates a team in Teams with a project structure as the use case. It creates predefined folders under each channels files tab.
The channels root document folder in SharePoint is not created until you click the file tab in that specific channel, therefore I create these in the script so I can go on to create subfolders.
Connecting to Graph API is done through an application and application secret from Azure. More on this in Lee Ford’s blog post here.
How to create a folder in SharePoint I got from this blog post here.
The module I used for the SharePoint part is the SharePointPnPPowerShellOnline module. You can find it here.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Param( | |
[Parameter (Mandatory= $true)] | |
[String] $teamname, | |
[Parameter (Mandatory= $true)] | |
[String] $owner, | |
[Parameter (Mandatory= $true)] | |
[String] $privatepublic, | |
[Parameter (Mandatory= $true)] | |
[String] $description | |
) | |
$Cred = Get-Credential | |
$Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid -Credential $Cred -Authentication Basic -AllowRedirection | |
Import-PSSession -Session $Session -DisableNameChecking:$true -AllowClobber:$true | Out-Null | |
Connect-MicrosoftTeams -Credential $Cred | |
# Azure AD OAuth Application Token for Graph API | |
# Get OAuth token for a AAD Application (returned as $token) | |
# Application (client) ID, tenant ID and secret | |
#Application (client) ID, tenant ID and secret | |
$clientId = "xxxxxxxxxxxxxxxxxxxxxxxx" | |
$tenantId = "xxxxxxxxxxxxxxxxxxxxxxxxxx" | |
$clientSecret = "XXXXXXXXXXXXXXXXXXXXX" | |
# Contruct URI | |
$uri = "https://login.microsoftonline.com/$tenantId/oauth2/v2.0/token" | |
# Construct Body | |
$body1 = @{ | |
client_id = $clientId | |
scope = "https://graph.microsoft.com/.default" | |
client_secret = $clientSecret | |
grant_type = "client_credentials" | |
} | |
# Get OAuth 2.0 Token | |
$tokenRequest = Invoke-WebRequest -Method Post -Uri $uri -ContentType "application/x-www-form-urlencoded" -Body $body1 -UseBasicParsing | |
$token = ($tokenRequest.Content | ConvertFrom-Json).access_token | |
#Get ID of team requester and set as owner. | |
$uri = 'https://graph.microsoft.com/beta/users/'+"$owner"+'?$select=id' | |
$method = "GET" | |
$query = Invoke-WebRequest -Method $method -Uri $uri -ContentType "application/json" -Headers @{Authorization = "Bearer $token"} -ErrorAction Stop -UseBasicParsing | |
$ownerID = ($query.content | ConvertFrom-Json).id | |
# Specify the URI to call and method | |
$uri = "https://graph.microsoft.com/beta/teams" | |
$method = "POST" | |
$body = @" | |
{ | |
"[email protected]": "https://graph.microsoft.com/beta/teamsTemplates/standard", | |
"displayName": "$teamname", | |
"description": "$description", | |
"channels": [ | |
{ | |
"displayName": "01-Management", | |
"isFavoriteByDefault": true, | |
"description": "Description" | |
}, | |
{ | |
"displayName": "02-Developement", | |
"isFavoriteByDefault": true, | |
"description": "DEscription" | |
}, | |
{ | |
"displayName": "03-Marketing", | |
"isFavoriteByDefault": true, | |
"description": "Description" | |
}, | |
{ | |
"displayName": "04-Finance", | |
"isFavoriteByDefault": true, | |
"description": "Description" | |
} | |
], | |
"memberSettings": { | |
"allowCreateUpdateChannels": true, | |
"allowDeleteChannels": false, | |
"allowAddRemoveApps": true, | |
"allowCreateUpdateRemoveTabs": true, | |
"allowCreateUpdateRemoveConnectors": true | |
}, | |
"guestSettings": { | |
"allowCreateUpdateChannels": false, | |
"allowDeleteChannels": false | |
}, | |
"funSettings": { | |
"allowGiphy": true, | |
"giphyContentRating": "Moderate", | |
"allowStickersAndMemes": true, | |
"allowCustomMemes": true | |
}, | |
"messagingSettings": { | |
"allowUserEditMessages": true, | |
"allowUserDeleteMessages": true, | |
"allowOwnerDeleteMessages": true, | |
"allowTeamMentions": true, | |
"allowChannelMentions": true | |
}, | |
"visibility": "$Private", | |
"[email protected]": [ | |
"https://graph.microsoft.com/beta/users('$ownerID')" | |
] | |
} | |
"@ | |
$query = Invoke-WebRequest -Method $method -Uri $uri -ContentType "application/json" -Body $body -Headers @{Authorization = "Bearer $token"} -ErrorAction Stop -UseBasicParsing | |
$location = ($query.Headers).Location | |
$GroupID = $location.Substring(8, 36) | |
Start-Sleep -Seconds 60 | |
$TeamSiteName = (Get-Team -groupid $GroupID).MailNickName | |
$SiteURL = "https://alexholmeset.sharepoint.com/sites/"+$TeamSiteName | |
$DocumentLibrary = "/shared documents" | |
#Channels | |
#Config Variables | |
$FolderNames= "01-Management","02-Developement","03-Marketing","04-Finance" | |
$RelativeURL= "$DocumentLibrary" #Relative URL of the Parent Folder | |
Try { | |
#Connect to PNP Online | |
Connect-PnPOnline -Url $SiteURL -Credentials $Cred | |
#sharepoint online create folder powershell | |
Foreach($Folder in $FolderNames){ | |
Add-PnPFolder -Name $folder -Folder $RelativeURL -ErrorAction Stop | |
Write-host -f Green "New Folder '$Folder' Added!" | |
} | |
} | |
catch { | |
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red | |
} | |
#01-Management | |
#Config Variables | |
$FolderNames= "Meetings","Presentations" | |
$RelativeURL= $DocumentLibrary+"/01-management" #Relative URL of the Parent Folder | |
Try { | |
#Connect to PNP Online | |
Connect-PnPOnline -Url $SiteURL -Credentials $Cred | |
#sharepoint online create folder powershell | |
Foreach($Folder in $FolderNames){ | |
Add-PnPFolder -Name $folder -Folder $RelativeURL -ErrorAction Stop | |
Write-host -f Green "New Folder '$Folder' Added!" | |
} | |
} | |
catch { | |
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red | |
} | |
#02-Developement | |
#Config Variables | |
$FolderNames= "Design","Specs","Labeling" | |
$RelativeURL= $DocumentLibrary+"/02-Developement" #Relative URL of the Parent Folder | |
Try { | |
#Connect to PNP Online | |
Connect-PnPOnline -Url $SiteURL -Credentials $Cred | |
#sharepoint online create folder powershell | |
Foreach($Folder in $FolderNames){ | |
Add-PnPFolder -Name $folder -Folder $RelativeURL -ErrorAction Stop | |
Write-host -f Green "New Folder '$Folder' Added!" | |
} | |
} | |
catch { | |
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red | |
} | |
#Subfolder | |
$FolderNames= "Sketches","Requirements" | |
$RelativeURL= $DocumentLibrary+"/02-Developement/Design" #Relative URL of the Parent Folder | |
Try { | |
#Connect to PNP Online | |
Connect-PnPOnline -Url $SiteURL -Credentials $Cred | |
#sharepoint online create folder powershell | |
Foreach($Folder in $FolderNames){ | |
Add-PnPFolder -Name $folder -Folder $RelativeURL -ErrorAction Stop | |
Write-host -f Green "New Folder '$Folder' Added!" | |
} | |
} | |
catch { | |
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red | |
} | |
#03-Marketing | |
#Config Variables | |
$FolderNames= "Communication Brief","Competitor Review","Consumer Insights","Product FAQ","Product Information","Product Strategy" | |
$RelativeURL= $DocumentLibrary+"/03-Marketing" #Relative URL of the Parent Folder | |
Try { | |
#Connect to PNP Online | |
Connect-PnPOnline -Url $SiteURL -Credentials $Cred | |
#sharepoint online create folder powershell | |
Foreach($Folder in $FolderNames){ | |
Add-PnPFolder -Name $folder -Folder $RelativeURL -ErrorAction Stop | |
Write-host -f Green "New Folder '$Folder' Added!" | |
} | |
} | |
catch { | |
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red | |
} | |
#04-Finance | |
#Config Variables | |
$FolderNames= "Budget","Presentations" | |
$RelativeURL= $DocumentLibrary+"/04-Finance" #Relative URL of the Parent Folder | |
Try { | |
#Connect to PNP Online | |
Connect-PnPOnline -Url $SiteURL -Credentials $Cred | |
#sharepoint online create folder powershell | |
Foreach($Folder in $FolderNames){ | |
Add-PnPFolder -Name $folder -Folder $RelativeURL -ErrorAction Stop | |
Write-host -f Green "New Folder '$Folder' Added!" | |
} | |
} | |
catch { | |
write-host "Error: $($_.Exception.Message)" -foregroundcolor Red | |
} | |
Thank You for Sharing. This is very useful
LikeLike