Пример #1
0
        // add item version
        public static void UpdateItemInformation(Item item, Sitecore.Workflows.WorkflowState newState)
        {
            if (item == null || String.IsNullOrEmpty(item.Fields["__Default workflow"].Value))
            {
                return;
            }
            try
            {
                Item isIntialVersionInWeb = Sitecore.Configuration.Factory.GetDatabase("web").GetItem(item.ID);
                Sitecore.Workflows.IWorkflow currnetItemWorkflow = item.Database.WorkflowProvider.GetWorkflow(item);
                if (!currnetItemWorkflow.GetState(item).FinalState&& item.Fields["__Workflow state"].Value != ID.Parse(newState.StateID).ToString())
                {
                    using (new Sitecore.SecurityModel.SecurityDisabler())
                    {
                        // Update New Workflow state first
                        item.Editing.BeginEdit();
                        item.Fields["__Workflow state"].Value = ID.Parse(newState.StateID).ToString();
                        item.Locking.Unlock();
                        item.Editing.EndEdit();

                        // If it is final state
                        if (newState.FinalState)
                        {
                            Item updatedItem = item.Versions.AddVersion();
                            updatedItem.Editing.BeginEdit();
                            updatedItem.Fields["__Workflow state"].Value = ID.Parse(newState.StateID).ToString();
                            updatedItem.Locking.Unlock();
                            updatedItem.Editing.EndEdit();
                        }
                    }
                }
                Sitecore.Diagnostics.Log.Audit("HealthIS WP- Finish updating item \"" + item.Paths.FullPath + "\"", item);
            }
            catch (Exception ex)
            {
                Sitecore.Diagnostics.Log.Error("HealthIS WP- Error while updating item \"" + item.Paths.FullPath + "\"", ex, item);
            }

            try
            {
                // If it is final state, find any media item in the datasoure and publish
                if (newState.FinalState)
                {
                    foreach (Field field in item.Fields)
                    {
                        // Media Field
                        if (FieldTypeManager.GetField(field) is ImageField && !String.IsNullOrEmpty(field.Value))
                        {
                            ImageField imagePath = field;
                            Item       mediaItem = imagePath.MediaItem.Paths.Item;

                            // Publish image item
                            using (new Sitecore.SecurityModel.SecurityDisabler())
                            {
                                Database source  = Sitecore.Configuration.Factory.GetDatabase("master");
                                Database target  = Sitecore.Configuration.Factory.GetDatabase("web");
                                var      options = new Sitecore.Publishing.PublishOptions(source, target,
                                                                                          Sitecore.Publishing.PublishMode.SingleItem, mediaItem.Language,
                                                                                          DateTime.Now)
                                {
                                    RootItem = mediaItem,
                                    Deep     = false,
                                };
                                var publisher = new Sitecore.Publishing.Publisher(options);
                                publisher.PublishAsync();
                            }
                            Sitecore.Diagnostics.Log.Audit("HealthIS WP- Finish updating media item field \"" + field.Value + "\" in the item \"" + mediaItem.Paths.FullPath + "\"", mediaItem);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Sitecore.Diagnostics.Log.Error("HealthIS WP- Error while updating media item in \"" + item.Paths.FullPath + "\"", ex, item);
            }
        }
        //public void PublishAuthorProfile(Item dataItem)
        //{
        //    // Match Created By field
        //    string createdBy = dataItem.Statistics.CreatedBy.Replace("\\", "");
        //    Item getUserProfileItem = BlogSettings.AuthorProfileLibrary.Axes.GetDescendants().Where(c => c.Name.Equals(createdBy)).FirstOrDefault();
        //    Item itemInWeb = targetDb.GetItem(getUserProfileItem.ID);
        //    if (getUserProfileItem != null && (itemInWeb == null || getUserProfileItem.Statistics.Revision != itemInWeb.Statistics.Revision))
        //    {
        //        Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(masterDb,
        //            targetDb,
        //            Sitecore.Publishing.PublishMode.SingleItem,
        //            getUserProfileItem.Language,
        //            System.DateTime.Now);

        //        Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions);
        //        publisher.Options.RootItem = getUserProfileItem;
        //        publisher.Options.Deep = false;
        //        publisher.Options.PublishRelatedItems = true;
        //        publisher.Publish();
        //    }
        //}

        public void PublishPostItem(Item dataItem)
        {
            Item item = dataItem;

            // Add new version / Update Workflow / Keep inital created date for RSS Feed Sort
            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                if (Sitecore.Data.Database.GetDatabase("web").GetItem(item.ID) != null)
                {
                    Sitecore.Workflows.IWorkflow     workflow    = item.Database.WorkflowProvider.GetWorkflow(item);
                    Sitecore.Workflows.WorkflowState newState    = workflow.GetStates().FirstOrDefault(state => state.FinalState == true);
                    Sitecore.Data.Fields.DateField   pubDateTime = item.Fields["pubDate"];

                    string originalAuthor = dataItem.Fields["__Created by"].Value;

                    item = dataItem.Versions.AddVersion();
                    item.Editing.BeginEdit();
                    item.Fields["__Workflow state"].Value = newState.StateID.ToString();
                    item.Fields["__Created"].Value        = pubDateTime.Value;
                    item.Fields["__Created by"].Value     = originalAuthor;
                    item.Locking.Unlock();
                    item.Editing.EndEdit();
                }
            }

            //// Clearing master db cache
            //ClearCaches.ClearDatabaseCache(masterDb, item);

            // Author Profile - Republish
            if (BlogSettings.AuthorProfileLibrary != null)
            {
                PublihsingMethod(BlogSettings.AuthorProfileLibrary, Sitecore.Publishing.PublishMode.SingleItem, true, true);
            }

            // Media Library - Smart Publish
            Item imageLibrary = masterDb.GetItem(BlogSettings.ImageLibrary);

            if (imageLibrary != null)
            {
                PublihsingMethod(imageLibrary, Sitecore.Publishing.PublishMode.Smart, true);
            }

            // Tag Repository - Smart Publish
            if (BlogSettings.TagRepository != null)
            {
                PublihsingMethod(BlogSettings.TagRepository, Sitecore.Publishing.PublishMode.Smart, true);
            }

            // Post - Republish
            if (item != null)
            {
                PublihsingMethod(item, Sitecore.Publishing.PublishMode.SingleItem, false);
            }

            //// Clearing web db cache
            //ClearCaches.ClearDatabaseCache(targetDb, item);

            // Refresh the new post for search index web
            if (Sitecore.Data.Database.GetDatabase("web").GetItem(item.ID) != null)
            {
                var tempItem = (SitecoreIndexableItem)Sitecore.Data.Database.GetDatabase("web").GetItem(item.ID);
                // Replace master with web
                string searchIndexWeb = BlogSettings.SearchIndexName.Replace("master", "web");
                Sitecore.ContentSearch.ContentSearchManager.GetIndex(searchIndexWeb).Refresh(tempItem);
                Sitecore.Diagnostics.Log.Audit("Search Indexing: " + searchIndexWeb, this);
            }
        }