public void PublishItem(int changeId, string approvalStatus) { List <ItemChangeHistory> changeList = GetItemChangeHistory(changeId, approvalStatus); if (changeList.Count > 0) { ItemChangeHistory change = GetItemChangeHistory(changeId, approvalStatus)[0]; string itemId = change.ItemId; string changeType = change.ChangeType; ETDataExtension etd = new ETDataExtension(); etd.InsertIntoDataExtension(change.ItemHtml, change.ChangeType, change.ProductCategory); Sitecore.Data.Database master = Sitecore.Configuration.Factory.GetDatabase("master"); Sitecore.Data.Database web = Sitecore.Configuration.Factory.GetDatabase("web"); Sitecore.Data.Items.Item item = master.Items.GetItem(itemId); Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(item.Database, web, Sitecore.Publishing.PublishMode.SingleItem, item.Language, System.DateTime.Now); Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); publisher.Options.RootItem = item; publisher.Options.Deep = true; publisher.Publish(); } }
public void removeProgramsWhite(object sender, EventArgs e) { ProgramsWhite(sender, e); foreach (Sitecore.Data.Items.Item result in ProgramList) { using (new Sitecore.SecurityModel.SecurityDisabler()) { Domain.Pages.Program program = Domain.Context.Get <Domain.Pages.Program>(result.ID); result.Editing.BeginEdit(); try { result.Fields["Title"].Value = program.Title.Trim(); result.Editing.EndEdit(); } catch (System.Exception ex) { Sitecore.Diagnostics.Log.Error("Could not update item " + result.Paths.FullPath + ": " + ex.Message, this); result.Editing.CancelEdit(); } Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(result.Database, Sitecore.Data.Database.GetDatabase("web"), Sitecore.Publishing.PublishMode.SingleItem, result.Language, System.DateTime.Now); // Create a publisher with the publishoptions Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); // Choose where to publish from publisher.Options.RootItem = result; // Do the publish! publisher.Publish(); } } Programs.Text += ProgramList.Count.ToString() + " Program Titles Trimed"; }
public void Process(object sender, EventArgs args) { SitecoreEventArgs sArgs = args as SitecoreEventArgs; Sitecore.Publishing.Publisher pbl = sArgs.Parameters[0] as Sitecore.Publishing.Publisher; //This prevents from publishing the whole site by mistake if (pbl.Options.Deep) { var allowPublishigWithSubitems = false; customPublishConfig = sitecoreService.GetItem <ICustom_Publishing_Config>(ItemReferences.Instance.CustomPublishingConfig); if (customPublishConfig != null) { allowPublishigWithSubitems = customPublishConfig.Allow_Publishing_With_Subitems; } if (allowPublishigWithSubitems == false) { var rootItem = pbl.Options.RootItem; if (rootItem != null && (rootItem.TemplateID.Guid.Equals(new Guid(Constants.PublicationRootTemplateID)) || rootItem.TemplateID.Guid.Equals(new Guid(Constants.HomeRootTemplateID)) )) { pbl.Options.Deep = false; } } } }
/// <summary> /// Publishes a Sitecore <see cref="Item"/>. /// </summary> private static void PublishItem(Item item) { if (item == null) { throw new ArgumentNullException(nameof(item)); } using (new Sitecore.SecurityModel.SecurityDisabler()) { Database master = Sitecore.Configuration.Factory.GetDatabase("master"); Database web = Sitecore.Configuration.Factory.GetDatabase("web"); var publishOptions = new Sitecore.Publishing.PublishOptions( master, web, Sitecore.Publishing.PublishMode.SingleItem, item.Language, DateTime.Now); Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); publisher.Options.RootItem = item; publisher.Options.Deep = true; publisher.Publish(); } }
private void RepublishItem(Item item) { // Get all publishing targets var publishingTargets = Sitecore.Publishing.PublishManager.GetPublishingTargets(item.Database); // Loop through each target, determine the database, and publish foreach (var publishingTarget in publishingTargets) { // Find the target database name, move to the next publishing target if it is empty. var targetDatabaseName = publishingTarget["Target database"]; if (string.IsNullOrEmpty(targetDatabaseName)) { continue; } // Get the target database, if missing skip var targetDatabase = Sitecore.Configuration.Factory.GetDatabase(targetDatabaseName); if (targetDatabase == null) { continue; } // Setup publishing options based on your need Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(item.Database, targetDatabase, Sitecore.Publishing.PublishMode.SingleItem, item.Language, System.DateTime.Now); Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); publisher.Options.RootItem = item; publisher.Options.Deep = true; var publishResult = publisher.PublishWithResult(); } }
/// <summary> /// Publish an item to all publishing targets /// </summary> /// <param name="item"></param> /// <param name="mode">The publishing mode</param> /// <param name="publishSubItems">Specify whether to publish sub items</param> public static void Publish(this Item item, Sitecore.Publishing.PublishMode mode, bool publishSubItems) { Assert.ArgumentNotNull(item, "item"); try { //get all publishing targets var publishingTargetsFolder = item.Database.GetItem("/sitecore/system/Publishing targets"); if (publishingTargetsFolder != null) { var targetDBs = new List <Database>(); var languages = item.Languages; var targets = publishingTargetsFolder.Children.Where(o => string.Compare(o.TemplateName, "Publishing target", true) == 0); //get all defined databases foreach (var t in targets) { var db = Sitecore.Data.Database.GetDatabase(t["Target database"]); if (db != null) { targetDBs.Add(db); } } if (targetDBs.Any()) { foreach (var targetDB in targetDBs) { var publishOptions = new Sitecore.Publishing.PublishOptions( item.Database, targetDB, mode, item.Language, DateTime.Now); var publisher = new Sitecore.Publishing.Publisher(publishOptions); publisher.Options.RootItem = item; publisher.Options.Deep = publishSubItems; publisher.Publish(); } } } } catch (Exception ex) { Sitecore.Diagnostics.Log.Error("Sitecore - unable to auto-publish item", ex, typeof(ItemExtensions)); } }
//public string SaveNewEvent(string parentId, EventViewModel eventToSave) //{ // using (new Sitecore.SecurityModel.SecurityDisabler()) // { // try // { // var masterDB = Database.GetDatabase("master"); // TemplateItem templateItem = masterDB.GetItem(Templates.Event.Id); // var parentItem = masterDB.GetItem(new ID(parentId)); // if (parentItem != null) // { // var eventName = // $"{eventToSave.Name.Replace(@"\", "").Replace(@"/", "").Replace(@":", "").Replace(@"?", "").Replace(@"<", "").Replace(@">", "").Replace(@"|", "").Replace(@"[", "").Replace(@"]", "").Replace(@"-", "").Replace("\"", "").Replace(@"(", "").Replace(@")", "")}"; // var newEvent = parentItem.Add(eventName, templateItem); // newEvent.Editing.BeginEdit(); // newEvent.Fields["ModuleTitle"].Value = projectModel.ModuleTitle; // newEvent.Editing.EndEdit(); // PublishItem(newEvent); // return LinkManager.GetItemUrl(newEvent, new ItemUrlBuilderOptions()); // } // } // catch (Exception e) // { // Console.WriteLine(e); // throw; // } // } //} private static void PublishItem(Item item) { var publishOptions = new Sitecore.Publishing.PublishOptions(item.Database, Database.GetDatabase("web"), Sitecore.Publishing.PublishMode.SingleItem, item.Language, DateTime.Now); var publisher = new Sitecore.Publishing.Publisher(publishOptions); publisher.Options.RootItem = item; publisher.Options.Deep = true; publisher.Publish(); }
public static void Publish(Item itemToPublish, Sitecore.Data.Database publishDatabase) { var publishOptions = new Sitecore.Publishing.PublishOptions(itemToPublish.Database, publishDatabase, Sitecore.Publishing.PublishMode.SingleItem, itemToPublish.Language, DateTime.Now); var publisher = new Sitecore.Publishing.Publisher(publishOptions); publisher.Options.RootItem = itemToPublish; publisher.Options.Deep = true; publisher.Publish(); }
/// <summary> /// Publishes the item. /// </summary> /// <param name="targetItem">The target item.</param> public static void PublishItem(Item targetItem, bool childs = false) { DateTime publishDate = DateTime.Now; Sitecore.Data.Database master = GetContentDatabase(); var targetDBs = GetPublishingTargets(master); foreach (var db in targetDBs) { foreach (Sitecore.Globalization.Language language in master.Languages) { Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(master, db, Sitecore.Publishing.PublishMode.SingleItem, language, publishDate); publishOptions.RootItem = targetItem; publishOptions.Deep = childs; Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); publisher.Publish(); } } }
private void PublishItem(Item item) { Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(item.Database, Database.GetDatabase(publishDB), Sitecore.Publishing.PublishMode.SingleItem, item.Language, System.DateTime.Now); // Create a publisher with the publishoptions Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); // Choose where to publish from publisher.Options.RootItem = item; // Publish children as well? publisher.Options.Deep = true; // Do the publish! publisher.Publish(); }
private void PublishItem(Item item) { try { Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(item.Database, Database.GetDatabase(ModuleUtilsConstant.WebDatabase), Sitecore.Publishing.PublishMode.SingleItem, item.Language, System.DateTime.Now); Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); publisher.Options.RootItem = item; publisher.Options.Mode = Sitecore.Publishing.PublishMode.Full; publisher.Publish(); } catch (System.Exception ex) { Sitecore.Diagnostics.Log.Error($"Failed to publish item {item.ID}", ex, this); } }
public void CreateComment(CivilCommentsSectionViewModel model) { using (new Sitecore.SecurityModel.SecurityDisabler()) { Database masterDb = Sitecore.Configuration.Factory.GetDatabase("master"); var dataSource = model.DatasourceItem; dataSource = masterDb.GetItem(dataSource.ID); var commentsFolder = masterDb.GetItem("{A0660FA6-D500-453A-A807-4FFE7997F83C}"); if (commentsFolder == null) { return; } TemplateItem template = masterDb.GetTemplate("{F0A8C4E9-FEE1-407E-99DB-A7F2D16024D1}"); var newComment = commentsFolder.Add(String.Format("Comment {0}", DateTime.Now.ToString("g")).SanitizeToItemName(), template); var user = Sitecore.Context.User; var username = user == null ? "anon" : user.Name; if (newComment == null) { return; } newComment.Editing.BeginEdit(); newComment.Fields["Comment"].Value = model.Comment; newComment.Fields["Username"].Value = username; newComment.Editing.EndEdit(); Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(newComment.Database, Database.GetDatabase("web"), Sitecore.Publishing.PublishMode.SingleItem, newComment.Language, System.DateTime.Now); // Create a publisher with the publishoptions Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); publisher.Options.RootItem = newComment; publisher.Publish(); } }
/// <summary> /// Publishing this item to 'web' database /// </summary> /// <param name="item">item to be published</param> private void PublishItem(Sitecore.Data.Items.Item item) { // The publishOptions determine the source and target database, // the publish mode and language, and the publish date Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(item.Database, Database.GetDatabase("web"), Sitecore.Publishing.PublishMode.SingleItem, item.Language, System.DateTime.Now); // Create a publisher with the publishoptions Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); // Choose where to publish from publisher.Options.RootItem = item; // Publish children as well? publisher.Options.Deep = false; // Do the publish! publisher.Publish(); }
private void ExecutePublishItem(string sourceDBName, string targetDBName, Sitecore.Data.Items.Item rootItem, bool withChildren, bool isRepubish) { try { using (new UserSwitcher(@"sitecore\admin", true)) { var sourceDB = Database.GetDatabase(sourceDBName); var targetDB = Database.GetDatabase(targetDBName); Sitecore.Publishing.PublishOptions publishOptions = null; if (!isRepubish) { publishOptions = new Sitecore.Publishing.PublishOptions(sourceDB, targetDB, Sitecore.Publishing.PublishMode.Smart, rootItem.Language, System.DateTime.Now); } else { publishOptions = new Sitecore.Publishing.PublishOptions(sourceDB, targetDB, Sitecore.Publishing.PublishMode.Full, rootItem.Language, System.DateTime.Now); } Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); publisher.Options.RootItem = rootItem; publisher.Options.Deep = withChildren; publisher.Publish(); } } catch (Exception x) { Log.Error(x.Message, this); } }
public void CreateComment(string comment) { Database masterDb = Sitecore.Configuration.Factory.GetDatabase("master"); var dataSource = GetDataSourceItem(); dataSource = masterDb.GetItem(dataSource.ID); var commentsFolder = dataSource.Axes.GetDescendants().FirstOrDefault(x => x.TemplateName == "Comments Folder"); if (commentsFolder == null) { return; } TemplateItem template = masterDb.GetTemplate("{F0A8C4E9-FEE1-407E-99DB-A7F2D16024D1}"); var newComment = commentsFolder.Add(String.Format("Comment {0}", DateTime.Now.ToString("g")).SanitizeToItemName(), template); var user = Sitecore.Context.User; var username = user == null ? "anon" : user.Name; if (newComment == null) { return; } newComment.Editing.BeginEdit(); newComment.Fields["Comment"].Value = comment; newComment.Fields["Username"].Value = username; newComment.Editing.EndEdit(); Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(newComment.Database, Database.GetDatabase("web"), Sitecore.Publishing.PublishMode.SingleItem, newComment.Language, System.DateTime.Now); // Create a publisher with the publishoptions Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); publisher.Options.RootItem = newComment; publisher.Publish(); }
protected void Unnamed_Click(object sender, EventArgs e) { var db = Sitecore.Configuration.Factory.GetDatabase("master"); var homeItem = db.GetItem(new Sitecore.Data.ID("{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}")); // duplicate new home item var newHomeItem = homeItem.Duplicate(homeItem.DisplayName + countryCode.Text); if (UpdateRendering(db, homeItem, newHomeItem) && UpdateConfig(newHomeItem) && UpdateIISSite()) { if (IsPublish.Checked) { Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(newHomeItem.Database, Database.GetDatabase("web"), Sitecore.Publishing.PublishMode.Smart, newHomeItem.Language, System.DateTime.Now); // Create a publisher with the publishoptions Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); // Choose where to publish from publisher.Options.RootItem = newHomeItem; // Publish children as well? publisher.Options.Deep = true; // Do the publish! publisher.Publish(); SetMesasgeInCookie("Done", 1); } else { SetMesasgeInCookie("Done. Please publish new site", 1); } Response.Redirect("http://sc8mvc/duplicateitems.aspx"); } }
/// <summary> /// Publish the item and recursivley any ancestors that haven't yet been published /// </summary> /// <param name="item">The item to publish</param> public static void PublishItemAndRequiredAncestors(Item item, Database targetDatabase) { if (item != null) { var ancestorInTarget = targetDatabase.GetItem(item.ParentID); if (ancestorInTarget == null) { PublishItemAndRequiredAncestors(item.Parent, targetDatabase); } ancestorInTarget = targetDatabase.GetItem(item.ParentID); if (ancestorInTarget != null) { foreach (Sitecore.Globalization.Language language in item.Database.Languages) { Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(item.Database, targetDatabase, Sitecore.Publishing.PublishMode.SingleItem, language, DateTime.Now); publishOptions.RootItem = item; publishOptions.Deep = false; Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); publisher.Publish(); } } } }
public string publish() { using (new Sitecore.SecurityModel.SecurityDisabler()) { Sitecore.Data.Database master = Sitecore.Data.Database.GetDatabase("master"); Sitecore.Data.Database web = Sitecore.Data.Database.GetDatabase("web"); string mode = req.Params["mode"]; if (mode == "") { mode = "site"; } Sitecore.Publishing.PublishMode publishMode = Sitecore.Publishing.PublishMode.Incremental; if (mode == "item") { publishMode = Sitecore.Publishing.PublishMode.SingleItem; } var options = new Sitecore.Publishing.PublishOptions(master, web, publishMode, Sitecore.Globalization.Language.Parse("en"), System.DateTime.Now); string id = req.Params["id"]; //return id; if (mode == "item" && id != "") { //return "I have an item and an ID of " + id; Sitecore.Data.ID itemID = Sitecore.Data.ID.Parse(id); options.RootItem = master.GetItem(itemID); } //return options.RootItem.Name; var publisher = new Sitecore.Publishing.Publisher(options); //publisher.Options.RootItem = master.GetItem("/sitecore/content/employee/home"); publisher.Options.Deep = true; publisher.PublishAsync(); } return "yo!"; }
/// <summary> /// Function taken from http://briancaos.wordpress.com/2011/01/14/create-and-publish-items-in-sitecore/ /// </summary> /// <param name="item"></param> private void PublishItem(Sitecore.Data.Items.Item item) { // The publishOptions determine the source and target database, // the publish mode and language, and the publish date Sitecore.Publishing.PublishOptions publishOptions = new Sitecore.Publishing.PublishOptions(item.Database, Sitecore.Data.Database.GetDatabase("web"), Sitecore.Publishing.PublishMode.SingleItem, item.Language, System.DateTime.Now); // Create a publisher with the publishoptions Sitecore.Publishing.Publisher publisher = new Sitecore.Publishing.Publisher(publishOptions); // Choose where to publish from publisher.Options.RootItem = item; // Publish children as well? publisher.Options.Deep = true; // Do the publish! publisher.Publish(); }
public bool publish() { Sitecore.Data.Database master = Sitecore.Data.Database.GetDatabase("master"); Sitecore.Data.Database web = Sitecore.Data.Database.GetDatabase("web"); var options = new Sitecore.Publishing.PublishOptions(master, web, Sitecore.Publishing.PublishMode.Smart, Sitecore.Globalization.Language.Parse("en"), System.DateTime.Now); var publisher = new Sitecore.Publishing.Publisher(options); publisher.Options.RootItem = master.GetItem("/sitecore/content/home"); publisher.Options.Deep = true; publisher.Publish(); return true; }
// 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); } }