How to Create new application pool and change App Pool of SharePoint Website

In OOTB SharePoint, you do not have anything which will allow you to create new application pools in IIS and assign them to applications. Also nothing was found in STSADM.

If the application pool does not exist you can create it using the following code snippet:

    1:  SPFarm farm = SPFarm.Local; 
    2:  SPWebService service = farm.Services.GetValue<SPWebService>(); 
    3:  SPApplicationPool appPool = new SPApplicationPool("App Pool Title", service); 
    4:  appPool.CurrentIdentityType = IdentityType.SpecificUser; 
    5:  appPool.Username = "domain\\username"; 
    6:  appPool.Password = "password"; 
    7:  appPool.Update(); 
    8:  appPool.Deploy();

Be aware that application pools you created in IIS directly cannot be used with the SharePoint object model. The reason is that these are not known in the Config DB and they could also be with different configuration on different WFE's in the farm.

So you have to ensure that the application pool has been created using the method above. Once the application pool is created or it already exists you can look it up using the following code snippet and assign to the SharePoint application:

    1:  SPFarm farm = SPFarm.Local;
    2:  SPWebService service = farm.Services.GetValue<SPWebService>();
    3:  SPApplicationPool appPool = service.ApplicationPools["App Pool Title"];
    4:  
    5:  SPSite site = new SPSite("http://url-to-your-sitecollection");
    6:  SPWebApplication webApp = site.WebApplication;
    7:  webApp.ApplicationPool = appPool;
    8:  webApp.Update();
    9:  webApp.ProvisionGlobally();

Hope this helps you to elevate some issues… Happy Coding…Smile