I have this problem. When I run this piece of code all I get is
1. {"Operation is not valid due to the current state of the object."}
2. Stack gets here " at Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext context)
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPFarm objSpfarm = SPWebService.AdministrationService.Farm;
SPWebApplicationBuilder webAppBld = null;
webAppBld = new SPWebApplicationBuilder(objSpfarm);
webAppBld.Port = 33665;
webAppBld.Id = System.Guid.NewGuid();
webAppBld.AllowAnonymousAccess = false;
webAppBld.UseNTLMExclusively = true;
webAppBld.UseSecureSocketsLayer = false;
webAppBld.ApplicationPoolId = "SharePointAppPoolId-33665";
webAppBld.ApplicationPoolUsername = @"testsvr02\testadmin";
// build the password as a secure string
SecureString appPoolPwd = new SecureString();
appPoolPwd.AppendChar('1');
appPoolPwd.AppendChar('2');
appPoolPwd.AppendChar('3');
appPoolPwd.AppendChar('W');
appPoolPwd.AppendChar('e');
appPoolPwd.AppendChar('l');
appPoolPwd.AppendChar('c');
appPoolPwd.AppendChar('o');
appPoolPwd.AppendChar('m');
appPoolPwd.AppendChar('e');
// more of the same to build-up the password
appPoolPwd.MakeReadOnly();
webAppBld.ApplicationPoolPassword = appPoolPwd;
webAppBld.CreateNewDatabase = true;
webAppBld.DatabaseServer = @"TESTSVR02\OfficeServers";
webAppBld.DatabaseName = "TestSite";
webAppBld.DatabaseUsername = @"testsvr02\testadmin";
webAppBld.DatabasePassword = "123Welcome";
webAppBld.DefaultZoneUri = new Uri("http://TESTSVR02:33665/");
webAppBld.RootDirectory = new DirectoryInfo(@"C:\Inetpub\wwwroot\wss\VirtualDirectories\33665");
SPWebApplication webApp = webAppBld.Create();
});
It fails even though the above technique is used. Any ideas ?