fixing end of string expected at position error in Sitecore

Recently while verifying some of the components in Experience Editor, I observed that while adding datasource to the renderings, it was throwing “End of string expected at position 75” error.

I checked following things to troubleshoot the issue:

  1. If Datasource location specified in the rendering exists or not, and it exists.
  2. If Datasource template is defined in rendering or not, and it was there.
  3. Check the errors in log file, and there was an error there, please see the below screen shot what error it was.log
  4. Following error was visible in front end while adding datasource.end-of-string-error

 

It turns out Sitecore doesn’t like hyphens “-” in query path, and few other which includes:

  1. If you query contains keyword “and
  2. If query contains keyword “or
  3. If query contains hyphen “

In my case when i checked the query and it contains keyword “and“, which was basically failing and not allowing me to add  datasource from Experience editor.

Solution:

In order to fix this issue we can add escape character “#” before and after of the items that contains any of these keywords or hyphen “-“, so, for example:

Before : query:./ancestor::*[@@templatename=’SitecoreRoot’]/Global//Modules/Image and Media Module

After : query:./ancestor::*[@@templatename=’SitecoreRoot’]/Global//Modules/#Image and Media Module#

After making this change, the issue was fixed.

It seems this issue was there in Sitecore 6, but it can be replicated in Sitecore 8.1 (rev 160302) as well.

I have raised this with Sitecore support team, and they have registered this as Wish/Feature, so, we may see this issue resolved in coming product versions.

I hope this helps somebody.

Happy learning 🙂

http://sdn.sitecore.net/Reference/Using%20Sitecore%20Query/Sitecore%20Query%20Syntax.aspx

 

Test changes without publishing to web DB in Sitecore

In your development environment, did you ever felt that publishing is not providing a great value, but acting like a hindrance?

PublishItem

In Sitecore you can directly test your changes without publishing your changes to web DB, and configure Sitecore to use content from master DB.
This can be done in two ways:

  1. If you don’t have any custom Site configuration config file exist, please go to App_Config/Include/LiveMode.config.example file and rename it to LiveMode.config or
  2. If you have custom Site configuration file exists, you can change the database value to “master” from “web”.

Please make sure to revert your changes, before deploying the solution to QA or production environment.

Happy learning 🙂

 

Securing Sitecore connection strings

There are scenarios where we don’t want to compromise with the security of our application resources, this includes Connectionstring.config file as example, If someone compromised CD servers, they will have access to Connection string file and can use the details in a wrong way.

There are ways we can secure the connection string in Sitecore, in this blog post I would like to give an example of one of the approach which can be used:

Please see the sample code snippet, to encrypt and decrypt  connection string file.

Configuration config = WebConfigurationManager.OpenWebConfiguration(“~”);

// Get the connectionStrings section.
ConnectionStringsSection section = config.GetSection(“connectionStrings”) as ConnectionStringsSection;
if (section.SectionInformation.IsProtected)
{
section.SectionInformation.UnprotectSection();
}
else
{
section.SectionInformation.ProtectSection(“DataProtectionConfigurationProvider”);
}
// Save changes to the Web.config file.
config.Save();

Few points to note and consider:

  • This approach uses System.Web.Configuration namespace to work.
  • We access application’s web.config file via OpenWebConfiguration method.
  • We access connectionStrings section of the web.config file via GetSection method.
  • Encrypt and decrypt the sections.
  • Save the changes.

This is how the connection string.config file looks before encryption:

<?xml version=”1.0″ encoding=”utf-8″?>
<connectionStrings>
<!–
Sitecore connection strings.
All database connections for Sitecore are configured here.
–>
<add name=”core” connectionString=”user id=user;password=password;Data Source=(server);Database=Sitecore_Core” />
<add name=”master” connectionString=”user id=user;password=password;Data Source=(server);Database=Sitecore_Master” />
<add name=”web” connectionString=”user id=user;password=password;Data Source=(server);Database=Sitecore_Web” />
<add name=”analytics” connectionString=”mongodb://localhost/analytics” />
<add name=”tracking.live” connectionString=”mongodb://localhost/tracking_live” />
<add name=”tracking.history” connectionString=”mongodb://localhost/tracking_history” />
<add name=”tracking.contact” connectionString=”mongodb://localhost/tracking_contact” />
<add name=”reporting” connectionString=”user id=user;password=password;Data Source=(server);Database=Sitecore_Analytics” />
</connectionStrings>

After encryption, this is how the file looks:

<?xml version=”1.0″ encoding=”utf-8″?>
<connectionStrings configProtectionProvider=”DataProtectionConfigurationProvider”>
<EncryptedData>
<CipherData>
<CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAA2ivFsj7cNEaM7QEbCTKmIwQAAAACAAAAAAAQZgAAAAEAACAAAAAhdGbhIMVnCuLPqxeQN8XGwAwUQAAJxH+/HDKbuXL70AAAAAAOgAAAAAIAACAAAAA7VJJIQ1/LGK2vEPieVV4MAh7sFiLEBf0rbHNQOqObocAKAAA+aDLJMlW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u/7ArAZJUJ7D8aLreeNsI3d48bIO/j/GtAdCLTN6sBo21R8B8tVv0qbbic83D8P8EDodHEJvxHUtEPBYjpFgloek+ToPnHH7aHUqSXtFH5EI1bbp2zNdVpwB4kJ9beC6Y8foR81NAKVlNjeCSVmrW2+ZUw19CSMLiiMh8DCG5jQvZ5ejBqmIGpHYZEIc5yQLxb5AlSbYeNEH6MsCh6mhpBfWXbTKJxUhZvFuaBh0+UgI0sRB4OQ2vJ0TtgRRnCiFnOlszBqiiEH5MbAFwWtFS/mNWPFi76HNN3YF2sL4QtrlqLw6TcVuGIRCqWkZasb7hdwdMarV0A8fCx996PwMsWPdQ8StVjXqz567yzev5d1T3tbSrzdo8HgLwIAIdLQOxP1JnNlnLqwXW0k37CFZyUjwWV3ZfiXJN1tIolul1ogf5modPF3YB0aS4FQAAAAB6C3jSoZh6M0ltHxa/HTqQUTSQexkdlI/5baIOQQx/dmJEoTWBa/D2JTHAwYhTQa/psamG03lzeKsm0jetzACw=</CipherValue>
</CipherData>
</EncryptedData>
</connectionStrings>

If we want to decrypt the section, we can use UnprotectSection()  and can review the connections, once we are good we can again encrypt the setting using ProtectSection() method.

We can also secure the setting using aspnet_regiis.exe tool.

I hope this helps someone, who is looking for something similar, please let me know for any questions on this, happy to discuss more.

Happy learning 🙂

MongoDB authentication in Sitecore

Securing application data is critical for any client and business, and the same principle applies to Sitecore application as well.

One of the most important component of Sitecore is MongoDB, which is where we store all Experience(xDB) related data, MongoDB was shipped into Sitecore’s ecosystem from Sitecore 7.5, and it’s very important to make sure the data stored in xDB is secured, and only authorized uses has access to the data.

Recently, we heard about thousands of MongoDB data being hacked, so what’s the reason behind it? any guess? it’s simple- all those DBs were not configured to be secure and anyone can access it.

It would be great if MongoDB installation itself comes with an option, where we can secure our data while installing it, like how we do it for SQL.

Even though, we can go back and secure the data  by setting up users/roles and permissions, but it’s always great to do it it first place.

We also have to see and make sure that Connection string used for MongoDB is all protected with credentials, so that only authorized users can access it.

As part of this blog post, i would like to cover the steps which we can follow and make our Sitecore application more secure.

  1. Create MongoDB User
    1. Follow the below command to create the user.
    2. db.createUser({user: “mongoadmin”,pwd: “mongoadmin”,roles: [ { role: “userAdminAnyDatabase”, db:”admin” },{ role: “root”, db:”admin” }]})
    3. We just created a new mongouser- “mongoadmin” under database “admin”, and has given all rights to this user by assigning role “root“.
  2. Verifying the User
    1. If we want to verify that user gets created or not, we can use the following command for the same:
    2. db.auth(“mongoadmin”,”mongoadmin”)
    3. This should return 1, if the user is authenticated.
    4. We can also see the list of all users by running following command:
    5. db.getUsers()
  3. Assigning specific roles to unique collections
    1. We shouldn’t be giving “root” level access to the user, and it should be more specific to the database and collection.
    2. For example: we can give read and write access to analytics database in Sitecore.
    3. In order to do that, please login to mongo shell and switch to admin.
    4. use analytics
      db.createUser({user: “mongouser”,pwd: “mongopassword”,roles: [ { role: “readWrite“, db:”analytics” }]})

    5. In this case we created a new user called “mongouser” and assigned “readWrite” role to it, and is specific to “analytics” database.
    6. In the same way, we can do it for other three databases also.
  4. Connection string updates
    1. This is how the default connection string looks like:
    2. <add name=”analytics” connectionString=”mongodb://localhost:27017/sample_analytics” />
      <add name=”tracking.live” connectionString=”mongodb://localhost:27017/sample_tracking_live” />
      <add name=”tracking.history” connectionString=”mongodb://localhost:27017/sample_tracking_history” />
      <add name=”tracking.contact” connectionString=”mongodb://localhost:27017/sample_tracking_contact” />

    3. After making updates to connection string, and adding required username and password details to it, this is how it looks:
    4. <add name=”analytics” connectionString=”mongodb://mongoadmin:mongoadmin@localhost:27017/sample_analytics?authSource=admin” />
      <add name=”tracking.live” connectionString=”mongodb://mongoadmin:mongoadmin@localhost:27017/sample_tracking_live?authSource=admin” />
      <add name=”tracking.history” connectionString=”mongodb://mongoadmin:mongoadmin@localhost:27017/sample_tracking_history?authSource=admin” />
      <add name=”tracking.contact” connectionString=”mongodb://mongoadmin:mongoadmin@localhost:27017/sample_tracking_contact?authSource=admin” />

    5. This is how the format looks like:
    6. mongodb//[username:password@]host[:port]/database?authSource

If we are trying to access MongoDB without passing valid credentials, we get this error in the log, please see the screen shot for ref:

MongoAuthenticationFailed

Once we pass the valid credentials, this error will go off.

It’s always a good practice to authenticate MongoDB in local environment as well, this helps us in setting the habit for it and we can uncover any issues well in advance.

I hope this helps in getting the understanding about how we can secure and authenticate MongoDB, and how to create users/permissions for the same.

There is a great article in MongoDB documentation, around setting up auth for Mongo and setting up users, creating roles for the same, please consider reviewing this as well, this is great source of information.

https://docs.mongodb.com/manual/tutorial/enable-authentication/

Thanks, and please let me know for any questions, happy to discuss more.

Happy learning 🙂

Changing data directory for MongoDB in Sitecore

As part of my MongoDB Blog series part 1, we discussed how we can Install MongoDB and we used “C” drive as default Installation location for it.

But as a best practice and considering scalable environment, we shouldn’t be using “C” drive to store any application data, it should be reserved only for system files, so that if we need to upgrade the OS or need to repair existing Installation, our application data is still safe.

We have done this for one of the implementation, and it’s always better to do this in early phase of the development.

We can always go back and change the default data directory location for MongoDB in Sitecore, and can use mongo config file which we created for Installing MongoDB.(Please refer MongoDB Blog series part-1 for more details)

This is how it was before:

mongoconfig

Let’s use another drive to store data and logs for MongoDB, in this case it’s “G

mongoconfigrevised

From the above screenshot we can see that, the data and log folder points to “G” drive now, and rest of the configuration is all same.

If you Installed MongoDB as a service, your service still points to “C” drive, but your data and log will be stored in “G” drive.

If you want to copy over existing data from “C” to “G” (in this example), make sure to stop your service, copy your data from “C” to “G”(in this example) and restart MongoDB Service.

This is good from maintenance perspective, and easy to manage afterwards.

I hope this helps someone, who is looking for something similar.

Thanks, and please let me know for any questions and any feedback, happy to discuss more.

Happy learning 🙂

Sitecore MongoDB Blog series: Part 3-Creating custom contact facets

In previous blog post, we gone through introduction of MongoDB with Sitecore which includes scaling, contacts and understanding MongoDB queries.

In this blog post we will go through and understand, how to create a custom contact facet and how to deploy it to Sitecore.

By default there are facets which Sitecore uses, and you can find the details here:

\App_Config\Include\Sitecore.Analytics.Model.Config

<entities>
<contact>
<factory type=”Sitecore.Analytics.Data.ContactFactory, Sitecore.Analytics” singleInstance=”true” />
<template type=”Sitecore.Analytics.Data.ContactTemplateFactory, Sitecore.Analytics” singleInstance=”true” />
<facets>
<facet name=”Personal” contract=”Sitecore.Analytics.Model.Entities.IContactPersonalInfo, Sitecore.Analytics.Model” />
<facet name=”Addresses” contract=”Sitecore.Analytics.Model.Entities.IContactAddresses, Sitecore.Analytics.Model” />
<facet name=”Emails” contract=”Sitecore.Analytics.Model.Entities.IContactEmailAddresses, Sitecore.Analytics.Model” />
<facet name=”Phone Numbers” contract=”Sitecore.Analytics.Model.Entities.IContactPhoneNumbers, Sitecore.Analytics.Model” />
<facet name=”Picture” contract=”Sitecore.Analytics.Model.Entities.IContactPicture, Sitecore.Analytics.Model” />
<facet name=”Communication Profile” contract=”Sitecore.Analytics.Model.Entities.IContactCommunicationProfile, Sitecore.Analytics.Model” />
<facet name=”Preferences” contract=”Sitecore.Analytics.Model.Entities.IContactPreferences, Sitecore.Analytics.Model” />
</facets>
</contact>
</entities>

In this specific example, we will show how to add a custom string value to existing contact card, we can call this Facet “Education

In order to create a custom Facet, we need following components:

  1. Interface (that’s used to create a contract/facet)
  2. Implementation
  3. Configuring the system to use new Facet.

Interface:

Education is the Facet, and the Education property we define for this is Element for this Facet,so we need to create Facet and Element interface.

Here is IProfileEducationFacet Interface, this should inherit IFacet.

IProfileFacet

Next step is to create IProfileEducationElement Interface, here is the sample snippet for the same:

IProfileEducationElement

Implementation:

Once we have Interfaces created for Facet and Element, next step will be to create class that can implement those interfaces.

ProfileEducationFacet class

ProfileFacet

ProfileEducationElement class

ProfileEducationElement

 

Once the Facets and Elements are created, next step will be to register these Facets and Elements in Sitecore, we also call this deploying to Sitecore.

In order to deploy these facets and Elements, we can either update the default configuration file, or we can also create a patch file, which will have changes specific to Education Facets.

Here is the patch file for ref:

FacetDeployPatch

<configuration xmlns:patch=”http://www.sitecore.net/xmlconfig/”&gt;
<sitecore>
<model>
<elements>
<element patch:after=”*[@interface=’Sitecore.Analytics.Model.Entities.IBehaviorProfileValue, Sitecore.Analytics.Model’]” interface=”Sample._Classes.xDBProfile.Elements.IProfileEducationElement, Sample.Sitecore”
implementation=”Sample._Classes.xDBProfile.Elements.ProfileEducationElement, Sample.Sitecore” />
<element patch:after=”*[@interface=’Sitecore.Analytics.Model.Entities.IBehaviorProfileValue, Sitecore.Analytics.Model’]” interface=”Sample._Classes.xDBProfile.Facets.IProfileEducationFacet, Sample.Sitecore”
implementation=”Sample._Classes.xDBProfile.Facets.ProfileEducationFacet, Sample.Sitecore” />
</elements>
<entities>
<contact>
<facets>
<facet patch:after=”*[@name=’Preferences’]” name=”Education” contract=”Sample._Classes.xDBProfile.Facets.IProfileEducationFacet, Sample.Sitecore” />
</facets>
</contact>
</entities>
</model>
</sitecore>
</configuration>

Getting and setting properties are done using GetAttribute and SetAttribute methods retrieved from Sitecore.Analytics.Model.Framework.Element and Sitecore.Analytics.Model.Framework.Facet.

Happy learning 🙂

Understanding Clones in Sitecore

There are scenarios when we want to replicate/duplicate entire content tree/individual items and create another structure in same Sitecore Instance, we also want to make sure that content can be shared from parent item to new item, in these scenarios and cases we can make use of Sitecore Cloning which helps to achieve the same.

Clone:

A clone is an item that is not just a copy of the original item, but one that inherits the field values from the original item. If we update a field in the original item, the corresponding field in the clone is also updated, this helps in implementing content sharing across sites/items in the same Sitecore Instance.

What can be cloned:

Here are the following items which can be considered for cloning:

  1. Complete site
  2. Section of site
  3. Individual Item

How to clone an Item:

In order to create a clone, please follow the following steps:

  1. Select the item which you want to clone from content tree.
  2. From the configure tab, click Cloneclone-tab
  3. From Clone Item dialog box, please select the location where you want to create the cloned item.
  4. Clone-Target
  5. Click Clone.
  6. Now go to the cloned item, and you will observe the “original value” next to the field, which explains that value for this field is inherited from parent/original item.clone-original-field-values

How to Unclone an Item:

In order to Unclone an item in Sitecore, please follow the below steps:

  1. Select the Item from content tree which you want to Unclone.
  2. Click on Unclone, from the configure tab.Unclone-tab
  3. Once we Unclone an Item, this item becomes an individual item which can be managed separately.

Breaking the Inheritance for Cloned Item:

As i mentioned above that we can Unclone an item, by which that item becomes a new item which can be managed separately.

But, we can also break the inheritance on field level, in order to break inheritance on the field level, just go to clone item and update any specific field value.

Once we update specific field value manually for any clone item, that field won’t share/inherit the value from parent/original item.

This is helpful in cases where certain field values to be shared from parent item, and certain field values managed separately in clone item.

Unclone-field value

In the above screen shot we can see that “Teaser” doesn’t show any “original value” text because the value was updated directly in the cloned item, but “Main Content” still inherits it from parent item.

Few points to note:

  • Before we take a decision what item/section should be clone, we need to do understand our content strategy.
  • For example- If we have to create a microsite which should be based on parent site, we can go for cloning, but, we also have to understand what are the different sections that needs to share the content, if only certain pages needs to be shared, we don’t have to clone the entire content tree(site), rather we can just clone specific pages/items.
  • Another example can be navigation, we want the navigation to be same for all the sites, but content differs, in this case we can just clone Navigation Items and not pages/items.
  • We can make use of field value inheritance, if the content of the clones items needs to be different, we can break the inheritance and add specific content to cloned item.

I hope this will help someone who is trying to do understand how clones work in sitecore, please let me know if you have any feedback or questions, happy to help.

Happy learning 🙂

Content managed favicon in Multisite Sitecore solution

There are scenarios when we want to configure content managed favicons for multiple sites in the same instance of Sitecore.

I have implemented this for one of our client, so thought of sharing the same with the community.

In my case, we have separate Site settings node created, which has all site related information, from the below screen shot you can see we created a new field called “Fav Icon” which is of file type.

FavIconField

Once we have the field available, content authors can content managed the fav icon based on the site requirement, please see the screen shot for ref:

Favicon

In the below code snippet, we are trying to fetch the sitenode based on start node(home node), and once we have sitenode, we are fetching site settings node from where we can get “Fav Icon” Url value.

 public static Item ContextWebsiteRoot
 {
   get
   {
     Item contextItem = null;
     Item webSiteContext = null;
     if (Sitecore.Context.Site != null && Sitecore.Context.Database != null)
     {
        Item homeNode = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.StartPath);
        webSiteContext = contextItem.Parent;
        // Use webSiteContext to get "Fav Icon" url...
        // We don't have to use .Parent, if "Fav Icon" field is created directly 
        // under home node.
     }
     return webSiteContext;
   }
 }

In my case we don’t have the Fav Icon in the site home node, so we need to add some rule to get the correct item, and then reading “Fav Icon” field, but just in case, if you are trying to add “Fav Icon” field directly in home node, you don’t need to fetch any parent Item.

Here is the simplified version of it, when “Fav Icon” is added to home node.

 Item contextItem = null;
 Item webSiteContext = null;
 if (Sitecore.Context.Site != null && Sitecore.Context.Database != null)
   {
      Item homeNode = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.StartPath);
      var siteFavIcon= contextItem["Fav Icon"];
   }

 

I hope this helps someone who is trying to implement something similar.

Happy learning 🙂

Sitecore best practice and guidelines

There are several useful articles and blogs about #Sitecore best practices, but I am trying to put together my learning on this area. Sitecore has different components shipped within it i.e “analytics“, “workflows“,”personalization” and others.

Before starting or working on any application, our main focus should be to understand application as a whole and not just part of it, believe me this is a key for overall success of your implementation, try to get involved and get more and more information about the solution as a whole.Once you have a big picture in mind, it’s easy to work on small features and then for integrations.

There are several categories we can have, when we talk about Sitecore best practices:

  1. Template.
  2. Content structure.
  3. Presentation.

Templates: Templates are the base for any Sitecore implementation, templates are grouping of certain fields that make one entity/table in Sitecore, which can have one or more then one field(s). There are few things which we should consider while designing a template.

  • Think about the entity first and all related fields which can be grouped.
  • List down all the fields which needs to be added to a particular entity.
  • Now identity if there are any set of fields which are common to other entities as well, at least to few, if not all, like “Headline”,”Teaser”, “Main Content” or “Image” fields.
  • If you get a list of common fields, then create a new “Base Template” and add all those fields in that template, this is also to make sure we are reusing the template and fields as much as possible.
  • You can use the “Display Name” property to provide a user-friendly name for any item.
  • Always try to group fields in section, this is logic separation of fields, still all the fields comes under same template only.
  • Try to give user friendly name for all sections,fields and templates so that business users/content authors can identify and understand them easily.
  • Always set default values in standard values of template, so that we can prepopulate some of the field values.
  • If there is predefined structure we have to follow, we should always consider branching template, this really helps content authors and they will love you for this.
  • Try to have unique icon added to you template, so that it is easy to recognize.
  • Presentation should be configured in standard values, so that it can be applied to all instances of that template.
  • Creation of “Base Templates” should always be in your radar, so that you can make use of Sitecore inheritance where required.

Content structure: This is the second step where we create items based on the template created, content structure helps content authors to look for the information or details they are interested, here are some of the things that we should consider while creating/designing content structure.

  • As a standard, try not to have more than 100 items under particular node, can be folder or page item, this can impact Sitecore performance.
  • If expecting more than 100, then consider using buckets or if not buckets create folders in a way that it doesn’t exceed more then 100 child per folder/item.
  • Try to have only page items created under home page, this can change, you can read my blog on different options for configuring content tree https://ankitjoshi2409.wordpress.com/2017/02/07/structuring-and-organizing-sitecore-content-in-content-tree/
  • Plan content structure based on site map, place all the items which are accessed using URL as descendants of the website, we should mimic sitemap structure in sitemap as well.
  • Make sure we have security added for items which which should not be accessed by specific user role(s).
  • Indexes should be configured for automatic updates,for better and faster content editor experience.
  • Make sure to consider multi-site environment as well, and create folder accordingly, this also helps in content sharing from one site to another.
  • Try to create folder template for structuring page items, should try for it as much as possible so that we have more control on the tree.
  • Maintain only a few versions of each item in the implementation- this is to improve the performance.

Presentation : Some of the things to consider while setting up the presentation:

  • Page should be Experience editor friendly.
  • Make sure to use Experience editor friendly tags like
  • Sublayout(s) and Rendering(s) should be configured to be cachable for better browsing experience.
  • Rendering should be added to page template standard values,when applied at an item level, any changes to the layouts require a lot of effort to make changes to all items. When applied at standard values, such modifications become easier.
  • Any updates to the specific should be done directly on final layouts, please read my blog for more details- https://ankitjoshi2409.wordpress.com/2017/02/06/sitecore-shared-vs-final-layouts/
  • Rendering should have data source template and data source location defined, this helps personalizing the site and making sure the application is experience editor friendly, this is important configuration for Experience editor.
  • All the components/rendering(s) should have thumbnail image added to it, this helps CAs configuring dealing experience editor nicely.
  • Use placeholders settings and allowed control setting, so that CAs can add/create only those modules which are required for that page design to work.

General Practice:

  • Don’t allow a single template to contain two fields with the same name.
  • Use a source code management system to manage all code,this includes JavaScript and CSS as well.
  • Avoid hard-coding as much as possible so that we don’t have to update the code, if any change required.
  • Use IDs instead of paths/name whenever possible.
  • Avoid using any form of the descendant axix, like GetDescendents
  • Try not to modify Sitecore base files and use Sitecore patching for any updates to config files, this helps in upgrade process if required.
  • Content Editing should be done on the master database, and then pushed to web database.
  • Restrict access to content delivery servers.
  • Use the general best practices of developments applicable to C# with tools like Re-sharper.
  • Set up continuous integration.

As I mentioned before there are lot of articles that has more details on best practices and guidelines, please make sure to read those as well.

I hope this will give you some degree of information, which can be applied while designing any structuring your Sitecore site.

Please feel free to add feedback and comments, happy to discuss more.

Happy learning 🙂