Tuesday, March 31, 2009

Renaming the URL of a web application

Sometimes a company might want to change the URL of a sharepoint web application for branding or some other purpose. This change sounds a little challenging but it is not very complicated.

Following steps need to be followed:
1. In Central Administration, go to Alternate Access Mappings (AAM) under the Operations.
2. Select the Web Application from the Alternate Access Mapping Collection for which you want to change the URL.



3. Edit the public URL for this mapping collection to the new URL



Yes, thats it! Not really :-) ... any changes done in AAM do not get reflected in IIS. The host header for the web application need to be modified by opening the IIS manager. Here are the steps:
1. Open IIS Manager and go to the Web Site for which the URL is being changed.
2. Right-click and click on Properties.
3. Under the Wb Site tab, click on Advanced button next to the IP Address.
4. Change the host header to the new URL value.
5. The above steps need to be repeated for each server in the SharePoint web farm. :-(
Yes, That's it. The URL is changed. :-)
But wait, there are a few caveats:
1. Many times, the CEWP and image web parts have absolute links. SharePoint even changes relative links to absolute links. So, these links will still be pointing to the old URL. They will have to be updated. I wrote a small script to do it. Here is a snippet of the code:

mgr = oWeb.GetLimitedWebPartManager(page.Uri.ToString(), System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
parts = GetCEParts(mgr.WebParts);
foreach (ContentEditorWebPart wp in parts)
{
try
{
if (wp.Content.OuterXml.ToLower().Contains(urlToRemove.ToLower()))
{
if (page.ListItem.File.CheckOutStatus == SPFile.SPCheckOutStatus.None)
{
page.CheckOut();
}
swLog.WriteLine("Hard Coded path found in " + page.Uri);
XmlDocument doc = new XmlDocument();
string outerXML = wp.Content.OuterXml.Replace(" ", "");
outerXML = StringHelper.ReplaceText(outerXML, urlToRemove, urlReplace, StringHelper.CompareMethods.Text);
doc.LoadXml(outerXML);
wp.Content = doc.DocumentElement;
mgr.SaveChanges(wp);
page.Update();
page.CheckIn();
}
}
catch (Exception ex)
{
swErrorLog.WriteLine("Errors updating CEWP for: " + page.Uri.ToString() + ". Error was: " + ex.Message);
}
}
#region GetCEParts
private static List GetCEParts(SPLimitedWebPartCollection list)
{
List parts = new List();
foreach (System.Web.UI.WebControls.WebParts.WebPart item in list)
{
if (item.GetType() == typeof(ContentEditorWebPart) item.GetType().BaseType == typeof(ContentEditorWebPart))
parts.Add((ContentEditorWebPart)item);
}
return parts;
}
#endregion


This code would need to be run for all pages having CEWPs. Code also needs to be run to update image URLs for image web parts.
2. Sometimes, you will start getting "Value does not fall within the expected range" when going to Page Settings for a page. Hopefully, you do not need to go to the Page Settings of a lot of pages as resolving this is a manual process and takes time! Here is a great blog to resolve this issue: http://www.bronios.com/index.php/2008/01/18/moss-page-setting-error-value-does-not-fall-within-the-expected-range/.