1

After created site collection in PowerShell with this command:

 $SiteCollection = New-SPSite -Url $WebAPP.Url -OwnerAlias $SiteColl_Owner -Language 1033 -Template $SiteColl_Template -Name $SiteColl_Name

I want to use SPSite object without Get-SPSite

$spListCollection = $SiteCollection.Lists 

foreach($List in $SiteCollLists.List) 
{ 
   $ListTemplate = $SiteCollection.ListTemplates[$List.Template] 
   $spListCollection.Add($List.Name, $List.Name, $ListTemplate) 
}

Error is:

Cannot index into a null array. At C:\Users\mmma\Desktop\CreatePortal\TestCreateInfra.ps1:69 char:8 + $ListTemplate = $SiteCollection.ListTemplates[$List.Template] + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : NullArray

2
  • But new site has been created rite? Dec 29, 2017 at 9:02
  • Yes, new site has been created.
    – Goshky
    Dec 29, 2017 at 9:08

2 Answers 2

3

I can only assume that $SiteCollection = New-SPSite ... after successful creation of new site it's not returning site object to $SiteCollection variable.

So, try to $SiteCollection = Get-SPSite *newsiteurl* after creation.

As per MSDN documentation there is NO site return type for New-SPSite.

1

Site collections do not have lists, webs do.

$SiteCollection = New-SPSite ...

$myWeb = $SiteCollection.RootWeb
$spListCollection = $myWeb.Lists 

foreach($List in $SiteCollLists.List) 
{ 
   ...
}

Same thing is true of ListTemplates. That is a property of the root web object.

Your code looks like it's trying to duplicate every list from one web to another. Keep in mind that a new site starts with a number of visible and hidden lists, and your code would try to duplicate all of them. A new STS#0 Team Site starts with these.

Title
-----
appdata
Composed Looks
Content type publishing error log
Converted Forms
Documents
Form Templates
List Template Gallery
Master Page Gallery
MicroFeed
Project Policy Item List
Site Assets
Site Pages
Solution Gallery
Style Library
TaxonomyHiddenList
Theme Gallery
User Information List
Web Part Gallery
wfpub

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.