Monday, April 11, 2011

SPNavigationNode object won't take title

I was trying to script the creation of Quick Launch links. This appears to be quite straightforward. Here is the Powershell:
$SPSite = Get-SPSite "<some URL>"
$OpenWeb = $SPSite.OpenWeb()
$QuickLaunch = $OpenWeb.Navigation.QuickLaunch



$node = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode("Some Title", "<some URL", $true)

At this point I was curious to see what was in the $node object. So, I took a look and it looks something like this:


Title                  :
TitleResource          :
IsVisible              :
IsExternal             : True
Id                     : 0
ParentId               : 0
Parent                 :
Navigation             :
Url                    : <some URL>
LastModified           : 1/1/0001 12:00:00 AM
Children               : {}
Properties             :
TargetSecurityScopeId  : 00000000-0000-0000-0000-000000000000
TargetParentObjectType : Web

So, why is the Title blank? I tried a whole bunch of things with no luck. Then I read the documentation of SPNavigationNode more carefully. In the Remarks section, it states: "The new SPNavigationNode object is not completely initialized until it has been added to a collection. For more information, see the SPNavigationNodeCollection class."

Are you kidding me? Creating an object through a constructor does not fully create the object. You have to add it to some kind of collection and that has the side effect of filling in the rest of the properties. IMHO, that is very poor design.

Oh well, so I continued with adding the node with the following Powershell and all is good:
$QuickLaunch.AddAsLast($node)





2 comments:

  1. I was wondering too why I could not set the SPNavigationNode.Title when using a script (SPNavigationNode Exception setting Title object reference not set to an instance of an object blabla issue) but you pointed out the "why". Thanks!

    ReplyDelete
  2. This is another fucked up sharepoint design... urhh thanks .

    ReplyDelete