Wednesday, August 1, 2007

MOSS 2007 - Enable treeviews on all sites programatically

I just added a MOSS utility (to my growing list of MOSS utilities) that would allow me to enable treeviews on all sites programmatically, rather than going into each site doing it manually. This is significant time savings when you are doing a migration - because there are potentially hundreds of sites that already exist and treeview is not enabled on them.

Here is the code. This assumes that you run a stsadm -o enumsites on the server so that you can get the list of the site collections on your server. This also assumes that you are reading that list from a config file (because you might want to run this for a subset of your site collections).

public static void EnableTreeView() {

Console.WriteLine("Enabling treeviews");

try {

NameValueCollection coll = (NameValueCollection)ConfigurationManager.GetSection("FarmSettings/SiteCollections");

string[] sitecolls = coll.AllKeys;

foreach (string sitecollstr in sitecolls) {

SPSite sitecoll = new SPSite(sitecollstr);

SPWebCollection webcols = sitecoll.AllWebs;

foreach (SPWeb web in webcols) {

Console.WriteLine("Enabling treeview for site: " + web.Url);

web.TreeViewEnabled = true;
web.Update();

web.Dispose();

}

}

}

catch (Exception ex)

{

Console.WriteLine(ex.Message);

}

}


If you have too many sites, you can pipe the output to a file for readibility.

[consoleapp.exe] > C:/treeviewSites.txt




2 comments:

Anonymous said...

This is cool, I just used it thanks.

-Joe

digital certificate said...

Grateful to check out your website, I seem to be ahead to more excellent sites and I wish that you wrote more informative post for us. Well done work.