Wednesday, December 5, 2007

Updating Custom Site Columns Feature for an existing Site

After updating and deploying your custom site column feature, simply deactivate and activate the custom feature on each existing site in order to get the new site column(s) to show up in the existing site. 

If you have a lot of sites or your site column feature is hidden you most probably want to do this programmatically.  The following code example will update an existing site with the new site column(s).

                //open site
SPSite site = new SPSite("http://myCompany/sites/CompanyStuff/");

try
{
//set the guid of the feature to be toggled
Guid g = new Guid("4055557A-9AB2-43b4-83EB-3B55555555EA");

//fetch and toggle
SPFeatureCollection featureCollect = site.Features;
//reomve the custom site column feature
featureCollect.Remove(g);
//add the custom site feature back and activate it
featureCollect.Add(g);
}
catch (Exception ex)
{
throw new Exception("Something went wrong: " + ex.Message);
}
finally
{
site.Close();
site.Dispose();
}

No comments: