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:
1public override int Run(string command, System.Collections.Specialized.StringDictionary keyValues, out string output)
2{
3 output = string.Empty;
4
5 InitParameters(keyValues);
6
7 string url = Params["url"].Value.TrimEnd('/');
8
9 using (SPSite site = new SPSite(url))
10 {
11 using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(url)])
12 {
13 PublishingWeb pubweb = PublishingWeb.GetPublishingWeb(web);
14
15 if (Params["showsubsites"].UserTypedIn)
16 pubweb.IncludeSubSitesInNavigation = bool.Parse(Params["showsubsites"].Value);
17
18 if (Params["showpages"].UserTypedIn)
19 pubweb.IncludePagesInNavigation = bool.Parse(Params["showpages"].Value);
20
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 }
27
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 }
42
43 if (Params["inheritglobalnav"].UserTypedIn)
44 pubweb.InheritGlobalNavigation = bool.Parse(Params["inheritglobalnav"].Value);
45
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 }
65
66 pubweb.Update();
67 }
68 }
69
70 return 1;
71}
72
73internal 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).
Parameters:
-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