I am creating a new site in 2013 with powershell (New-SPWeb) and attempting to set some Search Navigation settings. The problem I encounter is that my Get-SPWeb call fails until I access the site via the browser. After that, the powershell commands work as expected. Does anyone have any insight as to what might be happening? There are no errors when I access the site via the browser and I am actually able to get the Get-SPWeb object but when I use it as follows, it gives me an error.
$web = get-spweb http://sc.mysite.ca/TestNewSite
$node = New-Object -TypeName "Microsoft.SharePoint.Navigation.SPNavigationNode" -ArgumentList "This Site", "/TestNewSite/template/_layouts/15/osssearchresults.aspx?u=http://sc.mysite.ca/TestNewSite", $true;
$web.Navigation.SearchNav.AddAsFirst($node);
Error:
You cannot call a method on a null-valued expression.
At C:\scripts\Create-NewSite.ps1:984 char:1
+ $web.Navigation.SearchNav.AddAsFirst($node);
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
If I put a sleep command in before the $web.Navigation.SearchNav.AddAsFirst($node)
call, then access the site via the browser, the program runs successfully. I have seen some posts referring to the fact that there may be an error with the front page, but I have no errors indicating any issues. I am using a standard template PROJECTSITE#0.
Some additional messages I have been able to narrow down from the logs:
CatalogConfig.ReadFromPropertyBag; Catalog configurations don't exist for site collection http://sc.mysite.ca/TestNewSite.
SiteServicesAddins.ReadFromPropertyBag: Site services addins don't exist for site collection http://sc.mysite.ca/TestNewSite.
Here is the output of the SPWeb object:
Url
---
http://sc.mysite.ca/TestNewSite
Here is the output of the $node1 object:
Title :
TitleResource :
IsVisible :
IsDocLib :
IsExternal : True
Id : 0
ParentId : 0
Parent :
Navigation :
Url : /TestNewSite/Project Template/_layouts/15/osssearchresults.aspx?u=http://sc.mysite.ca/TestNewSite
LastModified : 1/1/0001 12:00:00 AM
Children : {}
Properties :
TargetSecurityScopeId : 00000000-0000-0000-0000-000000000000
TargetParentObjectType : Web
Here is the output of the $node2 object:
Title :
TitleResource :
IsVisible :
IsDocLib :
IsExternal : True
Id : 0
ParentId : 0
Parent :
Navigation :
Url : http://search.myite.ca/Pages/results.aspx
LastModified : 1/1/0001 12:00:00 AM
Children : {}
Properties :
TargetSecurityScopeId : 00000000-0000-0000-0000-000000000000
TargetParentObjectType : Web
Populating navigation children for web
, I'm guessing this may be related.