0

I need to convert below event code(Tridion 2009) in Tridion 2011. Can anyone please help me ??

Convert component type and get fields and Multimedia property

//Sample Code
Component compCurrent = null;
compCurrent = page.ComponentPresentations[1].Component;

string compTitle = compCurrent.Info.OwningPublication.Title;
string compID = compCurrent.ID;
string compTitleValue = compCurrent.Fields["Title"].value[1];
string fileName = compCurrent.Multimedia.Filename;

Convert component type and page publish

    //Sample Code
    Tridion.ContentManager.Interop.TDS.Page page2 = null;
    page2 = (Tridion.ContentManager.Interop.TDS.Page)TDSEClass.GetObject(uRI, EnumOpenMode.OpenModeView, TDSEClass.GetPublication(TcmUri.PublicationId.ToString()), XMLReadFilter.XMLReadAll);
    page2.Publish(targets, false, true, true, DateTime.Now.AddMinutes(-82.0), DateTime.MinValue, DateTime.Now, true, EnumPublishPriority.Normal, false, 0);

My whole method is as below. The parameter named Page in method is of type Tridion.ContentManager.CommunicationManagement.Page

And the whole method body is written in event code of Tridion 2009.

public void ProcessSciencePrizeGalleryPageForPublish(Page page, TcmUri pubTargUri)
    {
        Component compCurrent = null;
        EventHelper o = null;
        TDSEClass class2 = null;
        Page page2 = null;
        Component component = null;
        Component component3 = null;
        Component component4 = null;
        PublicationTarget targets = null;
        StringBuilder builder = new StringBuilder();
        XmlDocument document = new XmlDocument();
        string prefixUrl = string.Empty;
        try
        {
            o = new EventHelper();
            class2 = new TDSEClass();
            class2.Impersonate("MTSUser");
            targets = class2.GetPublicationTarget(pubTargUri);
            string str = "";
            compCurrent = page.ComponentPresentations[1].Component;

            Publication publication = class2.GetPublication(page.Publication.ID);
            string str2 = "<image>";
            builder.AppendFormat("<comptcm>{0}</comptcm>", compCurrent.ID);
            builder.AppendFormat("<pagetcm>{0}</pagetcm>", page.ID);
            builder.AppendFormat("<title>{0}</title>", compCurrent.Fields["Title"].value[1]);

            if ((compCurrent.Fields["thumbnail"] != null) && (compCurrent.Fields["thumbnail"].value.Count > 0))
            {
                component3 = (Component)compCurrent.Fields["thumbnail"].value[1];
                string strthumbnailimagepath = publication.MultimediaURL + "/" + this.GetImagePublishPath(component3.Multimedia.Filename, component3.ID);
                builder.AppendFormat("<thumbnail>{0}</thumbnail>", strthumbnailimagepath);
            }

            if ((compCurrent.Fields["previewimage"] != null) && (compCurrent.Fields["previewimage"].value.Count > 0))
            {
                component4 = (Component)compCurrent.Fields["previewimage"].value[1];
                string strpreviewimagepath = publication.MultimediaURL + "/" + this.GetImagePublishPath(component4.Multimedia.Filename, component4.ID);
                builder.AppendFormat("<previewimage>{0}</previewimage>", strpreviewimagepath);
                builder.AppendFormat("<imagesize>{0}</imagesize>", component4.Multimedia.FileSize);
            }

            builder.AppendFormat("<category>{0}</category>", compCurrent.Fields["category"].value[1]);

            string pubDateFormat = string.Empty;
            try
            {
                pubDateFormat = Convert.ToDateTime(compCurrent.Fields["publish_date"].value[1].ToString()).GetDateTimeFormats('s')[0];
                // strXMLBody.AppendFormat("<publish_date>{0}</publish_date>", string.Format("{0:yyyy-MM-dd hh:mm:ss}", Component.Fields["publish_date"].value[1]));
            }
            catch (Exception)
            {
            }

            builder.AppendFormat("<publish_date>{0}</publish_date>", pubDateFormat);
            builder.AppendFormat("<short_description><![CDATA[{0}]]></short_description>", compCurrent.Fields["short_description"].value[1]);
            builder.AppendFormat("<displayimageonStagProd>{0}</displayimageonStagProd>", compCurrent.Fields["displayimageonStagProd"].value[1]);

            string str3 = "</image>";

            string uRI;
            uRI = o.Value("SCIENCEPRIZEIMAGEGALLERYPAGE");

            page2 = (Page)class2.GetObject(uRI, EnumOpenMode.OpenModeView, class2.GetPublication(page.Publication.ID), XMLReadFilter.XMLReadAll);
            component = page2.ComponentPresentations[1].Component;

            if (!string.IsNullOrEmpty(component.Fields["Body"].value[1].ToString()))
            {
                document.LoadXml(component.Fields["Body"].value[1].ToString());
                XmlNode oldChild = document.SelectSingleNode("descendant::image[comptcm='" + compCurrent.ID + "']");
                if (oldChild != null)
                {
                    oldChild.ParentNode.RemoveChild(oldChild);
                }
                XmlNode node2 = document.SelectSingleNode("images");
                XmlElement newChild = null;
                newChild = document.CreateElement("image");
                newChild.InnerXml = builder.ToString();
                node2.AppendChild(newChild);

                str = document.InnerXml.ToString();

                oldChild = null;
                newChild = null;
            }
            else
            {
                str = "<?xml version=\"1.0\" encoding=\"utf-8\"?><images>" + str2 + builder.ToString() + str3 + "</images>";
            }

            if (component.Info.IsCheckedOut)
            {
                component.CheckIn(true);
            }
            component.CheckOut(true);
            component.Fields["Body"].value[1] = str;
            component.Save(true);
            component.CheckIn(true);

            page2.Publish(targets, false, true, true, DateTime.Now.AddMinutes(-82.0), DateTime.MinValue, DateTime.Now, true, EnumPublishPriority.Normal, false, 0);

        }
        catch (Exception exception)
        {
            EventHelper.WriteEventToHTMLLog("ProcessSciencePrizeGalleryPageForPublish: Error " + exception.Message + Environment.NewLine + "StackTrace: " + exception.StackTrace.ToString());
            throw exception;
        }
        finally
        {
            Marshal.ReleaseComObject(compCurrent);
            o = null;
            Marshal.ReleaseComObject(class2);
            Marshal.ReleaseComObject(page2);
            Marshal.ReleaseComObject(component);
            Marshal.ReleaseComObject(component3);
            Marshal.ReleaseComObject(targets);
            builder = null;
            document = null;
        }
    }

Pls help..

10
  • 1
    Please explain it and give little background of your problem. Commented Mar 4, 2014 at 12:14
  • 1
    It would also help if you explained the context for the code - is it a template, a command line tool, an event system?
    – Nuno Linhares
    Commented Mar 4, 2014 at 12:16
  • Hi Saurabh & Nuno. I have updated my question as per your suggestion.
    – Urvesh
    Commented Mar 4, 2014 at 12:35
  • Hi Nuno,I have modified the question to indicate that it is event code
    – Urvesh
    Commented Mar 4, 2014 at 12:48
  • 2
    No offense, but we're not here to do your job :) Unless you have a specific question that would also be useful to other Stack Exchange users, this question is likely to be closed. Right now it reads like "Could someone please convert this code for me" which isn't appropriate for this site. Commented Mar 4, 2014 at 15:25

1 Answer 1

5

The migration of a TOM Interop based event system to a TOM.NET based event system is not something you should approach by just rewriting some code. You are migrating from one API to another.

Even though the main objects like Page and Component mean the same thing, they will each have different interfaces, methods and properties.

For starters you need to understand that your TOM Interop event system was COM based and you accessed the SDL Tridion CMS all via the TDSEClass. The TOM.NET based event system, has a subscribe model, you subscribe to delegate methods called by the CMS core.

I would suggest you start with reading up on the features of the new Event System which you can do here and here (please note I'm not just sending you away with a few links, it is really important that you understand how the new model works if you want to migrate your code).

The next steps will be to extract the requirements of your old event system (you can do this based on the code) and to implement them in a new (set of) event handler(s). Keep in mind that when you really start from the requirements, you will see that maybe with the new model and TOM.NET API, you can improve a lot of your code. You will definitely get better performance since you're using TOM.NET instead of COM Interop. You will also have a finer granularity where you can hook into different levels. So you can implement only the events you need, and you can even choose do this in separate assemblies making your event system modular.

Once you understand the new model (and to some extend the differences of it compared to the old model), you can start with implementing your first event handler. The basis of your code will be this:

using Tridion.ContentManager.CommunicationManagement;
using Tridion.ContentManager.ContentManagement;
using Tridion.ContentManager.Extensibility;
using Tridion.ContentManager.Extensibility.Events;

namespace Example
{
  [TcmExtension("ExampleEventHandlerExtension")]
  public class EventHandler : TcmExtension
  {
    public EventHandler()
    {
      EventSystem.Subscribe<Page, PublishEventArgs>(PagePublishAction, EventPhases.Initiated);
    }

    private static void PagePublishAction(Page subject, PublishEventArgs args, EventPhases phase)
    {
      Publication publication = (Publication) subject.ContextRepository;
      Component component = subject.ComponentPresentations[1].Component;
    }
  }
}

For more example code and particularly working with Component Fields and item Save actions, see also my Rapid Editorial Interface event handler. I've migrated that event system a couple of years ago from a TOM based one to the TOM.NET API.

See also onComponentSave (SaveEventArgs) for an example of how to work with the TOM.NET Fields Collection of a Component, I would like to point out, that the TOM.NET Fields Collection is a detached collection, so when updating a Field in that Collection you need to sync that change back to the Component as shown in the line: component.Metadata = metadata.ToXml(); which could also be something like component.Content = fields.ToXml();.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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