Hi All,
if you want to add a WebApplication programmatically with the Object Model of SharePoint, this Code-Snippet might be helpful:
Create a new Project in VS2005
Add a Reference to Microsoft.SharePoint.dll
Add the usings:
using System.IO;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
In the Declaration-Header build Variables for a SPFarm-Object and a SPWebApplicationBuilder-Object and a SPWebApplication -Object:
SPFarm Farm = SPWebService.AdministrationService.Farm;
SPWebApplicationBuilder builder;
SPWebApplication app = null;
builder = new SPWebApplicationBuilder(Farm);
Set the Port and the RootDirectory where you want to install the Application, e.g:
builder.Port = 24521;
builder.RootDirectory = new DirectoryInfo(@"C:\Inetpub\wwwroot\wss\VirtualDirectories\24521";)
Set the ServerComment for the Application which will be the Name of the Application in the SharePoint-List And IIS. If you do not set this Property, the Name of the Application will be 'SharePoint - <Default given Portnumber from System>'
builder.ServerComment = MyApplicationname+ " - " + port;
Create also the Contentdatabase for this Application:
builder.CreateNewDatabase = true;
builder.DatabaseName = "WSS_Content_MyApplication_24521";
Some additionall Settings:
builder.UseNTLMExclusively = true;
builder.AllowAnonymousAccess = false;
builder.UseSecureSocketsLayer = false;
Let the builder create the Application:
app = builder.Create();
Set the TimeZone of the Application, e.g.
app.DefaultTimeZone = SPRegionalSettings.GlobalTimeZones[29].ID;
Yes, set the Name here again:
app.Name = MyApplicationname+ " - " + port;
app.Update();
app.Provision();
Set the Properties for the Application-Pool of this Application, e.g.
app.ApplicationPool.CurrentIdentityType = IdentityType.SpecificUser;
app.ApplicationPool.Username = MyDomain\MyUsername;
app.ApplicationPool.Password = MyPassword;
app.ApplicationPool.Update();
app.ApplicationPool.Provision();
Upload the newly created WebApplication to the List 'Web Application List' in Central Administration:
SPWebService.AdministrationService.WebApplications.Add(app);
Do a iisreset /noforce and everything should work.
If you have any doubts let me know...
All the best, Ronny

How to add a WebApplication with WSS 3.0
Caleb T
I tried to add the WebApplication on remote server.
For this purpose I replaced line
SPFarm Farm = SPWebService.AdministrationService.Farm;SPFarm Farm = SPWebService.AdministrationService.Farm;
to
SPFarm farm = SPFarm.Open("Data Source=1.2.3.4;Initial Catalog=Winigors_Config;User Id=sa;Password=mypwd");
SPWebService webService = farm.Services.GetValue<SPWebService>();
But I received this error
---------------------------------------------------------------------------------------------------------------------------------------
Unhandled Exception: System.Security.Principal.IdentityNotMappedException: Some or all identity references could not be translated.
at System.Security.Principal.NTAccount.Translate(IdentityReferenceCollectionsourceAccounts, Type targetType, Boolean forceSuccess)
at System.Security.Principal.NTAccount.Translate(Type targetType)
at Microsoft.SharePoint.Administration.SPFarm.CurrentUserIsAdministrator()
at Microsoft.SharePoint.Administration.SPPersistedObject.Update()
at Microsoft.SharePoint.Administration.SPEncryptedString.UpdateInternal()
at Microsoft.SharePoint.Administration.SPProcessIdentity.Update()
at Microsoft.SharePoint.Administration.SPApplicationPool.Update()
at Microsoft.SharePoint.Administration.SPWebApplication.CreateDefaultInstance(SPWebService service, Guid id, String applicationPoolId, IdentityType identityT
ype, String applicationPoolUsername, SecureString applicationPoolPassword, String iisServerComment, Boolean secureSocketsLayer, String iisHostHeader, Int32 iisP
ort, Boolean iisAllowAnonymous, DirectoryInfo iisRootDirectory, Uri defaultZoneUri, Boolean iisEnsureNTLM, Boolean createDatabase, String databaseServer, String databaseName, String databaseUsername, String databasePassword, SPSearchService
Instance searchServiceInstance, Boolean isPaired, Boolean autoActivateFeatures)
at Microsoft.SharePoint.Administration.SPWebApplicationBuilder.Create()
---------------------------------------------------------------------------------------------------------------------------------------
What am I doing wrongLocally this code work good.
Regards,
Alexey Rokhin
Hi, Ronny.
Thank you very much for your code sample.
It works well.
But after executing it I received new Web Application created with default Application Pool
To fix this I did this change:
before
builder.Create
line I added
builder.ApplicationPoolId = myAppName
;builder.IdentityType =
IdentityType.NetworkService;Regards,