Return null or String.Empty for Computed fields in Sitecore

It is quite obvious that we have to create computed fields in Sitecore, to process some of the information well before it get indexed, and not processing it while requesting for the data.

ankit-sitecore-download

There are chances when you don’t find any value to be added to you computed field, in those cases we can return either null or String.Empty().

Returning null basically tells that there is nothing to be indexed, and it won’t take any space in your index, but when we return string.Empty() , it actually creates an entry in index with no value.

Returning String.Empty() is helpful when you want to validate something based on empty string, but most of the case it is not worth.

Also, consider a case when you are dealing with several thousands Sitecore items, and if you are going to return String.Empty() for few thousands also, it will just get indexed that takes up space in you index.

We should carefully think about this, if there is a need to return String.Empty() we should do the same, but if not we should always try to return null.

Hope this helps someone.

Happy learning 🙂

Advertisements

Sitecore items mass delete through serialization

In one of the Sitecore application i worked, we had to sync large amount of data from XML, XML had several thousands of records, there was also a business rule in place which used to check certain conditions/fields before it can be inserted as item in Sitecore.

We performed several tests in local environment, before that utility can be executed in QA and other high end environment, but in this process, we have to go back and delete all existing imported items several times.

This was a time consuming process, as deleting several thousand items in Sitecore, can make your Sitecore instance slow, so, we used Sitecore Serialization to delete the items in bulk.

serialize

The Sitecore serialization functionality is designed to help teams of developers that
work on the same Sitecore solution to synchronize database changes between their
individual development environments, but is also valuable when a single developer
works on a solution.

Serialization allows you to serialize an entire Sitecore database or a series of items in
a database to text files. You can then use these text files to transfer this database or
series of items to another database or Sitecore solution.

This is particularly helpful when we use Sitecore Item buckets to structure all our content items.

Serialization option can be enabled from “Developer” ribbon.

sitecore-developer

In this example, I have created a folder called “Generic Items” and added few items under it.

serialize-s1

Follow the following steps to bulk delete the items:

  • Select the folder whose child items you want to delete, in this case, it’s “Generic Items” folder.
  • In next step, from “Developer” ribbon, click on “Serialize tree” link, this will serialize selected item and child items.
  • Serialization process will start and, it will create .item file for Generic Items folder and all child items under it.
  • Sitecore will store the .item files in data\serialization folder- in my case it’s Data\serialization\master\sitecore\content\Helix\Home\Test Eventsserialize-s2
  • Let’s assume we want to delete all items of “Generic Items” folder, delete the .item files from file system.
  • Once .items files are deleted, go back to Sitecore and from Developer ribbon click on “Revert tree” link.
  • Sitecore will start synching your items back from file system.Serialization-Sync
  • Once the process end, refresh your “Generic Items” folder, and you won’t find any child items there.
  • Sitecore serialization can delete several thousand of items in just few mins, which is way faster then manually deleting the items, which affects performance as well.

This can reduce your development and testing time, when working with large amount of data.

Please let me know if you have any questions, or want to share thoughts around this.

Happy learning 🙂

 

Improve Content Authors Experience in Sitecore

Sitecore site that is easy to use for Editors, developers and users is considered as best Implementation, we should always consider content editors while designing Sitecore, because any change afterwords can results in lot of back and forth and cost as well.

Sitecore

I would like to share my thoughts based on real time experience, and i think we should try to consider these in every project from beginning.

  • Experience Editor : Sitecore setup cannot be considered as complete or good, unless all the components are Experience Editor (Page Editor)friendly, this includes:
    1. Should be able to add components and datasource.
    2. Making sure components can be moved.
    3. Should be able to set the personalization.
    4. Should be able to configure test.
    5. Proper configuration of placeholders, which takes care of adding relevant components in proper section of the page.
    6. Edit page metadata.
    7. Editor should be able to add field values from Experience editor like (background Image, multi-list fields and etc)

A fully functional experience editor page is the first sign which shows your commitment towards client and quality of your delivery, and same time you are also making use of Sitecore at it’s best.

  •  Insert Rules: 
    1. Insert rules helps establishing information architecture of content tree, which increases consistency, with Insert rules you can restrict what content types can be added under specific location of content tree, which helps in defining scope and helps running queries as well.
    2. We can go to Configure -> Assign and set the Insert options, we also have a way to dynamically define it by using “Insert Option Rules“.
    3. Insert rules helps your content author, and guide them how and what can be added into specific location of content tree, this will help reducing any type of content error in the site, assist developers to define scope while writing programs which makes your code more stable.
  • Standard Values:  The standard values item is an item of the given template type, which will be used to hold the default field values.
    1. By defining default values we can make sure to pre-populate  the standard field values, so that content authors doesn’t have to populate it over and over again.
    2. We can set the default values explicitly, or use of token is a also a great way to populate the values.
    3. We can populate following fields every time when new item gets created in Sitecore:
      1. Title
      2. Description
      3. Date (if exists)

This a great way to help content authors to give them a feel that page doesn’t looks incomplete while adding a new page to content tree, and editors can of course go back and change the default values if required, you can configure default data sources for you renderings in required.

  • Help text : How content authors are comfortable with Sitecore, determines if your Implementation is successful, so, as a developer we always have to think about content author experience, and that should be our high priority.
    1. We should make sure field labels are optimized in such a way they serve the needs of both the developer as well as the content editors, we can achieve this following three ways:
      1. Title
      2. Short description and
      3. Help link
    2. The title field and the short description of a field item enable us to provide the content editor with useful information and have a technical field name for developers to work with.
    3. If you go to specific field item in Sitecore, you can set these values which helps content authors in understanding what this field is meant for, and what value should be added.
  • Clean Sitecore tree:
    1. A clean Information structure helps in making Site more consistent, and we should always remember that site will be used by content editors and not by developers, so it should be as clean as possible.
    2. A new content editor, who just joined the team shouldn’t find any difficulty understanding the site, and it’s component structure.
    3. Good information architecture is a key to successful Sitecore Implementation and maintenance.
  • Components/placeholder configuration(s):
    1. Proper configuration should be done for placeholders, so that content authors can add only those components which are meat for specific placeholder, this way we can make sure to maintain the design, and can prevent any sort of compatibility issues with the page.
    2. We can use allowed control that exists on the placeholder level, which defines the list of component(s) which can be added.
    3. Also, on each rendering we should define “datasource location” and “datasource template” which helps content authors to understand that these data items are compatible for specific rendering(s).
  • Thumbnail for Renderings:
    1. We should try to add thumbnails for all renderings, so that content authors can visualize that what rendering is meant for what, if there are no thumbnails added, it can creates a confusion to content editors, in case if no friendly names has been given to renderings.
    2. If friendly name has been given, it still makes more sense and add value to assign thumbnails.

As a Sitecore consultant and developer, we should keep an eye on every part of your solution from day 1, this helps you in understanding the system as a whole, and also gives you an opportunity to make sure that best practices has been followed while developing the solution,and also that we are using Sitecore features at it’s best.

I hope this helps somebody.

Happy learning 🙂

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 🙂

 

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 🙂