6

Got the following call:

var templatePath = "/User Defined/Project/Common/Content Types/Links";
var template = TemplateManager.GetTemplate(templatePath, Context.Database);

This is always returning null. I have checked the template path and can confirm 100% it is correct, the database is also present. This issue is happening in 8.2 any ideas on what I might be missing?

Immediate Window Debug Information

Context.Database
{web}
    Aliases: {Sitecore.Data.AliasResolver}
    ArchiveNames: Count = 2
    Archives: {Sitecore.Data.Archiving.DataArchives}
    Branches: {Sitecore.Data.BranchRecords}
    Caches: {Sitecore.Data.DatabaseCaches}
    ConnectionStringName: "web"
    DataManager: {Sitecore.Data.DataManager}
    DataProviders: Count = 1
    Engines: {Sitecore.Data.DatabaseEngines}
    HasContentItem: true
    Icon: "Images/database_web.png"
    Items: {Sitecore.Data.ItemRecords}
    Languages: {Sitecore.Globalization.Language[1]}
    Masters: {Sitecore.Data.BranchRecords}
    Name: "web"
    NotificationProvider: null
    Properties: {Sitecore.Data.DatabaseProperties}
    Protected: false
    ProxiesEnabled: false
    ProxyDataProvider: {Sitecore.Data.SqlServer.SqlServerProxyDataProvider}
    PublishVirtualItems: true
    ReadOnly: false
    RemoteEvents: {Sitecore.Data.Eventing.DatabaseRemoteEvents}
    Resources: {Sitecore.Resources.ResourceItems}
    SecurityEnabled: true
    "sitecore": sitecore (en#1@web), id: {11111111-1111-1111-1111-111111111111}
    Templates: {Sitecore.Data.TemplateRecords}
    WorkflowProvider: null

TemplateManager.GetTemplate("/sitecore/Templates/User Defined/Project/Common/Content Types/Icon Links", Context.Database)
null
TemplateManager.GetTemplate("/sitecore/templates/User Defined/Project/Common/Content Types/Icon Links", Context.Database)
null
TemplateManager.GetTemplate("User Defined/Project/Common/Content Types/Icon Links", Context.Database)
null

Snip of item path, on web database.

enter image description here

Update

TemplateManager.GetTemplate("/sitecore/templates/System/Templates/Standard template", Context.Database)
null

Using immediate window to try and get standard template also fails.

4
  • You probably have checked but is the template published? Are there any publishing restrictions? Commented Oct 7, 2016 at 11:55
  • Also worth trying to get a standard template to verify that your code is correct Commented Oct 7, 2016 at 11:58
  • @MichaelEdwards yes 100% the template is published.
    – Anicho
    Commented Oct 7, 2016 at 12:56
  • Absolute paths will fail. Consistently.
    – Mark Cassidy
    Commented Oct 7, 2016 at 15:33

1 Answer 1

11

Yea. Change your code to this:

var templatePath = "User Defined/Project/Common/Content Types/Links";
var template = TemplateManager.GetTemplate(templatePath, Context.Database);

If your path starts with "/", Sitecore will assume it to be absolute.

6
  • 1
    Also worth noting, that the templatePath parameter is case sensitive.
    – Anicho
    Commented Oct 7, 2016 at 11:23
  • 1
    "User Defined/Project/Common/Content Types/Icon Links" doesn't work however "sitecore/Templates/User Defined/Project/Common/Content Types/Icon Links" does. Sad Times. Sorry tested both before but when testing relative path might of forgot to publish solution :P.
    – Anicho
    Commented Oct 7, 2016 at 11:28
  • I can't reproduce. On a vanilla 8.2u0, relative path resolution always succeeds.
    – Mark Cassidy
    Commented Oct 7, 2016 at 12:07
  • 1
    I will check over the configurations and dlls. They upgraded to 8.2 not sure how clean the upgrade was now.
    – Anicho
    Commented Oct 7, 2016 at 12:56
  • Yeah it was just an out of date kernel binary sigh! What a waste of time! Part of the problem the right binary is referenced in solution, wrong one deployed :/.
    – Anicho
    Commented Oct 7, 2016 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.