1

I have a javascript file which is calling a service call method on the click of Save command. I need to validate this functionality only for pages. However, on the first save in tridion, pages will not have a valid TCM ID ending with "-64". How do I validate if the object is a page and then trigger my method from JavaScript?

Or should this be done only via Event handler? If yes, how?

Thanks,

Keirthana

Update:

A detailed picture of my situation to explain why I need JavaScript:

I am adding a customized tab at the component presentation level (next to the preview tab on the page when you select a component presentation). This tab contains 2 fields with data that I have to store in the APP_DATA table. The input data from the fields is available on the client side JavaScript since we are getting it by accessing the corresponding tab controls using the page object. Hence to save this data to the APP_DATA table, I use a service call which stores and retrieves my data. In this scenario, I get the invalid URI problem on first save since there won't be a valid page ID generated until the server side calls are over.

  • 2
    Why would you want to do it through Javascript? Use Event system, unless there's a valid reason to not use it – Andrey Marchuk May 1 '14 at 13:02
  • Can you explain your use case? What is your web service validating? what parameters does it need from the page to operate? What does this web service returns, and do you need to prevent the editor from saving that page based on the webservice call result or not? Or do you need to add content to the page metadata fields based on the web service results? You should be able to call you web service directly from an Event handler and read the TcmUri from subject.Id . – Philippe Conil May 1 '14 at 22:23
  • @PhilippeConil: My JavaScript is calling a web service method to save CP metadata in the APP_DATA table from the page. This should happen only for pages and hence need to validate if the object is a page before calling the web service method. I get the feeling this is not possible via JS(that is to retrieve the page TCM ID on the first click of save) So if we need to do it using event handler, can you help me with some steps to walkthrough? – Keirthana May 2 '14 at 5:43
  • Your requirements sound very "business logic" like, which means they really should be implemented in an event handler (as Andrey suggests). If you have never written a Tridion event handler, I'd suggest first spending a few searches on that. – Frank van Puffelen May 2 '14 at 6:58
  • @Keirthana I think you should be able to find out whether you are saving a Page depending on the view the Save was called from. But I have to agree with Frank that it sounds like you will be way better of with an event handler. Check out sdltridionworld.com/articles/sdltridion2011/… – Bart Koopman May 2 '14 at 16:35
1

Here is an untested code sample to get you started.

You can add the code to check if the current page already has a version to know if the event was triggered on first save or not.

Add this to a new class library C# project in Visual Studio, compile it, then add the DLL somewehre on the Content Manager server, then register it in the Tridion/config/Tridion.ContentManager.config file as follows:

...
<extensions>
<add assemblyFileName="C:\pathToTheExtensionDll\Tridion.Extensions.Events.dll"/>
...
</extensions>

Sample Code:

using System;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.ContentManagement.Fields;
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.Extensibility.Events;
using Tridion.Logging;

namespace Tridion.Extensions.Events
{
[TcmExtension("Page Events")]
public partial class PageEventsHandler : global::Tridion.ContentManager.Extensibility.TcmExtension
{
    #region Constructor
    /// <summary>
    /// Constructor to register events with Tridion Content Manager
    /// </summary>
    public PageEventsHandler()
    {
        try
        {
            RegisterEventHandlers();
        }
        catch (Exception e)
        {
            PageEventsHandler.LogMessage("An error occurred while registering event handlers: \n" + e.Message);
        }
    }
    #endregion

    /// <summary>
    /// Registers component (item) related event handlers with Tridion Content Manager
    /// </summary>
    private void RegisterEventHandlers()
    {
        EventSystem.Subscribe<Page, SaveEventArgs>(
            SetPageAppData, 
            EventPhases.Initiated, 
            EventSubscriptionOrder.Early
        );
    }

    /// <summary>
    /// Sets a Page App Data with CP metadata information
    /// </summary>
    /// <param name="subject">Page about to be saved</param>
    /// <param name="args">Event data for the save action</param>
    /// <param name="phase">Current event phase</param>
    private void setPageAppData(Page subject, SaveEventArgs args, EventPhases phase)
    {
        TcmUri pageUri = subject.Id;
        //Add AppData
    }

    #region Class Methods
    /// <summary>
    /// Writes a message to the Tridion log
    /// </summary>
    /// <param name="message">Message to log</param>
    private static void LogMessage(string message)
    {
        Tridion.Logging.Logger.Write(message, "Page Events", LoggingCategory.General);
    }
    #endregion
}
}
| improve this answer | |
0

Thank you all for your answers, but since this question and answers are getting beyond the original scope of the query, I have posted my exact query at Invalid Page ID when pushing data to APP_DATA filtering down the exact issue.

| improve this answer | |

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.