Wednesday 16 May 2012

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


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();

http://blogs.msdn.com/b/malag/archive/2010/04/14/how-to-create-new-application-pool-and-change-app-pool-of-sharepoint-website.aspx

No comments:

Post a Comment