/// <summary>
        /// Publishes a <see cref="T:Tridion.ContentManager.IdentifiableObject"/> to a given <see cref="T:Tridion.ContentManager.CommunicationManagement.PublicationTarget"/> and
        /// with <see cref="T:Tridion.ContentManager.Publishing.PublishPriority"/>
        /// </summary>
        /// <param name="PublishUser"><see cref="T:Tridion.ContentManager.Security.User"/></param>
        /// <param name="Item"><see cref="T:Tridion.ContentManager.IdentifiableObject"/></param>
        /// <param name="Target"><see cref="T:Tridion.ContentManager.CommunicationManagement.PublicationTarget"/></param>
        /// <param name="Priority"><see cref="T:Tridion.ContentManager.Publishing.PublishPriority"/></param>
        /// <param name="startDate"><see cref="T:System.DateTime"/></param>
        public void PublishItem(User PublishUser, IdentifiableObject Item, PublicationTarget Target, PublishPriority Priority, DateTime startDate)
        {
            if (Engine.RenderMode == RenderMode.Publish)
            {
                if (startDate == null)
                {
                    startDate = DateTime.Now;
                }

                using (Session session = new Session(PublishUser.Title))
                {
                    PublishInstruction publishInstruction = new PublishInstruction(session)
                    {
                        StartAt  = startDate,
                        DeployAt = startDate
                    };
                    RenderInstruction renderInstruction = new RenderInstruction(session);

                    renderInstruction.RenderMode         = RenderMode.Publish;
                    publishInstruction.RenderInstruction = renderInstruction;

                    PublishEngine.Publish(new IdentifiableObject[] { session.GetObject(Item.Id) }, publishInstruction, new PublicationTarget[] { Target }, Priority);
                }
            }
        }
        /// <summary>
        /// Check the Publishing queue and determine whether the given TcmUri is already present in the queue.
        /// </summary>
        /// <param name="engine">Engine object</param>
        /// <param name="tcmUri">String representing the tcmuri of the item to check</param>
        /// <param name="state">PublishTransactionState the publish state to filter on</param>
        public static bool IsInPublishingQueue(Engine engine, String tcmUri, PublishTransactionState state)
        {
            Log.Debug(String.Format("Check Publishing queue for item '{0}'", tcmUri));

            Session session = engine.GetSession();
            PublishTransactionsFilter filter = new PublishTransactionsFilter(session);

            filter.PublishTransactionState = state;
            RepositoryLocalObject item = engine.GetObject(tcmUri) as RepositoryLocalObject;

            if (item != null)
            {
                filter.ForRepository = item.ContextRepository;
            }

            PublicationTarget publicationTarget = engine.PublishingContext.PublicationTarget;

            if (publicationTarget != null)
            {
                filter.PublicationTarget = publicationTarget;
            }

            XmlElement          element          = PublishEngine.GetListPublishTransactions(filter);
            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(new NameTable());

            namespaceManager.AddNamespace("tcm", "http://www.tridion.com/ContentManager/5.0");

            String      xPath    = String.Format("tcm:ListPublishTransactions/tcm:Item[@ItemID='{0}']", tcmUri);
            XmlNodeList nodeList = element.SelectNodes(xPath, namespaceManager);

            return(nodeList.Count > 0);
        }
        /// <summary>
        /// Get 'current' PublishTransaction. It tries to identify a PublishTransaction from the publish queue that is on the
        /// given TcmUri, Publication, User, etc.
        /// </summary>
        /// <param name="engine">Engine object</param>
        /// <param name="tcmUri">String representing the tcmuri of the item to check</param>
        /// <returns>PublishTransaction if found; or null, otherwise</returns>
        private static PublishTransaction FindPublishTransaction(Engine engine, String tcmUri)
        {
            Log.Debug(String.Format("Find PublishTransaction for item '{0}'", tcmUri));

            PublishTransaction        result  = null;
            Session                   session = engine.GetSession();
            PublishTransactionsFilter filter  = new PublishTransactionsFilter(session);

            filter.PublishTransactionState = PublishTransactionState.Resolving;
            RepositoryLocalObject item = engine.GetObject(tcmUri) as RepositoryLocalObject;

            if (item != null)
            {
                filter.ForRepository = item.ContextRepository;
            }

            PublicationTarget publicationTarget = engine.PublishingContext.PublicationTarget;

            if (publicationTarget != null)
            {
                filter.PublicationTarget = publicationTarget;
            }

            XmlElement element = PublishEngine.GetListPublishTransactions(filter);


            XmlNamespaceManager namespaceManager = new XmlNamespaceManager(new NameTable());

            namespaceManager.AddNamespace("tcm", "http://www.tridion.com/ContentManager/5.0");

            String      xPath    = String.Format("tcm:ListPublishTransactions/tcm:Item[@ItemID='{0}']", tcmUri);
            XmlNodeList nodeList = element.SelectNodes(xPath, namespaceManager);

            String transactionId;

            if (nodeList != null && nodeList.Count == 1)
            {
                transactionId = nodeList[0].Attributes["ID"].Value;
                TcmUri transactionUri = new TcmUri(transactionId);
                result = new PublishTransaction(transactionUri, session);
            }
            else
            {
                foreach (XmlNode node in element.ChildNodes)
                {
                    transactionId = node.Attributes["ID"].Value;
                    TcmUri transactionUri = new TcmUri(transactionId);
                    result = new PublishTransaction(transactionUri, session);
                    if (IsPublishTransactionForTcmUri(result, tcmUri))
                    {
                        break;
                    }
                    result = null;
                }
            }

            Log.Debug("Returning PublishTransaction " + result);
            return(result);
        }
        /// <summary>
        /// Retrieve the <see cref="T:Tridion.ContentManager.IdentifiableObject" /> published information
        /// </summary>
        /// <param name="identifiableObject"><see cref="T:Tridion.ContentManager.IdentifiableObject" /></param>
        /// <returns><see cref="T:Tridion.ContentManager.Publishing.PublishInfo" /></returns>
        public static IEnumerable <PublishInfo> PublishInfo(this IdentifiableObject identifiableObject)
        {
            if (identifiableObject != null)
            {
                return(PublishEngine.GetPublishInfo(identifiableObject));
            }

            return(new PublishInfo[] { });
        }
        /// <summary>
        /// Determines whether item is published in the current publication context.
        /// </summary>
        /// <param name="identifiableObject">The item <see cref="Tridion.ContentManager.IdentifiableObject"/>.</param>
        /// <param name="publicationTarget">The publication target <see cref="Tridion.ContentManager.CommunicationManagement.PublicationTarget"/>.</param>
        /// <returns>
        ///   <c>true</c> if item is published in current publication context, otherwise <c>false</c>.
        /// </returns>
        public static bool IsPublished(this IdentifiableObject identifiableObject, PublicationTarget publicationTarget)
        {
            if (identifiableObject != null && publicationTarget != null)
            {
                return(PublishEngine.IsPublished(identifiableObject, publicationTarget, true));
            }

            return(false);
        }
 private bool IsPublished(Page page)
 {
     if (Engine.PublishingContext.PublicationTarget != null)
     {
         return(PublishEngine.IsPublished(page, Engine.PublishingContext.PublicationTarget));
     }
     //For preview we always return true - to help debugging
     return(true);
 }
        private static DateTime?GetPublishedDate(Page page, TargetType targetType)
        {
            PublishInfo publishInfo = PublishEngine.GetPublishInfo(page).FirstOrDefault(pi => pi.TargetType == targetType);

            if (publishInfo == null)
            {
                return(null);
            }
            return(publishInfo.PublishedAt);
        }
        private static string GetPublishedDate(Page page, PublicationTarget target)
        {
            ICollection <PublishInfo> publishInfos = PublishEngine.GetPublishInfo(page);

            foreach (PublishInfo publishInfo in publishInfos)
            {
                if (publishInfo.PublicationTarget == target)
                {
                    return(publishInfo.PublishedAt.GetIso8601Date());
                }
            }
            return("");
        }
Ejemplo n.º 9
0
        private void PublishTest()
        {
            var targets = ContentDeliveryTargetUtil.GetSiteSettings("http://local.cds.com/cdsapi/v1/settings/getsettings", "{9FFE62F6-FAE2-46E3-A6E9-2D1B4604767A}");

            if (targets != null && targets.Any())
            {
                var intSettings = targets.FirstOrDefault(x => x.Code.Equals("INT", StringComparison.InvariantCultureIgnoreCase));

                if (intSettings != null)
                {
                    PublishEngineContext publishEngineContext = new PublishEngineContext();
                    publishEngineContext.PublishTargetSettings = intSettings;

                    PublishEngine publishEngine = new PublishEngine();

                    var results = publishEngine.Publish(publishEngineContext);



                    richTextBox1.AppendText(string.Format("MainContentTempFolder:{0}\n", publishEngineContext.Results.MainContentTempFolder));
                    richTextBox1.AppendText(string.Format("MainContentCloned:{0}\n", publishEngineContext.Results.MainContentCloned));

                    richTextBox1.AppendText(string.Format("JsonFilesTempFolder:{0}\n", publishEngineContext.Results.JsonFilesTempFolder));
                    richTextBox1.AppendText(string.Format("JsonFilesDownloaded:{0}\n", publishEngineContext.Results.JsonFilesDownloaded));

                    richTextBox1.AppendText(string.Format("MergeSecondaryAndMaster:{0}\n", publishEngineContext.PublishTargetSettings.GitHubSettings.MergeSecondaryAndMaster));

                    richTextBox1.AppendText(string.Format("SecondaryContentTempFolder:{0}\n", publishEngineContext.Results.SecondaryContentTempFolder));
                    richTextBox1.AppendText(string.Format("SecondaryContentCloned:{0}\n", publishEngineContext.Results.SecondaryContentCloned));


                    richTextBox1.AppendText(string.Format("CiContentTempFolder:{0}\n", publishEngineContext.Results.CiContentTempFolder));
                    richTextBox1.AppendText(string.Format("CiContentCloned:{0}\n", publishEngineContext.Results.CiContentCloned));


                    richTextBox1.AppendText(string.Format("IsAbort:{0}\n", publishEngineContext.Results.IsAbort));
                    richTextBox1.AppendText(string.Format("IsFinished:{0}\n", publishEngineContext.Results.IsFinished));

                    richTextBox1.AppendText(string.Format("ErrorMessage:{0}\n\n", publishEngineContext.Results.ErrorMessage));


                    foreach (var eventItem in publishEngineContext.MessageLogger.GetEvents())
                    {
                        var     typeExample = new { EntryDate = eventItem.TimeStamp.ToLongDateString(), Level = eventItem.Level.Name, Message = eventItem.MessageObject };
                        JObject o           = JObject.FromObject(typeExample);
                        string  json        = o.ToString();
                        richTextBox1.AppendText(string.Format("{0}\n\n", json));
                    }
                }
            }
        }
        /// <summary>
        /// Retrieve the <see cref="T:Tridion.ContentManager.IdentifiableObject" /> published information
        /// </summary>
        /// <param name="identifiableObject"><see cref="T:Tridion.ContentManager.IdentifiableObject" /></param>
        /// <param name="publicationTarget">Publication target <see cref="T:Tridion.ContentManager.CommunicationManagement.PublicationTarget"/> to filter on.</param>
        /// <returns>
        ///   <see cref="T:Tridion.ContentManager.Publishing.PublishInfo" />
        /// </returns>
        public static IEnumerable <PublishInfo> PublishInfo(this IdentifiableObject identifiableObject, PublicationTarget publicationTarget)
        {
            if (identifiableObject != null && publicationTarget != null)
            {
                ICollection <PublishInfo> publishInfos = PublishEngine.GetPublishInfo(identifiableObject);

                if (publishInfos != null && publishInfos.Count > 0)
                {
                    return(publishInfos.Where(x => x.PublicationTarget.Id == publicationTarget.Id));
                }
            }

            return(new PublishInfo[] { });
        }
        /// <summary>
        /// Publishes an <see cref="T:Tridion.ContentManager.IdentifiableObject" /> intelligently if and only if, <see cref="T:Tridion.ContentManager.IdentifiableObject" /> is not in
        /// "Waiting For Publish" state or "Scheduled For Publish" state within the scheduleDateFilter <see cref="T:System.DateTime" />
        /// </summary>
        /// <param name="identifiableObject">The <see cref="T:Tridion.ContentManager.IdentifiableObject" />.</param>
        /// <param name="startDateFilter">The start <see cref="T:System.DateTime"/> filter.</param>
        /// <param name="scheduleDateFilter">The schedule <see cref="T:System.DateTime"/> filter.</param>
        /// <param name="publishStartDate">The publish start <see cref="T:System.DateTime"/>.</param>
        public void PublishIntelligently(IdentifiableObject identifiableObject, DateTime startDateFilter, DateTime scheduleDateFilter, DateTime publishStartDate)
        {
            PublishTransactionsFilter filter = new PublishTransactionsFilter(Engine.GetSession())
            {
                StartDate         = startDateFilter,
                PublicationTarget = PublicationTarget,
                ForRepository     = GetObject <Repository>(Publication.Id)
            };

            PublishTransaction publishTransaction = PublishEngine.GetPublishTransactions(filter)
                                                    .FirstOrDefault(t => t.Items.Count > 0 &&
                                                                    t.Items.First().Id == identifiableObject.Id &&
                                                                    (t.State == PublishTransactionState.WaitingForPublish ||
                                                                     (t.State == PublishTransactionState.ScheduledForPublish && scheduleDateFilter >= t.Instruction.StartAt)));

            if (publishTransaction == null)
            {
                PublishItem(identifiableObject, publishStartDate);
            }
        }
 private bool IsPublished(Page page)
 {
     //For preview we always return true - to help debugging
     return((Engine.PublishingContext?.PublicationTarget == null) || PublishEngine.IsPublished(page, Engine.PublishingContext.PublicationTarget));
 }
        /// <summary>
        /// On (Save, and) Check-in of a Component, create a Page for that Component and update an index Page with the Component and publish both to a staging target.
        /// </summary>
        /// <remarks>
        /// The metadata of the Folder the Component resides in, will be used as the configuration for the actions.
        /// </remarks>
        /// <param name="subject">checked in Component</param>
        /// <param name="args">check in event arguments</param>
        /// <param name="phase">event phase</param>
        private static void ComponentCheckInAction(Component subject, CheckInEventArgs args, EventPhases phase)
        {
            // get Folder from Component for configuration metadata
            Folder folder = (Folder)subject.OrganizationalItem;

            // proceed when Folder has metadata
            if (folder.Metadata == null)
            {
                return;
            }

            ItemFields metadata = new ItemFields(folder.Metadata, folder.MetadataSchema);
            ReiConfig  config   = new ReiConfig(metadata);

            // proceed when metadata contains valid URIs, and Schema of Component is recognised
            if (!config.IsValid || subject.Schema.Id.ItemId != config.SchemaUri.ItemId)
            {
                return;
            }

            // create list of items to publish
            List <IdentifiableObject> items = new List <IdentifiableObject>();

            // if Component is already used on any Page then no need to create new Page and update index, just publish Component
            UsingItemsFilter pageFilter = new UsingItemsFilter(subject.Session)
            {
                ItemTypes = new List <ItemType> {
                    ItemType.Page
                }
            };

            if (subject.HasUsingItems(pageFilter))
            {
                items.Add(subject);
            }
            else
            {
                // create Page and add Component Presentation (using Context Publication of Structure Group)
                TcmUri            localUri          = ReiConfig.TransformTcmUri(subject.Id, config.StructureGroupUri);
                Component         localComponent    = new Component(localUri, subject.Session);
                ComponentTemplate componentTemplate = new ComponentTemplate(config.ComponentTemplateUri, subject.Session);
                Page page = new Page(subject.Session, config.StructureGroupUri);
                try
                {
                    page.Title        = subject.Title;
                    page.FileName     = GetSafeFileName(subject.Title);
                    page.PageTemplate = new PageTemplate(config.PageTemplateUri, subject.Session);
                    page.ComponentPresentations.Add(new ComponentPresentation(localComponent, componentTemplate));
                    page.Save(true);

                    // add Page to publish items list
                    items.Add(page);
                }
                catch (Exception ex)
                {
                    Logger.Write(ex, ReiConfig.Name, LoggingCategory.General, TraceEventType.Error);
                }

                // add Component to index Page (using Context Publication of index Page)
                localUri          = ReiConfig.TransformTcmUri(subject.Id, config.IndexPageUri);
                localComponent    = new Component(localUri, subject.Session);
                componentTemplate = new ComponentTemplate(config.IndexComponentTemplateUri, subject.Session);
                Page indexPage = new Page(config.IndexPageUri, subject.Session);
                try
                {
                    indexPage.CheckOut();
                    indexPage.ComponentPresentations.Add(new ComponentPresentation(localComponent, componentTemplate));
                    indexPage.Save(true);

                    // add index Page to publish items list
                    items.Add(indexPage);
                }
                catch (Exception ex)
                {
                    Logger.Write(ex, ReiConfig.Name, LoggingCategory.General, TraceEventType.Error);
                }
            }

            // publish items
            if (items.Count > 0)
            {
                List <TargetType> targets = new List <TargetType> {
                    new TargetType(config.TargetTypeUri, subject.Session)
                };
                PublishInstruction publishInstruction = new PublishInstruction(subject.Session);
                PublishEngine.Publish(items, publishInstruction, targets, ReiConfig.Priority);
            }
            else
            {
                Logger.Write("No items were published.", ReiConfig.Name, LoggingCategory.General, TraceEventType.Information);
            }
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Publish Event handler which is triggered on publishing (or unpublishing) any repository local object. The handler
        /// will publish the item if published in a source publication to any publications defined as target publications.
        /// </summary>
        /// <param name="publishedItem">Item being published (page, component, category etc)</param>
        /// <param name="args">Publishing or unpublishing arguments</param>
        /// <param name="phase">event phase for the transaction</param>
        private void PublishEvent(RepositoryLocalObject publishedItem, PublishOrUnPublishEventArgs args, EventPhases phase)
        {
            var publication = publishedItem.ContextRepository as Publication;

            // Make sure the publication is in the list of publications where publishing should be mirrored
            if (Settings.SOURCE_PUBS.Any(p => p.Equals(publication?.Title) || p.Equals(publication?.Id)))
            {
                if (Settings.PUBLISH_LOGGING_ENABLED)
                {
                    logger.Debug("Publication title is in the list of source publications.");

                    var publishStatus  = args is PublishEventArgs ? "Publish" : "Unpublish";
                    var publishEvent   = args is PublishEventArgs ? args as PublishEventArgs : null;
                    var unpublishEvent = args is UnPublishEventArgs ? args as UnPublishEventArgs : null;

                    var creator = publishEvent != null?
                                  publishEvent.PublishTransactions?.FirstOrDefault()?.Creator:
                                  unpublishEvent?.PublishTransactions?.FirstOrDefault()?.Creator;

                    logger.Info(
                        $"{publishStatus} event " +
                        $"[initiated by]: {creator.Title} ({creator.Id}) -- " +
                        $"[publishing targets]: {args.Targets.Select(t => $"{t.Title} ({t.Id})")?.PrintList()} -- " +
                        $"[original publish item]: {publishedItem.Id} ({publishedItem.Title}) -- " +
                        $"[all {publishStatus} items (also published)]: {args.Items.Select(i => $"{i.Title} ({i.Id})")?.PrintList()}."
                        );
                }

                // Get the instruction & figure out if its a publish or an unpublish
                var publishUnpublishInstruction = TridionUtil.GetPublishOrUnpublishInstruction(args);

                // Get the publications for which publishing should be mirrored
                var mirrorPublications = TridionUtil.GetPublications(Settings.TARGET_PUBS, publishedItem.Session);

                if (mirrorPublications == null || mirrorPublications.Count < 1)
                {
                    logger.Debug("No mirror publications found."); return;
                }

                if (Settings.PUBLISH_LOGGING_ENABLED)
                {
                    logger.Debug($"Found the list of mirror publications : {mirrorPublications.Select(p => p.Title + ", ")?.PrintList()} .");
                }

                // Get the item which needs to be published in the mirrored publication
                var mirrorItems = TridionUtil.GetItemsInPublications(publishedItem, mirrorPublications?.Select(p => p.Id)?.ToList(), publishedItem.Session);

                if (mirrorItems == null || mirrorItems.Count < 1)
                {
                    logger.Debug("No mirror items found."); return;
                }

                if (Settings.PUBLISH_LOGGING_ENABLED)
                {
                    logger.Debug($"Mirroring {mirrorItems.Count} publish items : {mirrorItems.Select(i => i.Id + ", ")?.PrintList()}.");
                }

                try
                {
                    // Publish the items to be mirrored in the mirrored publications
                    if (args.Targets.Count() > 0 && mirrorItems.Count() > 0 && mirrorPublications.Count() > 0)
                    {
                        var publishedItemIds    = mirrorItems.Select(p => p.Id.ToString())?.PrintList();
                        var publicationTitles   = mirrorPublications.Select(p => p.Title)?.PrintList();
                        var publishingTargetIds = args.Targets.Select(t => t.Id.ToString())?.PrintList();

                        var targetTypes = args.Targets.Select(t => t as TargetType);

                        if (targetTypes == null || targetTypes.Count() < 1)
                        {
                            if (Settings.PUBLISH_LOGGING_ENABLED)
                            {
                                logger.Debug("No target types found. Returning.");
                            }

                            return;
                        }

                        if (args is PublishEventArgs)
                        {
                            if (Settings.PUBLISH_LOGGING_ENABLED)
                            {
                                logger.Debug("Publishing event being mirrored.");
                            }

                            var publishInstruction = (PublishInstruction)publishUnpublishInstruction;

                            if (Settings.FORCE_PUBLISH_CHILD_PUBS)
                            {
                                publishInstruction.ResolveInstruction.IncludeChildPublications = true;
                            }
                            if (Settings.FORCE_PUBLISH_MINOR_VERSION)
                            {
                                publishInstruction.ResolveInstruction.IncludeDynamicVersion = true;
                            }
                            if (Settings.FORCE_PUBLISH_WORKFLOW_VERSION)
                            {
                                publishInstruction.ResolveInstruction.IncludeWorkflow = true;
                            }

                            // if the setting is selected to mirror the transaction only if publish to children is selected, but the setting is false, exit.
                            if (Settings.MIRROR_IF_PROPOGATE_SELECTED && !publishInstruction.ResolveInstruction.IncludeChildPublications)
                            {
                                if (Settings.PUBLISH_LOGGING_ENABLED)
                                {
                                    logger.Debug("Exiting as mirror if propogate selected, but setting is false.");
                                }

                                return;
                            }

                            if (Settings.PUBLISH_LOGGING_ENABLED)
                            {
                                logger.Info($"Mirroring publishing items '{publishedItemIds}' -- to publications {publicationTitles} -- to targets {targetTypes.Select(t => t.Title)?.PrintList()}.");
                            }

                            PublishEngine.Publish(mirrorItems, publishInstruction, targetTypes, PublishPriority.Low);
                        }
                        else if (args is UnPublishEventArgs)
                        {
                            if (Settings.PUBLISH_LOGGING_ENABLED)
                            {
                                logger.Debug("Unpublishing event being mirrored.");
                            }

                            var unpublishInstruction = (UnPublishInstruction)publishUnpublishInstruction;

                            if (Settings.FORCE_PUBLISH_CHILD_PUBS)
                            {
                                unpublishInstruction.ResolveInstruction.IncludeChildPublications = true;
                            }

                            // if the setting is selected to mirror the transaction only if publish to children is selected, but the setting is false, exit.
                            if (Settings.MIRROR_IF_PROPOGATE_SELECTED && !unpublishInstruction.ResolveInstruction.IncludeChildPublications)
                            {
                                if (Settings.PUBLISH_LOGGING_ENABLED)
                                {
                                    logger.Debug("Exiting as mirror if propogate selected, but setting is false.");
                                }
                                return;
                            }

                            if (Settings.PUBLISH_LOGGING_ENABLED)
                            {
                                logger.Info($"Mirroring unpublishing items '{publishedItemIds}' -- to publications {publicationTitles} -- to targets {publishingTargetIds}.");
                            }

                            PublishEngine.UnPublish(mirrorItems, unpublishInstruction, targetTypes, PublishPriority.Low);
                        }
                    }
                }
                catch (Exception ex)
                {
                    logger.Error($"Error publishing items : {ex.Message} {ex.ToString()} {ex.StackTrace}");
                }
            }
        }