Friday, June 5, 2009

SharePoint: Code for extending a web app

This doesn't claim to be the code to extend a web app, but more like a boilerplate that you can adapt to your own needs. I tried it with a generous matrix of parameters and works correctly, but I have no doubt that some combinations may cause issues.

        private static void ExtendWebApplication(SPWebApplication webapp, string name, int port, SPUrlZone zone, bool secure, bool useWindowsAuth, bool allowAnonymous, bool useClaims)
{
if (webapp == null)
{
throw (new ArgumentNullException("webapp"));
}

SPServerBinding serverBinding = null;
SPSecureBinding secureBinding = null;
if (secure)
{
secureBinding = new SPSecureBinding();
secureBinding.Port = port;
}
else
{
serverBinding = new SPServerBinding();
serverBinding.Port = port;
}

string extendedAppPath = webapp.IisSettings[SPUrlZone.Default].Path.ToString().Substring(0, webapp.IisSettings[SPUrlZone.Default].Path.ToString().LastIndexOf(@"\") + 1) + port.ToString();
SPIisSettings iisSettings = new SPIisSettings(
string.Format(CultureInfo.InvariantCulture, "{0} - {1}", name, port),
false,
true,
serverBinding,
secureBinding,
new DirectoryInfo(extendedAppPath));
iisSettings.DisableKerberos = !useWindowsAuth;
iisSettings.UseWindowsIntegratedAuthentication = useWindowsAuth;
iisSettings.AllowAnonymous = allowAnonymous;
webapp.IisSettings.Add(zone, iisSettings);
webapp.AlternateUrls.SetResponseUrl(new SPAlternateUrl(GetAlternateUrl(port, secure), zone));
webapp.AlternateUrls.Update();
webapp.UseClaimsAuthentication = useClaims;
webapp.Update();
webapp.ProvisionGlobally();
}

No comments: