SharePoint 2010 loading templates from subfolders

If you are developing a custom fieldtype and therefore building the rendering template, the .ascx file, you normally deploy it to the template/controltemplates/ folder. But what if you want to deploy your files into custom subfolders which you can name like the project they belong to. This is the code which calls the Controls from your template normally:

myLabel = (Label)TemplateContainer.FindControl(“myLabel”);

But if you deploy them into a subfolder, the normal code does not find these controls in subfolders. So i found this nice article. Erik Burger wrotes how to achieve it. You have to use this code:

// Initialize template container
templateContainer = new TemplateContainer();
Control myControl =  (Control)Page.LoadControl("~/_controltemplates/MyProjects/myControlRenderingTemplate.ascx" );
templateContainer.Template = ((RenderingTemplate)myControl.Controls[0]).Template;
// Add the container
//Now you find the your label by using this:
myLabel = (Label)templateContainer.FindControl("myLabel");

Normally SharePoint should also look into the subdirectories, but for some reasons it does not work, so maybe this helps you, too.

My posts about custom fieldtypes:


The article or information provided here represents completely my own personal view & thought. It is recommended to test the content or scripts of the site in the lab, before making use in the production environment & use it completely at your own risk. The articles, scripts, suggestions or tricks published on the site are provided AS-IS with no warranties or guarantees and confers no rights.

Karsten Schneider
About Karsten Schneider 301 Articles
He is Consultant for SharePoint Solutions for collaboration, communication and business processes. He provides his customers applications based on standard features as well as development or combining the power of several microsoft tools like Dynamics CRM. In his free time he tries to collect tipps and worthy experience in this blog.

Be the first to comment

Leave a Reply

Your email address will not be published.