I've moved my blog to!. Please update your links. This blog is no longer in use--you can find all posts and comments at my new blog; I will no longer be posting to this site and comments have been disabled.

Friday, November 2, 2007

Set Navigation Settings

A while back I had created a command which allowed you to set the navigation elements/nodes for a given site. The command allowed you to also set a couple of basic switches that appear on the Site Navigation Settings page (Home > Site Settings > Modify Navigation) but it didn't allow you to set all the flags. I thought about possibly modifying this command to allow all the flags to be set but then decided it would introduce some complexities that were just not worth introducing.

In the end I decided to create a new command, gl-setnavigationsettings, which would allow you to set all the values found on the above mentioned page with the exception of the navigation nodes themselves. Fortunately, the code for this was extremely simple (especially considering I already had chunks that I could use thanks to the other command I had created). The code simply sets properties on the PublishingWeb object based on parameters provided by the user:

   1: public override int Run(string command, System.Collections.Specialized.StringDictionary keyValues, out string output)
   2: {
   3:  output = string.Empty;
   5:  InitParameters(keyValues);
   7:  string url = Params["url"].Value.TrimEnd('/');
   9:  using (SPSite site = new SPSite(url))
  10:  {
  11:   using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(url)])
  12:   {
  13:    PublishingWeb pubweb = PublishingWeb.GetPublishingWeb(web);
  15:    if (Params["showsubsites"].UserTypedIn)
  16:     pubweb.IncludeSubSitesInNavigation = bool.Parse(Params["showsubsites"].Value);
  18:    if (Params["showpages"].UserTypedIn)
  19:     pubweb.IncludePagesInNavigation = bool.Parse(Params["showpages"].Value);
  21:    OrderingMethod sortMethod = pubweb.NavigationOrderingMethod;
  22:    if (Params["sortmethod"].UserTypedIn)
  23:    {
  24:     sortMethod = (OrderingMethod)Enum.Parse(typeof (OrderingMethod), Params["sortmethod"].Value, true);
  25:     pubweb.NavigationOrderingMethod = sortMethod;
  26:    }
  28:    if (sortMethod != OrderingMethod.Manual)
  29:    {
  30:     if (Params["autosortmethod"].UserTypedIn)
  31:      pubweb.NavigationAutomaticSortingMethod = (AutomaticSortingMethod)Enum.Parse(typeof(AutomaticSortingMethod), Params["autosortmethod"].Value, true);
  32:     if (Params["sortascending"].UserTypedIn)
  33:      pubweb.NavigationSortAscending = bool.Parse(Params["sortascending"].Value);
  34:    }
  35:    else
  36:    {
  37:     if (Params["autosortmethod"].UserTypedIn)
  38:      Console.WriteLine("WARNING: parameter autosortmethod is incompatible with sortmethod {0}.  The parameter will be ignored.", sortMethod);
  39:     if (Params["sortascending"].UserTypedIn)
  40:      Console.WriteLine("WARNING: parameter sortascending is incompatible with sortmethod {0}.  The parameter will be ignored.", sortMethod);
  41:    }
  43:    if (Params["inheritglobalnav"].UserTypedIn)
  44:     pubweb.InheritGlobalNavigation = bool.Parse(Params["inheritglobalnav"].Value);
  46:    if (Params["currentnav"].UserTypedIn)
  47:    {
  48:     CurrentNavSettingsEnum currentNav = (CurrentNavSettingsEnum)Enum.Parse(typeof (CurrentNavSettingsEnum), Params["currentnav"].Value, true);
  49:     if (currentNav == CurrentNavSettingsEnum.InheritParent)
  50:     {
  51:      pubweb.InheritCurrentNavigation = true;
  52:      pubweb.NavigationShowSiblings = false;
  53:     }
  54:     else if (currentNav == CurrentNavSettingsEnum.CurrentSiteAndSiblings)
  55:     {
  56:      pubweb.InheritCurrentNavigation = true;
  57:      pubweb.NavigationShowSiblings = true;
  58:     }
  59:     else if (currentNav == CurrentNavSettingsEnum.CurrentSiteOnly)
  60:     {
  61:      pubweb.InheritCurrentNavigation = false;
  62:      pubweb.NavigationShowSiblings = false;
  63:     }
  64:    }
  66:    pubweb.Update();
  67:   }
  68:  }
  70:  return 1;
  71: }
  73: internal enum CurrentNavSettingsEnum
  74: {
  75:  InheritParent,
  76:  CurrentSiteAndSiblings,
  77:  CurrentSiteOnly
  78: }

The syntax of the command can be seen below:

C:\>stsadm -help gl-setnavigationsettings

stsadm -o gl-setnavigationsettings

Sets the navigation settings for a web site (use gl-setnavigationnodes to change the actual nodes that appear).

        -url <site collection url>
        [-showsubsites <true | false>]
        [-showpages <true | false>]
        [-sortmethod <automatic | manualwithautomaticpagesorting | manual>]
        [-autosortmethod <title | createddate | lastmodifieddate>]
        [-sortascending <true | false>]
        [-inheritglobalnav <true | false>]
        [-currentnav <inheritparent | currentsiteandsiblings | currentsiteonly>]
Here's an example of how to set various settings on a given web:
stsadm -o gl-setnavigationsettings -url "http://intranet/sitedirectory" -showsubsites true -showpages true -sortmethod automatic -autosortmethod title -sortascending true -inheritglobalnav false -currentnav currentsiteandsiblings


Mauro Masucci said...

Hi, would you be willing to send me the dll you came up with for setting the navigation to inherit from the parent site?

Gary Lapointe said...

There's links under the Helpful Resources section in the top right to download the code and a WSP for all these commands.

Francois Pienaar said...


Thanks for the great extension. Just one small question, do you perhaps have a command / switch that would enable me to execute this on an entire site collection?

Thanks in advance,
Francois Pienaar

Gary Lapointe said...

Not at the moment but I am working on something to do it.

Amreesh Sharma said...

Hi All,
I am trying to delete all list items from a calendar in WSS. i wrote the command
D:\Documents and Settings\asharma>stsadm -0 gl-deletelistitem -url "http://amreesh:200/Lists/EventCalendar/calendar.aspx"
i am getting error: missing operation name or operation is invalid.
your ideas are welcome..

Gary Lapointe said...

Amreesh - it's "stsadm -o", not "stsadm -0"

Amreesh Sharma said...

i tried with -o also but didnt work. i didnt add the .wsp(solution file) to the solutions. is this the real problem.????

Gary Lapointe said...

Well of course you have to deploy the commands first :)

stsadm -o addsolution -filename "c:\Lapointe.SharePoint.STSADM.Commands.wsp"
stsadm -o deploysolution -local -allowgacdeployment -name "Lapointe.SharePoint.STSADM.Commands.wsp"
stsadm -o execadmsvcjobs

Amreesh Sharma said...

Thanks Gary......

Amreesh Sharma said...

Hi Gary,
I deployed Lapointe.SharePoint.STSADM.Commands.wsp on my sharePoint site. I could add solution and deploy it successfully on MOSS now i am running the following command to delete the list items of a Calandar list:
D:\Documents and Settings\asharma>stsadm -o gl-deletelistitem -url "http://shserver:401/LAPP2.0/Lists/Absences/Leave%20Planner.aspx"

But I am not able to.
Please give some hints if ia m making mistake anywhere...

Gary Lapointe said...

Are you getting any kind of error?

Amreesh Sharma said...

NO it doesnt give any error but directlty goes to all the stsadm custom commands by You. On WSS it is working fine but when i try on a remote machine whcih is having MOSS, i am not able to....Any Specific reason???

Gary Lapointe said...

Is this remote machine part of the farm? You can't use stsadm remotely.

Amreesh Sharma said...

Hi Gary,
It worked later on. I was accessing system remotely but i was writing the commands on the same farm. Actually i was not having proper permissions in the system. Later on it worked....


Anonymous said...

As everyone else says... Thanks for these great additions. I am trying to work a way to create over 70 subsites via a script. After numerous tries, I am using the createweb option generated from a previous template. The Top Links do not carry over though. I was hoping to use the gl-setnavsettings you created. It only lists treeview and quicklaunch as options though. Am I missing something?


Gary Lapointe said...

For the WSS version I only support those two options. You need the MOSS version for the other options.

Kaushal said...

Hi Gary,

I am using SharePoint 2007 on Win Server 2008.

I dont have gl-setnavigationsettings opeeration with stsadm.

Can you please help me to make it available as I am trying to execute the operation for few of the sites.

Thanks in Advance. BTW, it was a nice post.


Gary Lapointe said...

Go to the download page - download the WSP for your environment - follow the instructions for installing.