Alvin, you pretty much provided most of the options in your question. Another option, if a special error message is required, or an even more subtle level of control, is to use an event system. Subscribe to the page save event. Initiated phase and write some verification code that throws an exception if there is an unwanted presentation of the component on the page.
Since the page types are really a combination of the page template, any metadata on the page and the types of presentation of the components on the page, we will need to check that we are dealing with the desired page type and if we meet a CP that does not match the desired one, we can just throw an exception . Here is a little code:
[TcmExtension("Page Save Events")] public class PageSaveEvents : TcmExtension { public PageSaveEvents() { EventSystem.Subscribe<Page, SaveEventArgs>(ValidateAllowedContentTypes, EventPhases.Initiated); } public void ValidateAllowedContentTypes(Page p, SaveEventArgs args, EventPhases phases) { if (p.PageTemplate.Title != "My allowed page template" && p.MetadataSchema.Title != "My allowed page metadata schema") { if (!ValidateAllowedContentTypes(p)) { throw new Exception("Content Type not allowed on a page of this type."); } } } private bool ValidateAllowedContentTypes(Page p) { string ALLOWED_SCHEMAS = "My Allowed Schema A; My Allowed Schema B; My Allowed Schema C; etc";
source share