In post we will see how to get the sub site details in a site collection in SharePoint online using power shell and save the details in .csv file
Make sure the SDK SharePoint Online Client Components SDK was installed in the system
1) Create a folder( with name Get all Site Collection Info) in one of the local drive.
2) copy the Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll to that folder. we will find the dlls in the location C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI after installing the SDK
3) copy the below script to notepad and save the file with Get all Site Collection Info.ps1
#Add - PSSnapin Microsoft.SharePoint.PowerShell $host.Runspace.ThreadOptions = "ReuseThread" #Definition of the function that allows to create a new view in a SharePoint Online list function Get-SubSiteCollectionInfo { param ($sCSOMPath,$sSiteUrl,$sUserName,$sPassword,$sWeburl,$FileName) try { #Adding the Client OM Assemblies $sCSOMRuntimePath=$sCSOMPath + "\Microsoft.SharePoint.Client.Runtime.dll" $sCSOMPathdll=$sCSOMPath + "\Microsoft.SharePoint.Client.dll" Add-Type -Path $sCSOMPathdll Add-Type -Path $sCSOMRuntimePath #SPO Client Object Model Context $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl) $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword) $spoCtx.Credentials = $spoCredentials $web = $spoCtx.Web $spSites = $web.Webs $spoCtx.Load($web) $spoCtx.Load($spSites) $spoCtx.ExecuteQuery() $AllSites = @() foreach ($webitem in $spSites) { $AllSites = New-Object -TypeName PSObject -Property @{ SiteID = $webitem.ID SiteName = $webitem.Title SiteUrl = ($sWeburl+$webitem.ServerRelativeUrl) } | Select SiteID,SiteName,SiteUrl $AllSites| Export-CSV ($sCSOMPath+"\"+$FileName+".csv") -NoTypeInformation -Append Get-SubSiteCollectionInfo -sCSOMPath $sCSOMPath -sSiteUrl ($sWeburl+$webitem.ServerRelativeUrl) -sUserName $sUserName -sPassword $sPassword -sWeburl $sWeburl -FileName $FileName } $spoCtx.Dispose() } catch [System.Exception] { $FailedSite = @() $FailedSite += New-Object -TypeName PSObject -Property @{ SiteUrl =$sSiteUrl Exception=$_.Exception.Message } | Select SiteUrl,Exception $FailedSite| Export-CSV ($sCSOMPath+"\"+$FileName+"_Failed.csv") -NoTypeInformation -Append #-Encoding UTF8 Write-Host -ForegroundColor Red $_.Exception.ToString() #Read-Host -Prompt "Operation failed..! Press any key to close this" } } function Get-SiteCollectionInfo { param ($sCSOMPath,$sSiteUrl,$sUserName,$sPassword,$sWeburl,$FileName) try { #Adding the Client OM Assemblies $sCSOMRuntimePath=$sCSOMPath + "\Microsoft.SharePoint.Client.Runtime.dll" $sCSOMPathdll=$sCSOMPath + "\Microsoft.SharePoint.Client.dll" Add-Type -Path $sCSOMPathdll Add-Type -Path $sCSOMRuntimePath #SPO Client Object Model Context $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl) $spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword) $spoCtx.Credentials = $spoCredentials Write-Host "Getting All Sub Sites in a Site Collection " Write-Host "-------------------------------------------" $web = $spoCtx.Web $spSites1 = $web.Webs $spoCtx.Load($web) $spoCtx.Load($spSites1) $spoCtx.ExecuteQuery() $AllSites1 = @() foreach ($webitem in $web) { $AllSites1 = New-Object -TypeName PSObject -Property @{ SiteID = $webitem.ID SiteName = $webitem.Title SiteUrl = $sSiteUrl } | Select SiteID,SiteName,SiteUrl } $AllSites1| Export-CSV ($sCSOMPath+"\"+$FileName+".csv") -NoTypeInformation -Append foreach ($webitem1 in $spSites1) { $AllSites1 = New-Object -TypeName PSObject -Property @{ SiteID = $webitem1.ID SiteName = $webitem1.Title SiteUrl = ($sWeburl+$webitem1.ServerRelativeUrl) } | Select SiteID,SiteName,SiteUrl $AllSites1| Export-CSV ($sCSOMPath+"\"+$FileName+".csv") -NoTypeInformation -Append Get-SubSiteCollectionInfo -sCSOMPath $sCSOMPath -sSiteUrl ($sWeburl+$webitem1.ServerRelativeUrl) -sUserName $sUserName -sPassword $sPassword -sWeburl $sWeburl -FileName $FileName } Read-Host -Prompt "file created Successfull..! in the following path $sCSOMPath, Press any key to close this window" $spoCtx.Dispose() } catch [System.Exception] { Write-Host -ForegroundColor Red $_.Exception.ToString() Read-Host -Prompt "Operation failed..! Press any key to close this" } } $scriptpath = $MyInvocation.MyCommand.Path $dir = Split-Path $scriptpath $FileName= "Source_SiteCollection" #Read-Host -Prompt "Enter File Name to write (eg: XXXXX)" $sWeburl = "https://tarun.sharepoint.com" #Read-Host -Prompt "Enter Root Site URL (eg: http://Server:port/)" $sUserName = "admin@tarun.onmicrosoft.com" #Read-Host -Prompt "Enter User Name" $sPassword = ConvertTo-SecureString "XXXX" -AsPlainText -Force #Read-Host -Prompt "Enter your password" -AsSecureString $sSiteUrl = "https://tarun.sharepoint.com/sites/sharepointmates" #Read-Host -Prompt "Enter Site Collection URL (eg: http://Server:port/Sites/Dev)" Get-SiteCollectionInfo -sCSOMPath $dir -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword -sWeburl $sWeburl -FileName $FileName
Note: please update the $sWeburl,$sUserName,$sPassword and $sSiteUrl variables with your SharePoint online environment.
6)Now the folder will look like this
5)Now Right click on the Get all Site Collection Info Power shell script on select Run with PowerShell
6) After Successful execution of the scripts we will get the below message
7) Now go to the folder, we will find the excel file.
8) Open the excel, We will find all the details of the sub sites in the excel file as below
On Premises:
For On premises we need to slightly change $spoCredentials and $sUserName variables in the above script.
$spoCredentials = New-Object System.Net.NetworkCredential($sUserName, $sPassword)
We need to give the user name along with domain name as below
$sUserName = "2013dev\spadmin"
Please change the on premises site collection url , web url and password as well.
[…] This post is the second post in series of the Power shell Scripts for Getting the data from site collection in SharePoint Online. you can access the first post Get all Sub site details in Site Collection in SharePoint online using Power shell . […]
LikeLike
[…] Get all Sub site details in Site Collection in SharePoint online using Power shell . […]
LikeLike
[…] Get all Sub site details in Site Collection in SharePoint online using Power shell . […]
LikeLike
[…] Get all Sub site details in Site Collection in SharePoint online using Power shell . […]
LikeLike