Publish Single Items

Publish Single Items

Code that publishes or imports data typically accesses the Master database by name rather than referencing the context database
  1. Create an object for the database where the item is located - replace SitecoreDB with the name of a Sitecore database, such as “master”
  2. Create an object for the target database of the publish operation - replace targetDB with the name of a Sitecore database, such as “web”
  3. Use the context language to create an object for the language of the publish operation
  4. Create an object for the time of the publish operation and specify now
  5. Create an object for the publish options and specify the previously created objects
  6. Activate the publish pipeline to invoke the publish operation
Alternatively:
  1. Set publish options
  2. Get the publish target
  3. Iterate through the /sitecore/system/publishing targets children
  4. Define the source and target databases
  5. Create a publisher
  6. Publish in AsyncMode
Third Option:
  1. Create an object of type DateTime for specifying the publish time and set it to the current time
  2. Create an object for the database where the item is located - replace SitecoreDB with the name of a Sitecore database, such as “master”
  3. Create an object for the target database of the publish operation - replace targetDB with the name of a Sitecore database, such as “web”
  4. Create an object for the publish options and specify the previously created objects
  5. Create an object for the target item in the database- replace itempath with the relative path to the item in the Content Tree, such as “home/about/item1” or start the path with a backslash (/) to indicate an absolute path, such as “/sitecore/content/home/about/item1”– a null value will be set in either case if the item does not exist - Sitecore does compare case when evaluating item paths

C# Web Control Code

Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("SitecoreDB");

Sitecore.Data.Database targetDB = Sitecore.Configuration.Factory.GetDatabase("TargetDB");

Language language = Sitecore.Context.Language;

DateTime publishTime = DateTime.Now;

Sitecore.Publishing.PublishOptions options = new PublishOptions(db, targetDB, PublishMode.SingleItem, language, publishTime);

Sitecore.Publishing.Pipelines.PublishItem.PublishItemPipeline.Run(item.ID, options);

 

OR

 

Sitecore.Publishing.PublishOptions options = new PublishOptions(PublishMode.Full, Sitecore.Globalization.Language.DefaultLanguage, DateTime.Now);
Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("SitecoreDB"); 
options.RootItem = sourceDatabase.Items[ “/sitecore/content” ];
string targetDB = "web";
Database targetDatabase = Factory.GetDatabase(targetDB);
options.SourceDatabase = sourceDatabase;
options.TargetDatabase = targetDatabase;
Sitecore.Publishing.Publisher pub = new Sitecore.Publishing.Publisher(options);
Sitecore.Jobs.Job job = pub.PublishAsync( );
job.Start();

 

OR

DateTime publishTime = DateTime.Now;

Sitecore.Data.Database db = Sitecore.Configuration.Factory.GetDatabase("SitecoreDB");

Sitecore.Data.Database targetDB = Sitecore.Configuration.Factory.GetDatabase("targetDB");

Sitecore.Publishing.PublishOptions options = new Sitecore.Publishing.PublishOptions(db, targetDB, Sitecore.Publishing.PublishMode.Full, Sitecore.Data.Managers.LanguageManager.GetLanguage("en", master), publishTime);

options.Deep = true;

string id = db.Items.[“itempath”] .ID.ToString();

if (!string.IsNullOrEmpty(id))

      {

             var id = new ID(id);

             options.RootItem = db.Items[id];

             Sitecore.Publishing.Publisher pub = new Sitecore.Publishing.Publisher(options);

            Sitecore.Jobs.Job job = pub.PublishAsync();

             job.Start();

      }