Custom site definition – Using GetAvailableWebTeplates / GetWebTemplates


In SharePoint object model when the following sequence of steps happen, the deployed custom site definition will not be found.


Step 1 : Add Solution ( The solution package consisting of site templates and features )

Step 2 : Create Web application.

Step 3 : Deploy solution. ( The solution is deployed and the dlls are deployed in the local bin folder of the newly created web application)

Step 4 : Create Site Collection( Create new site collection based on the custom site definition that was deployed through the solution earlier).


·           All the first three steps works fine. When the Step 4 ( Create Site Collection ) is called, the application doesn't find the site definition of the newly deployed site definiton. When we stop and re-run the application and just call the fourth Step ( create site collection ) alone, it finds the custom site definition. In the subsequent calls it will be able to get the site definition. Only in the first call, it won’t find the site definition. 


·           When the solution is added and deployed in UI it works fine.


·           Even when the solution is added and deployed using STSAdm command it works fine.  Only when the solution is added and deployed using object model the problem occurs. 



The site templates are actually cached when a new web application is created.  If a new web application is created and then custom site definitions are deployed, the web application is not going to find out the site definitions in the code if it runs in the same process.  In the UI, internally, IIS reset happens and that’s why the deployed site definitions are seen.  In our windows application, IIS reset will not work but we have to reset the process itself.  If we reset the process, it works fine.


Possible workarounds :


·           Call all the first three Steps on a fly. Then stop and re-run the application and call only the fourth Step (create site collection). This works fine as the process is restarted and now the web application finds the deployed custom site definition.


·           Create a new console application and have code for creating site collection with the template of custom site definiton. Compile and have the executable file. Call all the four steps on a fly. In the fourth Step (create site collection), load the executable file in a different process. It works fine as it is called in a different process all together.


 ·          Use STSAdm command in the code itself.  In this, the web application will be able to get the newly deployed custom site definition.


·           Add and deploy the solution either through UI of SharePoint or through STSAdm command in command prompt.


The site templates are actually cached.  So, when we call GetAvailabeTemplates of SPWeb or GetWebTemplates of SPSite, it doesn’t actually look into the physical files but rather it looks in the cache.  So, deploying the custom site definition after creating web application won’t get us the deployed site templates. So, we need to deploy   custom site definitions  before creating web application.  In this case, when the web application is created, it is aware of the custom site definition and it will get us the template.


Skip to main content