Question

Currently I have created SharePoint Hosted App (Picture Slider) in Office 365.I have created picture Library Manually, and now trying to create Picture Library using powershell in office 365.

Was it helpful?

Solution

PowerShell to create Sharepoint Picture Library:

############################################################################################################################################ 
#Script that allows to create a new list in a SharePoint Online Site 
# Required Parameters: 
#  -> $sUserName: User Name to connect to the SharePoint Online Site Collection. 
#  -> $sPassword: Password for the user. 
#  -> $sCSOMPath: CSOM Assemblies Path. 
#  -> $sSiteUrl: SharePoint Online Site Url. 
#  -> $sListName: Name of the list we are going to create. 
#  -> $sListDescription: List description. 
############################################################################################################################################ 

$host.Runspace.ThreadOptions = "ReuseThread" 

#Definition of the function that allows to create a new view in a SharePoint Online list 
function Create-NewListSPO 
{ 
    param ($sCSOMPath,$sSiteUrl,$sUserName,$sPassword,$sListName,$sListDescription) 
    try 
    {    
        #Adding the Client OM Assemblies         
        $sCSOMRuntimePath=$sCSOMPath +  "\Microsoft.SharePoint.Client.Runtime.dll"         
        $sCSOMPath=$sCSOMPath +  "\Microsoft.SharePoint.Client.dll"              
        Add-Type -Path $sCSOMPath          
        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 "----------------------------------------------------------------------------"  -foregroundcolor Green 
        Write-Host "Creating List $sListName in $sSiteUrl !!" -ForegroundColor Green 
        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green         

        $spoWeb=$spoCtx.Web 
        $spoListCreationInformation=New-Object Microsoft.SharePoint.Client.ListCreationInformation 
        $spoListCreationInformation.Title=$sListName 
        #https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.client.listtemplatetype.aspx 
        $spoListCreationInformation.TemplateType=[int][Microsoft.SharePoint.Client.ListTemplatetype]::PictureLibrary 
        $spoList=$spoWeb.Lists.Add($spoListCreationInformation) 
        $spoList.Description=$sListDescription 
        $spoCtx.ExecuteQuery() 

        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green 
        Write-Host "Lsita $sListName created in $sSiteUrl !!" -ForegroundColor Green 
        Write-Host "----------------------------------------------------------------------------"  -foregroundcolor Green   
        $spoCtx.Dispose() 
    } 
    catch [System.Exception] 
    { 
        Write-Host -ForegroundColor Red $_.Exception.ToString()    
    }     
} 

#Required Parameters 
$sSiteUrl = "https://<O365Domain>.sharepoint.com/<SPO_Site>" 
$sUserName = "<O365User>@<O365Domain>.onmicrosoft.com"  
$sListName= "<SPO_List_Name>" 
$sListDescription="<List Description>" 
#$sPassword = Read-Host -Prompt "Enter your password: " -AsSecureString   
$sPassword=ConvertTo-SecureString "<SPO_Password>" -AsPlainText -Force 
$sCSOMPath="<SPO_Path>" 

Create-NewListSPO -sCSOMPath $sCSOMPath -sSiteUrl $sSiteUrl -sUserName $sUserName -sPassword $sPassword -sListName $sListName -sListDescription $sListDescription

In $sCSOMPath set the path for SharePoint Client dll.

Ref: How to create a List in a SharePoint Online Site using PowerShell

OTHER TIPS

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
Start-SPAssignment -Global
$SPWeb = Get-SPWeb -Identity $Web
$listTemplate = $SPWeb.ListTemplates["Picture Library"]
$SPWeb.Lists.Add($ListUrl,$Description,$listTemplate)
$list = $SPWeb.Lists[$ListUrl]
$list.Title = $ListTitle
$list.Update()
$SPWeb.Dispose()    
Stop-SPAssignment -Global
$clientContext = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $securePassword) 
$clientContext.Credentials = $credentials 

if (!$clientContext.ServerObjectIsNull.Value) 
{ 
    Write-Host "Connected to SharePoint Online site: '$Url'" -ForegroundColor Green 
} 
$web = $clientContext.Web
$templates = $clientContext.Site.GetCustomListTemplates($web)
$clientContext.Load($templates)
$clientContext.ExecuteQuery()

$template = $templates | Where-Object{ $_.Name -eq "PictureLibrary" }

$lci = New-Object Microsoft.SharePoint.Client.ListCreationInformation
$lci.Title = "Custom Picture library"
$lci.TemplateFeatureId = $template.FeatureId
$lci.TemplateType = $template.ListTemplateTypeKind
$lci.DocumentTemplateType = $template.ListTemplateTypeKind

$lists = $clientContext.Web.Lists;
$clientContext.Load($lists);
$clientContext.ExecuteQuery();

$list = $lists.Add($lci)
$list.Update()
$clientContext.ExecuteQuery()
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top