1

We need to implement Short URL Functionality for one of the marketing Campaign. For example www.domainurl/x/y/z should go to www.domainurl/a

We have a redirect Page and Component through which redirection works perfectly fine. I want to follow below approach to have ShortURL Functionality:

Approach: Editors have choice to add a Short URL in the redirect Components because Short URL is nothing but only a redirection.

If we go with this approach, following things need to be done.

  1. Add a redirect in redirect Component where From will consist "ShortURL", and To will have actual URL.

  2. While Saving redirect Component we need to check following things through Event System:

    a. Check, there should not be any published page with the same name as Short URL in the root directory

    b. Check, there should not be any duplicate Short URLs in redirect Component

  3. For every Page unpublishing we need to check:

    • If the Page is in the redirect Components "To" field then remove that redirect entry from redirect Component and publish redirect Page.

So My Questions are :

  1. Can I get all Root Level Pages on each Page publishing/unpublishng or on Component saving?

  2. Can I get a Component based on a Schema on each Page publishing/unpublishng ?

4

When you subscribe to the following event handler:

EventSystem.Subscribe<Page, UnPublishEventArgs>(OnPageUnPublish, EventPhases.Processed);

You will have access to the Page that is going to be unpublished. But please note, it is a trigger on the action in the UI of unpublishing the Page, it is not unpublished (not even when you choose the Event Phase TransactionCommitted, since it still has to go through the Content Delivery side to get unpublished). So you might want to choose instead to subscribe to the changing of the publish state, since then you know it is successfully unpublished.

EventSystem.Subscribe<Page, SetPublishStateEventArgs>(OnPageStateChange, EventPhases.TransactionCommitted);

Then in your OnPageStateChange method you can check whether it was a publish or unpublish action:

private void OnPageStateChange(Page subject, SetPublishStateEventArgs args, EventPhases phase)
{
    if (!args.IsPublished)
    {
        // Page was just unpublished on args.Target
    }
}

update

Your added questions are a bit like: "please do my homework for me", it is a lot more beneficial if you think about a solution and ask for help where you get stuck, but I'll do my best to give you a nudge in the right direction.

  1. Yes you can get to any item in the CM from inside an event handler. Look at my event handler example code and the subject parameter is the Page (similar thing you would have in a Component event handler), so you can simply use the following:

    Publication pub = (Publication)subject.ContextRepository;
    StructureGroup root = pub.RootStructureGroup;
    
    // get list of Pages in Structure Group
    OrganizationalItemItemsFilter filter = new OrganizationalItemItemsFilter(subject.Session)
    {
        ItemTypes = new[] { ItemType.Page },
        Recursive = false
    };
    IEnumerable<Page> items = root.GetItems(filter).Cast<Page>();
    foreach (Page page in items)
    {
        // do whatever you need to do with these Pages
    }
    
  2. Again yes you can, but you will need to add a configuration to your event handler to store the Schema, or get it in another way. To find a Component based on a certain Schema is not that difficult.

    // get the Schema TCM URI from somewhere (hardcoded in this example)
    TcmUri tcmUri = new TcmUri("tcm:1-2-8");
    Schema schema = new Schema(tcmUri, subject.Session)
    
    // get list of Components based on given Schema
    OrganizationalItemItemsFilter filter = new OrganizationalItemItemsFilter(subject.Session)
    {
        ItemTypes = new[] { ItemType.Component },
        BasedOnSchemas = new[] { schema },
        Recursive = false
    };
    Publication pub = (Publication)subject.ContextRepository;
    IEnumerable<Component> items = pub.RootFolder.GetItems(filter).Cast<Component>();
    foreach (Component comp in items)
    {
        // do whatever you need to do with these Components
        // keep in mind, you might only have one Component based on this Schema, 
        // but there can always be multiple
        // Maybe instead of getting a Component based on a Schema, 
        // you can store a link to the Component in the Publication Metadata
        // and get it right away from there
    }
    
  • thanks @Bart , but on each Page Unpublishing,i i want to access a redirect Component , this redirect Componnet is no where link with the Pages. so i am wondering if i can access a component which is not linked to the page, also i want to access all root Structure Group Pages on any Page Unpublishing. – Neelesh Raghuvanshi Nov 14 '17 at 12:56
  • @NeeleshRaghuvanshi I updated my answer based on your updated question – Bart Koopman Nov 15 '17 at 9:54
  • Thanks for this help, I don't know why,but i forgot that Event System uses nothing but just a TOM net API. – Neelesh Raghuvanshi Nov 15 '17 at 11:20
  • Glad I could help with that, an Event Handler is in my opinion the easiest way to accomplish things since you have full Visual Studio support and when you add to that the TOM.NET API guide you should have endless possibilities. – Bart Koopman Nov 15 '17 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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