Today I had a requirement to add an additional property to over 200 MOSS 2007 publishing pages. Since there were over 200 pages I decided to do this in code rather than editing each page manually as this would be much faster and much less dull.
First off I had to add the property to the content type for the page. In this case it was a development code for a group of housing developments, so I added this as a string value and called it DevCode. I then had to think about how I was going to do the update...
Each publishing page obviously has a URL, so I asked the client to provide me with an Excel spreadsheet containing 2 columns matching up the URL for each housing development with the 3 digit code for the development. I then imported the Excel spreadsheet into a list in a SharePoint configuration site that I created earlier.
My plan was to now write a simple .Net windows application that could be run on the web front end server itself. Here is a brief synopsis of what the code does:
Upon clicking a button, the code will create a publishing site instance in order to get the collection of publishing pages. Once that is done we get the list containing the new codes with URLs from the configuration site, the plan being to loop round this list using the URL to find the page we're after in the publishing page collection.
Once the page is dug out of the page collection we need to find the DevCode field that we want to update, set the value to the new DevCode and perform the update on the item. Here is the complete code, C# as ever:
using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using System.Configuration;
string url = string.Empty;
string devCode = string.Empty;
// open a web site
string pubSiteRoot = ConfigurationManager.AppSettings["PublishingSiteRoot"];
SPSite pubSite = new SPSite(pubSiteRoot);
pubSite.AllowUnsafeUpdates = true;
using (SPWeb pubWebSite = pubSite.OpenWeb())
{
if (PublishingWeb.IsPublishingWeb(pubWebSite))
{
// get the publishing web instance for this site
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(pubWebSite);
// get the collection of publishing pages
PublishingPageCollection ppages = publishingWeb.GetPublishingPages();
string cfgSiteRoot = ConfigurationManager.AppSettings["ConfigSiteRoot"];
SPSite cfgSite = new SPSite(cfgSiteRoot);
using (SPWeb cfgWebSite = cfgSite.OpenWeb())
{
//loop round the list items in the devcodes list
SPList liDevCodes = cfgWebSite.Lists["DevCodes"];
for (int i = 0; i < liDevCodes.ItemCount; i++)
{
url = "Pages/";
SPListItem item = liDevCodes.Items[i];
SPField fldUrl = item.Fields.GetFieldByInternalName("ows_Title");
SPField fldNewDevCode = item.Fields.GetFieldByInternalName("ows_DevelopmentCode");
url = url + item[fldUrl.Id].ToString().ToLower() + ".aspx";
devCode = item[fldNewDevCode.Id].ToString();
PublishingPage thePage = ppages[url];
if (thePage != null)
{
SPListItem theItem = thePage.ListItem;
SPField fldDevCode = theItem.Fields.GetFieldByInternalName("DevCode");
thePage.CheckOut();
theItem[fldDevCode.Id] = devCode;
// update the page, check in and publish
theItem.Update();
thePage.CheckIn("");
thePage.ListItem.File.Publish("");
thePage.ListItem.File.Approve("");
pubWebSite.Update();
}
url = string.Empty;
thePage = null;
}
}
}
}