Post

Get content types and columns with PowerShell for SharePoint Online
There is already a script on my blog which lists the site structure for SharePoint Online but I also created a script to get all content types and columns with PowerShell for SharePoint Online. The script will do the following: – Get all content types for each site collection in SharePoint Online – Get columns […]

There is already a script on my blog which lists the site structure for SharePoint Online but I also created a script to get all content types and columns with PowerShell for SharePoint Online. The script will do the following:

– Get all content types for each site collection in SharePoint Online

– Get columns associated with each content type in SharePoint Online

– collect this information and store this in an HTML table

The script will generate the following output:

image_thumb[15]

You will need to install the SharePoint Online Management Shell from https://www.microsoft.com/en-us/download/details.aspx?id=35588 and all of its pre-requisites. The below script needs to be copied to a .ps1 file on the workstation you are planning to run this script. It will create a logfile on the same location.

function connectToO365{
# Let the user fill in their admin url, username and password
$adminUrl = Read-Host "Enter the Admin URL of 0365 (eg. https://<Tenant Name>-admin.sharepoint.com)"
$userName = Read-Host "Enter the username of 0365 (eg. admin@<tenantName>.onmicrosoft.com)"
$password = Read-Host "Please enter the password for $($userName)" -AsSecureString
 
# set credentials
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $userName, $password
$SPOCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $password)
 
#connect to to Office 365
try{
Connect-SPOService -Url $adminUrl -Credential $credentials
write-host "Info: Connected succesfully to Office 365" -foregroundcolor green
}
catch{
write-host "Error: Could not connect to Office 365" -foregroundcolor red
Break connectToO365
}
 
#create HTML file
$filePath = create-outputfile
 
#start getting site collections
get-siteCollections
 
add-content -value "
</body>
</html>
" -path $filePath
}
 
function create-outputfile(){
#Create unique string from the date
$date = get-date -format dMMyyyyhhmm
 
#set the full path
$filePath = "$($PSScriptRoot)\Output$($date).html"
 
#test path
if (!(Test-Path -path $filePath)){
#create file
New-Item $filePath -type file | out-null
#print info
write-host “File created: $($filePath)” -foregroundcolor green
#add start HTML information to file
add-content -value "
<html>
<body>
<h1>Content Types and Columns in Office 365</h1>
<table border='1' style='font-family: Calibri, sans-serif'>
<tr>
<th style='background-color:blue; color:white'>Site Collection</th>
<th style='background-color:blue; color:white'>Content Type</th>
<th style='background-color:blue; color:white'>Columns</th>
</tr>
" -path $filePath
}
else{
#break so there won't be duplicate files
write-host "Output file already exists, wait 1 minute" -foregroundcolor yellow
Break create-outputfile
}
return $filePath
}
 
function get-siteCollections{
#Get all site collections
$siteCollections = Get-SPOSite
 
#loop through all site collections
foreach ($siteCollection in $siteCollections)
{
#add info to HTML document
add-content -value "<tr style='background-color:orange'><td>$($siteCollection.url)</td><td></td><td></td></tr>" -path $filePath
write-host "Info: Found $($siteCollection.url)" -foregroundcolor green
 
#get content type information for each site collection
Get-SPOContentTypes($siteCollection.url)
}
}
 
function Get-SPOContentTypes($url){
#fill metadata information to the client context variable
$context = New-Object Microsoft.SharePoint.Client.ClientContext($url)
$context.Credentials = $SPOcredentials
$web = $context.Web
$context.Load($web.ContentTypes)
try{
$context.ExecuteQuery()
#loop through all content types in the site collection
foreach($ct in $web.ContentTypes){
#skip hidden content types
if ($ct.group -ne "_hidden")
{
$i++
add-content -value "<tr style='background-color:yellow'><td></td><td>$($ct.name)</td><td></td></tr>" -path $filePath
 
#get columns of content type
$context.load($ct.fields)
$context.ExecuteQuery()
get-SPOColumns $ct
}
}
write-host "Info: Found $($i) content types" -foregroundcolor green
}
catch{
write-host "Could not load content types" -foregroundcolor red
}
 
}
 
function Get-SPOColumns($ct){
try{
#get column information from the variable
foreach($ctColumn in $ct.fields){
#skip content type entry for each content type
if ($ctColumn.Title -ne "Content Type"){
#skip hidden columns
if ($ctColumn.hidden -ne $true){
$j++
add-content -value "<tr style='background-color:lightyellow'><td></td><td></td><td>$($ctColumn.title)</td></tr>" -path $filePath
}
}
}
write-host "Info: Found $($j) columns in $($ct.name)" -foregroundcolor green
}
catch{
write-host "Could not load columns" -foregroundcolor red
}
 
}
 
connectToO365

 

Manual

This script has been run on a workstation with Windows 8.1. First run the SharePoint Online Management Shell as administrator

SharePoint Online Management Shell logo.

Then run the following command

& “script location”

image_thumb[1]

And answer the following three questions

image_thumb[12]

And press enter

image_thumb[9]

All content types and columns have been successfully crawled and its contents is listed in the HTML document located next to the .ps1 file

Information

You can list more information if you want and you can find the properties on the following locations:

– SPOSite properties: http://documents.software.dell.com/one-identity-quick-connect-for-cloud-services/3.6/administrator-guide/working-with-microsoft-office-365/microsoft-office-365-data-supported-out-of-the-box/sposite-object-attributes

– Web properties: https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.client.web_properties.aspx

Let me know if you have any remarks or updates

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Archive