Associate (dis-associate) page-types and templates with a given site - programmatically

Sergiu Filip asked on December 15, 2014 17:35

I tried an educated guess, browsing to the barely documented APIs , bu the following fragment doesn't see to do it (for a doc type);

SiteInfo siteinfo  = SiteInfoProvider.GetSiteInfo(siteId);
DocumentTypeInfo docInfo =  (DocumentTypeInfo)DataClassInfoProvider.GetDataClassInfo("Some.PageType");
docInfo.AssignedSites.Add(siteinfo);

Nor this one, for a template:

var templateInfo = PageTemplateInfoProvider.GetPageTemplateInfo("Some.Template");
templateInfo.AssignedSites.Add(siteinfo);

Correct Answer

Martin Danko answered on December 16, 2014 13:54

Hello Sergiu,

I would also recommend you to take a look at the API Examples application, where you can find this example:

 private bool AddPageTemplateToSite()
{
    // Get the page template
    PageTemplateInfo template = PageTemplateInfoProvider.GetPageTemplateInfo("MyNewTemplate");
    if (template != null)
    {
        int templateId = template.PageTemplateId;
        int siteId = SiteContext.CurrentSiteID;

        // Save the binding
        PageTemplateSiteInfoProvider.AddPageTemplateToSite(templateId, siteId);

        return true;
    }

    return false;
}

Best regards, Martin

2 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on December 15, 2014 20:11

Try this out once, look at the DataClassInfo object vs. DocumentTypeInfo. It might look something like this:

CMS.DataEngine.DataClassInfo dci = CMS.DataEngine.DataClassInfoProvider.GetDataClassInfo("className");
dci.AssignedSites.Add(siteInfo);
CMS.DataEngine.DataClassInfoProvider.SetDataClassInfo(dci);
1 votesVote for this answer Mark as a Correct answer

Sergiu Filip answered on December 23, 2014 18:02

Thank you Martin & Brenden.

The approach for adding the template to a site works. The one for a document-type, unfortunately, does not. I.E. the following fragment will not save the mapping:

DataClassInfo docInfo = DataClassInfoProvider.GetDataClassInfo("MyDocType");
if (bAdd)
    docInfo.AssignedSites.Add(siteinfo);
else
    docInfo.AssignedSites.Remove(siteinfo);

DataClassInfoProvider.SetDataClassInfo(docInfo);
0 votesVote for this answer Mark as a Correct answer

Martin Danko answered on December 29, 2014 15:01

Hi Sergiu,

The first thing is that you are trying to do it without Kentico API, you should always use Kentico API when you are working with application's objects. Second thing that I want to note is that I would recommend you to try to inspect or even debug what is happening when you do that via UI, so you can see what code is handling this functionality.

So take a look at the following file:

\CMSModules\AdminControls\Controls\Class\ClassSites.ascx.cs

and around line 150 you can find the code responsible for adding sites to page type (classes are used).

Best regards, Martin

0 votesVote for this answer Mark as a Correct answer

Ben Stoke answered on October 30, 2017 07:57

The approach for adding the template to a site works. The one for a document-type, unfortunately, does not. I.E. the following fragment will not save the mapping. 70-357 dumps

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.