0

I tried this script

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
Start-SPAssignment -Global
$Web = 'http://test'
$SPWeb = Get-SPWeb -Identity $Web
write-Host $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

to create a Picture library, But It throws an error message

**Get-SPWeb : Cannot find an SPWeb object with Id or Url :****

And I need to create it for different subsites also, I am not sure how to achieve this, Please suggest me some idea?

I referred this links

How to create Picture Library on office365 sharepoint using powershell?

1
  • have u opened the client context? Commented Jan 16, 2017 at 8:28

2 Answers 2

1
function Create-SharePointLibrary { 
    [CmdletBinding()]    
    Param(    
       [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]    
       [string]$webUrl,
       [Parameter(Mandatory=$true, Position=1)]   
       [string]$LibraryName, 
       [Parameter(Mandatory=$true, Position=2)]
       [string]$Description, 
       [Parameter(Mandatory=$false, Position=3)]
       [string]$LibraryTemplate
    )  
   Process   
   {  
      Start-SPAssignment -Global    
      $spWeb = Get-SPWeb -Identity $webUrl       
      $spListCollection = $spWeb.Lists    
      $spLibrary = $spListCollection.TryGetList($LibraryName)  
      if($spLibrary -ne $null) {
          Write-Host -f Yellow "Library $LibraryName already exists in the site"

      } else {           
          Write-Host -NoNewLine -f yellow "Creating  Library - $LibraryName"   
          $spListCollection.Add($LibraryName, $Description, $LibraryTemplate)    
          $spLibrary = $spWeb.GetList($spWeb.ServerRelativeUrl+"/"+"$LibraryName") 
          Write-Host -f Green "...Success!"    
      }            
      Stop-SPAssignment -Global  
   } 
} 

Calling Function

$webUrl = "http://sever:port"
#-----------Picture Library------------
$PicLibName = "Pictures"  
$LibraryTemplateDL = [Microsoft.SharePoint.SPListTemplateType]::PictureLibrary
Create-SharePointLibrary $webUrl  $PicLibName $PicLibName $LibraryTemplateDL
-1

Try it as below:

Add-PSSnapin "Microsoft.SharePoint.PowerShell"
Start-SPAssignment -Global
$SPWeb = Get-SPWeb -Identity "http://test"
$listTemplate = $SPWeb.ListTemplates["Picture Library"]
$SPWeb.Lists.Add("Custom Picture Library","Custom Picture Library desc",$listTemplate)
$list = $SPWeb.Lists["Custom Picture Library"]
$list.Title = "Custom Picture Library"
$list.Update()
$SPWeb.Dispose()    
Stop-SPAssignment -Global
3
  • Thanks, while running the script it throws an error abe02e3c-e792-4692-884c-d78463545712 Index operation failed; the array index evaluated to null. At C:\Users\Administrator\desktop\Picturelibrary.ps1:6 char:1 + $list = $SPWeb.Lists[$ListUrl] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Commented Jan 16, 2017 at 9:43
  • But Picture Library is created @GautamSheth Commented Jan 16, 2017 at 9:44
  • Edited code, can you please check ? Commented Jan 16, 2017 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.