Dynamically associate pages with a layer rule.

Topics: Writing modules
Jan 27, 2014 at 8:32 AM
Hi,
I am creating thousands of pages on run time through the source code. Now I want to create a layer dynamically and associate those pages to the layer through layer rule.

Example : say I have 3 pages page1,page2 and page3 with their Ids 1,2 and 3 respectively. Now I want to create a layer "TestLayer" with layer rule something like url("~/1") or url("~/2") or url("~/3").

What is best way to achieve this. Some code examples will be highly appreciated.
Jan 27, 2014 at 9:48 AM
Check LayerHintHandler.cs in Widgets module. It displays a notification when you create a page in admin dashboard to create a layer for a single page. The layer is actually created in AddLayerPOST method of AdminController of the module.

Jan 27, 2014 at 10:49 AM
Edited Jan 27, 2014 at 10:54 AM
In this case it enforces to create a layer for every page I am creating. I need a single layer rule for all the pages say 500 pages based on their Ids.

Here is the demo code for creating pages...
for (int i = 0; i < 500; i++)
        {
            var page = _contentManager.Create("Page");
            page.As<TitlePart>().Title = "Sample Page" + i.ToString();
           _contentManager.Publish(page);  
        }
Now I want to create a layer "TestLayer" with layer rule which contains all the pages created in above code.

I hope things must be better clear now.
Jan 27, 2014 at 11:11 AM
Yes, this is the example code you were looking for. You will need to build the layer rule you need.

An idea, you might want to set an autoroute alias to the pages you want to share a layer, something like /pagegroup<some identifier>/page<id>, and make the layer rule with url('/pagegroupX/*') instead of building a layer rule with lots of ORs. Probably it will be way faster.




Jan 27, 2014 at 12:23 PM
Thanks a ton for the reply. I got the idea but it would really be great if you can give some code snippet for the same.

for (int i = 0; i < 500; i++)
    {
        var page = _contentManager.Create("Page");
        page.As<TitlePart>().Title = "Sample Page" + i.ToString();

        // To Do : *******************************

       _contentManager.Publish(page);  
    }