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:

[sourcecode language=”csharp”]

// 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
Controls.Add(templateContainer);
//Now you find the your label by using this:
myLabel = (Label)templateContainer.FindControl(“myLabel”);

[/sourcecode]

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:

..:: I LIKE SHAREPOINT ::..

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.

About Karsten Schneider 312 Articles
Consultant for Microsoft 365 Applications with a strong focus in Teams, SharePoint Online, OneDrive for Business as well as PowerPlatform with PowerApps, Flow and PowerBI. I provide Workshops for Governance & Security in Office 365 and Development of Solutions in the area of Collaboration and Teamwork based on Microsoft 365 and Azure Cloud Solutions. 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.


*