1

I know that adding Master DB In CD Config is not a good practice ,But if i have a scenario of Creating Sitecore Items Dynamically (Ofcourse in Master DB) and Publish it to web ,What do you recommend ?

2
  • 1
    You can use Item Web API or Sitecore Services Client depending on which Sitecore version. More info at sitecore.stackexchange.com/questions/4434/… Oct 30 '18 at 15:05
  • If this is user-generated content triggered from the CD, I would question why you are storing it in the master database and publishing out? Unless you have some sort of approval process in the way, or you need to use the experience editor/personalization/other features of Sitecore on the content, perhaps you are better off with a custom store for this data? Oct 31 '18 at 11:53
5

One solution is to utilize the Event Queue. By default, it uses the Core database and thus, presumably, is accessible from your CM and CD servers.

1) Create payload class

[DataContract]
public class ItemCreatorRemoteEvent : IHasEventName
{
    public ItemCreatorRemoteEvent(string itemName, string itemData, string eventName)
    {
        ItemName = itemName;
        ItemData = itemData;
        EventName = eventName;
    }

    // This is the custom data you will be passing between servers
    // Any serializable data type (int, string, bool, simple class) can be used
    [DataMember]
    public string ItemName { get; protected set; }

    [DataMember]
    public string ItemData { get; protected set; }

    // add as much information as you need here to pass between servers. You have a 2GB cap, so go nuts

    // This is implemented from IHasEventName and bridges the gap between the remote and local event
    [DataMember]
    public string EventName { get; protected set; }
}

2) Set Up Subscriber On All Servers

Class

public class ItemCreatorSubscriber
{
    public void Initialize(PipelineArgs args)
    {
        EventManager.Subscribe<ItemCreatorRemoteEvent>(new Action<ItemCreatorRemoteEvent>(OnGenericRemoteEvent<ItemCreatorRemoteEvent>));
    }

    private static void OnGenericRemoteEvent<TEvent>(TEvent @event) where TEvent : IHasEventName
    {
        RemoteEventArgs<TEvent> remoteEventArgs = new RemoteEventArgs<TEvent>(@event);
        Event.RaiseEvent(@event.EventName, (IPassNativeEventArgs) remoteEventArgs);
    }
}

Patch Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <pipelines>
            <initialize>
                <processor type="MyNamespace.ItemCreatorSubscriber, MyDll" method="Initialize" />
            </initialize>
        </pipelines>
    </sitecore>
</configuration>

3) Create Item Creation Code

Class

public class ItemCreatorEvent
{
    public void CreateItem(object sender, EventArgs args)
    {
        var itemCreatorArgs = args as RemoteEventArgs<ItemCreatorRemoteEvent>;
        if (itemCreatorArgs == null)
        throw new InvalidOperationException("Unexpected event args: {0}".FormatWith((object) args.GetType().FullName));

        // verify you're on the CM server (this event will fire on all servers). 
        var isCmServer = Settings.GetBoolSetting("IsCmServer");

        // Item creation code here
        if(isCmServer){
          // create your item
        }
    }
}

Patch Config

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
    <sitecore>
        <events>
            <event name="itemCreator:remote">
                <handler type="MyNamespace.ItemCreatorEvent, MyDll" method="CreateItem" />
            </event>
        </events>
    </sitecore>
</configuration>

4) Trigger The Event From a CD Server

var remoteEvent = new ItemCreatorRemoteEvent("my item name", "my item data", "itemCreator:remote");
Sitecore.Eventing.EventManager.QueueEvent<ItemCreatorRemoteEvent>(remoteEvent); // default is to run `globally`, i.e. all servers but the one it was triggered from.

You will need a bit of logic to trigger the event from a CD server- how you do so is up to you. Maybe a form submit action?

The main things to focus on are the item payload (ItemCreatorRemoteEvent) and the ItemCreatorEvent > CreateItem method. The payload gets serialized into the event queue table in the Core database. The CreateItem method will need to target the CM instance with a setting or something as outlined in the comments.

More detailed information here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.