After I had created the gl-sitewelcomepage
command I needed a quick way to find all the pages that were pointing to the wrong place. To do this I hacked out this command which essentially just loops through a farm, web application, site collection or web and displays what the current welcome page is set to for each web. The command is called gl-enumwelcomepages
. Like the gl-sitewelcomepage
command this command is really simple – most of the code is focused on looping through the various objects to get to the PublishingWeb
object:
1/// <summary>
2/// Runs the specified command.
3/// </summary>
4/// <param name="command">The command.</param>
5/// <param name="keyValues">The key values.</param>
6/// <param name="output">The output.</param>
7/// <returns></returns>
8public override int Run(string command, StringDictionary keyValues, out string output)
9{
10 output = string.Empty;
11
12 InitParameters(keyValues);
13
14 string url = Params["url"].Value;
15 if (url != null)
16 url = url.TrimEnd('/');
17
18 string scope = Params["scope"].Value.ToLowerInvariant();
19
20 if (scope == "farm")
21 {
22 foreach (SPService svc in SPFarm.Local.Services)
23 {
24 if (!(svc is SPWebService))
25 continue;
26
27 foreach (SPWebApplication webApp in ((SPWebService)svc).WebApplications)
28 {
29 DisplayWelcomePageUrl(webApp);
30 }
31 }
32 }
33 else if (scope == "webapplication")
34 {
35 SPWebApplication webApp = SPWebApplication.Lookup(new Uri(url));
36 DisplayWelcomePageUrl(webApp);
37 }
38 else if (scope == "site")
39 {
40 using (SPSite site = new SPSite(url))
41 {
42 DisplayWelcomePageUrl(site);
43 }
44 }
45 else if (scope == "web")
46 {
47 using (SPSite site = new SPSite(url))
48 using (SPWeb web = site.AllWebs[Utilities.GetServerRelUrlFromFullUrl(url)])
49 {
50 DisplayWelcomePageUrl(site, web, true);
51 }
52 }
53
54 return 1;
55}
56
57
58/// <summary>
59/// Validates the specified key values.
60/// </summary>
61/// <param name="keyValues">The key values.</param>
62public override void Validate(StringDictionary keyValues)
63{
64 if (Params["scope"].UserTypedIn)
65 {
66 if (Params["scope"].Value.ToLowerInvariant() == "farm" && Params["url"].UserTypedIn)
67 throw new SPSyntaxException("The url parameter is not compatible with a scope of Farm.");
68 if (Params["scope"].Value.ToLowerInvariant() != "farm" && !Params["url"].UserTypedIn)
69 throw new SPSyntaxException("The url parameter is required if the scope is not Farm.");
70 }
71 base.Validate(keyValues);
72}
73
74#endregion
75
76
77/// <summary>
78/// Displays the welcome page URL.
79/// </summary>
80/// <param name="webApp">The web app.</param>
81private static void DisplayWelcomePageUrl(SPWebApplication webApp)
82{
83 foreach (SPSite site in webApp.Sites)
84 {
85 try
86 {
87 DisplayWelcomePageUrl(site);
88 }
89 finally
90 {
91 site.Dispose();
92 }
93 }
94}
95
96/// <summary>
97/// Displays the welcome page URL.
98/// </summary>
99/// <param name="site">The site.</param>
100private static void DisplayWelcomePageUrl(SPSite site)
101{
102 foreach (SPWeb web in site.AllWebs)
103 {
104 try
105 {
106 DisplayWelcomePageUrl(site, web, false);
107 }
108 finally
109 {
110 web.Dispose();
111 }
112 }
113}
114
115/// <summary>
116/// Displays the welcome page URL.
117/// </summary>
118/// <param name="site">The site.</param>
119/// <param name="web">The web.</param>
120/// <param name="recurseSubWebs">if set to <c>true</c> [recurse sub webs].</param>
121internal static void DisplayWelcomePageUrl(SPSite site, SPWeb web, bool recurseSubWebs)
122{
123 if (recurseSubWebs)
124 {
125 foreach (SPWeb subweb in web.Webs)
126 {
127 DisplayWelcomePageUrl(site, subweb, recurseSubWebs);
128 }
129 }
130
131 if (!PublishingWeb.IsPublishingWeb(web))
132 return;
133
134 PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(web);
135
136 Console.WriteLine(site.MakeFullUrl(web.ServerRelativeUrl) + " = " + pubWeb.DefaultPage);
137}
The syntax of the command can be seen below:
C:\>stsadm -help gl-enumwelcomepages
stsadm -o gl-enumwelcomepages
Lists all the welcome page(s) for a farm, web application, site collection, or web.
Parameters:
-url <url to search>
-scope <Farm | WebApplication | Site | Web>
Here’s an example of how to list all the welcome pages in a web application:
stsadm -o gl-enumwelcomepages -url "http://intranet" -scope webapplication