Create Items in Master DB from CD Environment

When you deploy your website to production, you have to configure CM and CDs servers,  the infrastructure will look like this:

cm.jpg

As we see in the diagram, CM Server will have access to Master, Core and Web while the CDs Servers will have access only to the Pub Database (Live one), and Core. and in CDs configuration you should not have any access to Master database.

so if you have written a code to create items on your development environment, this will not work on the production environment, this is something that you need to take in your consideration during development, usually this happens with people who haven’t done any Sitecore deployment before,  the question here:

Why I have to do that and what is the best way to achieve this?

There are too many reasons that requires creating items in Master DB from CD server for example:

  1.  You have blog module on the Website, and you need to store comments in Sitecore for some reasons, let’s say that you need them as items to  apply workflow on these comments.
  2. Clothing company will give access to vendors to portal Site to add their own products, these products should be saved in Sitecore so when the end-users access the Site, they can see the new added products.

There are three approaches to achieve this task:

Sitecore Item Web APIs:

Manipulate the content items in Sitecore through HTTP requests, so you can create, retrieve, update and delete items from Sitecore, The API gives access to content through items paths, IDs, and Sitecore queries. This service produces output in JSON format and is both highly customizable and optimized to support a minimum
number of requests.

On CD server, sitecore services security policy is set by default to ServicesLocalOnlyPolicy, and you need to change it to ServicesOnPolicy you can find those configurations under : App_Config/include/Sitecore.Services.client.config.

for more details about Sitecore Item Web API, you can check this link

Pros

  • Development is not required.
  • Expose everything so you can create, retrieve, update and delete any item you want.

Cons

  • Changing Sitecore services security policy, this will impact the security of Sitecore.

Example: Creating item from a template using Item Web API

http://Sitename/-/item/v1/sitecore/Content/Home?
name=MyItem&template=Sample/SampleItem&sc_database=master

Create your own Custom Web APIs

This option is good, you can create your own APIs, and you can expose only what you need.

Pros

  • Control your security, you can choose any authentication method to implement this.
  • Expose only the APIs that you need.

Cons

  • Development is required.
  • Requires Access from CDs servers to the CM server.
  • There is a dependency from CD server on the CM server, so if the CM environment went down, data will not be saved.

Example: Creating item from a template and calling it from CM

Public class ApiExampleController:ServicesApiController
{
[HttpPost]
public void CreateItem()
 {
  using (new SecurityDisabler())
     {
       Database masterDb = 
       Sitecore.Configuration.Factory.GetDatabase("master");
       Item parentItem = masterDb.Items["/sitecore/content/home"];
       TemplateItem template = masterDb.GetTemplate("sample/sample item"); 
       parentItem.Add("NewItemName", template);
    }
 }
}

And using this URL from CD server to create item

http://cm-site/api/createitem

Create Custom Database (Recommended)

1. Create a custom database to store information that you need (example: products added by the vendor).
2. Added a connection string to that custom database in connectionString.config .
3. when the vendor add product, it will be stored in this custom database.
4. Create a Sitecore scheduler task to migrate the products from this table to the Sitecore content tree.

Pros

  • More secured as you don’t need to open connection from CD to CM server.

Cons

  • Development is required.

That is it 🙂

I hope you find this post helpful, If you have any additional knowledge on this subject, comments or questions, please let me know in the comments section below.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s