none
PowerShell Script

    Question

  • Hi,

    I am using Online SharePoint. In that I have a Site Collection. In that Site Collection I have many sub site I want to add a common Document Library in all the Sub sites at once with Power Shell Script.

    Can anyone tell me the Script for this?

    Thanks.

    Thursday, March 16, 2017 5:53 PM

Answers

  • Hi

    this is the code you need

    cls
    $url="https://site_collection_O365_URL"
    $description = " This is my library"
    $Title = "00My List Title"
    $list_ID = 101
    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
     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
    $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()
    }

    where most common list/libraries IDs are :

    Document Library
    101
    Form Library
    115
    Wiki Page Library
    119
    Picture Library
    109
    Links
    103
    Announcements
    104
    Contacts
    105
    Calendar
    106
    Promoted Links
    170
    Discussion Board
    108
    Tasks (2010)
    107
    Tasks
    171
    Project Tasks
    150
    Issue Tracking
    1100
    Custom List
    100

    Let me know if you need more details


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.



    Thursday, March 16, 2017 9:49 PM

All replies

  • Use PnP Powershell script - 

    https://github.com/SharePoint/PnP-PowerShell/blob/master/Documentation/NewPnPList.md

    Refer to this PnP Powershell sample, which deactivates feature in all sub sites in the site collection, instead of deactivate cmdlet replace it with the "NewPnPList" cmdlet to create the library.


    ---
    Rajesh
    rjesh.com| @rjesh
    You don't need to buy me a beer, if helpful just smile, vote, and mark it as answer.

    Thursday, March 16, 2017 6:41 PM
  • I am using Online SharePoint(O365). How to run this code for O365 SharePoint?
    Thursday, March 16, 2017 6:55 PM
  • PnP PowerShell works for Online and On-Premises.

    Read the documentation for more details - https://github.com/SharePoint/PnP-PowerShell


    ---
    Rajesh
    rjesh.com| @rjesh
    You don't need to buy me a beer, if helpful just smile, vote, and mark it as answer.

    Thursday, March 16, 2017 9:12 PM
  • Hi

    this is the code you need

    cls
    $url="https://site_collection_O365_URL"
    $description = " This is my library"
    $Title = "00My List Title"
    $list_ID = 101
    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
     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
    $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()
    }

    where most common list/libraries IDs are :

    Document Library
    101
    Form Library
    115
    Wiki Page Library
    119
    Picture Library
    109
    Links
    103
    Announcements
    104
    Contacts
    105
    Calendar
    106
    Promoted Links
    170
    Discussion Board
    108
    Tasks (2010)
    107
    Tasks
    171
    Project Tasks
    150
    Issue Tracking
    1100
    Custom List
    100

    Let me know if you need more details


    Romeo Donca, Orange Romania (MCSE, MCITP, CCNA) Please Mark As Answer if my post solves your problem or Vote As Helpful if the post has been helpful for you.



    Thursday, March 16, 2017 9:49 PM
  • Hi Romeo,

    Thanks for your Script. It works. Great!!!

    Can you give me script for creating multiple sub site at once ? I mean that details of Sub Sites stores in csv file , I wanna to import that details and create multiple sub site within a Site Collection using power shell for Online SharePoint.

    Thanks Once Again.

    Friday, March 17, 2017 5:20 AM