Create a library in each subsite in SharePoint Online

Starting from this post, bellow is the script which will create a library in each site for a specific site collection.

$url = “your site collection URL”
$description = ” This is my library”
$Title = “00My List Title in
$list_ID = 101
####### connectiong to SharePoint Online
Add-Type -Path “path\Microsoft.SharePoint.Client.dll”
Add-Type -Path “path\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “path\Microsoft.Online.SharePoint.Client.Tenant.dll”
$global:cred = Get-Credential
Connect-MsolService -Credential $global:cred -ErrorAction SilentlyContinue
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($Url)
$cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.username, $cred.Password) 
$clientContext.Credentials = $cred
$web = $clientContext.Web
$clientContext.Load($web)
$clientContext.ExecuteQuery()
#create document library in site collection
 write-host ” Creating the library in site: “$web.Title
$newlistinfo  = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$newlistinfo.Description=$description
$newlistinfo.TemplateType = $list_ID
$newlistinfo.title = $title + $web.Title
$newlist = $Web.Lists.Add($newlistinfo)
$clientContext.Load($newlist)
$clientContext.ExecuteQuery()
############# creation for each sub-site in that site-collection
$sites = $web.Webs
$clientContext.Load($sites)
$clientContext.ExecuteQuery()
foreach ($item in $sites)
{
    write-host ” Creating the library in site: “$item.Title
    $newlist = $item.Lists.Add($newlistinfo)
    $clientContext.Load($newlist)
    $clientContext.ExecuteQuery()
}
This entry was posted in CSOM, Office 365, Powershell, Sharepoint Online. Bookmark the permalink.