2
votes

I've got a situation where I want end-users to be able to create new Sitecore items... and then immediately be able to navigate to the Item's URL. Obviously the item will have to be published... but this happens in something of a black box. Is there a way to guarantee an item has been published from Master to Web?

Alternately, I could create the Item in the Web DB... then re-create/copy a Master version at some point. But this strategy seems fraught with peril. And maybe just a bad idea in general.

Suggestions? Am I being needlessly paranoid about creating items directly in Web?

4
Why not just Preview the item, or create the item directly in Page Editor?Derek Hunziker
These users are not Sitecore authors.Bryan
if those are not authors then who ?Harsh Baid
Will it be ok to open a new tab with 'sc_site' or 'sc_database' query string in 'item:saved' event ?Harsh Baid

4 Answers

2
votes

You can't "guarantee" because publishing may be queued and won't go live instantly. Instead if you need instant access, I recommend a preview site that points to the master database:

How to Setup a Sitecore Preview Site to Review Content Before Publishing

1
votes

I'll start by saying my answer is not necessarily advisable. However, depending on your situation it should work.

If the items that users are creating always have the same template you could try creating a custom item resolver that falls back to using the Master database.

  1. Allow Sitecore to attempt to resolve the item normally.
  2. When it fails, look in the Master database.
  3. If the item is in there, make sure it has the correct template
  4. Set the found item as the context item.

Using this method, you can publish the items from Master->Web s normal, but you don't have to wait until publishing is completed to see it.

Once again, this should solve the problem, but it's up to you to weigh the risks of serving up Master DB content.

0
votes

You could create a event mapping to a class with the code to publish the item. In the code you can define any rule you want regarnding whether to publish or not.

<event name="item:saved">
   <handler type="YourType.ItemEventHandler, YourType" method="OnItemSaved" />
</event>

And the OnItemSaved would look like this: (not tested)

protected void OnItemSaved(object sender, EventArgs args)
{
    if (args == null)
    {
        return;
    }
    Item item = Event.ExtractParameter(args, 0) as Item;

    var webDb = Sitecore.Configuration.Factory.GetDatabase("web");
    var masterDb = Sitecore.Configuration.Factory.GetDatabase("master");

    foreach (Language language in masterDb.Languages)
    {
        var options = new PublishOptions(masterDb, webDb, PublishMode.SingleItem, language, DateTime.Now) 
                    { RootItem = item, Deep = true };

        var myPublisher = new Publisher(options);
        myPublisher.Publish();
    }
}

And about creating the item in the web db, I also wouldn`t go that way. It would be replaced by the default publishing process unexpectedly.

0
votes

Personally i don't like front-end user creating items in master database, i feel only content authors/editors should be the ones who create items from either content editor or page editor.

There is no guarantee the item gets published instantly, I would recommend you to store any user-created data in a separate database and on the redirect URL just read the data from this database.