Reading and writing items using Sitecore powershell extensions

Sitecore Powershell Extension is a great tool/module developed by Adam Najmanowicz and Michael West that provides a command line and scripting environment for automating tasks.

In my previous blog post on Sitecore Powershell extensions, we learned very basics of this great module, like it’s introduction, how to install this module, and it’s features.

I would like to take another step in continuation of previous blog post, and like to show some features which can be leveraged out of it.

Retrieve Sitecore Item

We can fetch Sitecore Item based on ID or Path:

If we know the Sitecore item ID, it can be retrieved in following way:

PS-ID

We have to make sure that we are adding the context for database, in this case I have added “master

If we know the Sitecore item path, it can be retrieved in following way:

PS-Path

Retrieve Items from all languages and versions

We just need to pass language and version parameter, if we want items  based on specific language or version, we can specify that language and version as parameter values, instead of passing * to it.

PS-Item-language-version

Retrieve child Items

We can get all child items based on Sitecore path, this is how it can be used:

PS-ChildItems

If you want to retrieve all descendants from the given path, we can add recurse parameter, please see following for ref:

PS-ChildItems-recurse

Bulk Updates

There can be scenarios, where certain field(s) are not updated or empty for some reasons, and now you want to update that specific field for all the items, this is now not so easy, as you have several hundreds of items, but,there is an easy way to make this bulk update using powershell extensions.

PS-Bulkupdates

In the above example- we are trying to filter all the child items under /content/helix/home node where “Browser Title” field is empty, once we have a list of all items, we are iterating over all such items and updating the field with the “Display Name” value of the item.

Bulk updates for “Alt” text

Another very valid scenario for bulk updates can be updating all the Image items where “Alt” text is missing, following can be used to update all such Image items:

PS-Bulkupdates-Images

In the above example- we have used Sitecore query to get all the items based on template name “Image” and then finding such items which has missing “Alt” text, once we have a list we just updated the “Alt” text with item display name value, and then it can be updated by content authors to put something more specific, if required.

I strongly encourage all Sitecore developers to start using this module, and see how this can be leveraged into your solution, this is for sure helping and going to help everyone a lot.

I will continue sharing my thoughts and experiences on this topic, please let me know if you have any questions.

References:

http://blog.najmanowicz.com/sitecore-powershell-console/

https://michaellwest.blogspot.in/

Happy learning 🙂

Advertisements

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

 

Sitecore MongoDB Blog series: Part 2-Understand scaling, contacts and queries

In previous blog post we have gone through MongoDB introduction with Sitecore, features and installation.In this blog we will go over available scaling options in MongoDB, and then followed with introduction to contacts and out of the box queries.

Scaling:

There are three types of scaling:

  1. Standalone environment
  2. Vertical Scaling and
  3. Horizontal Scaling

Standalone environment:

A standalone is all in one configuration, where we install all xDB components in the same computer, which includes:

  • Content management server
  • Content delivery server
  • Database server
  • Reporting server
  • Collection server.

This is not an optimal production environment setup, and it’s mostly resembles the development environment, where we have all components in the same workstation, we can say this setup as “not scalable environment”.

standalone-setup

Vertical Scaling:

Vertical scaling means adding more resources to single node in the system,which typically involves adding/upgrading more hardware to single machine.

When we start inclining towards Vertical setup, we tend to have separate servers for each component, i.e separate servers for:

  • Database
  • Content management
  • Content delivery and
  • Reporting server

If we see that specific component requires hardware upgrade, then we can just scale that environment/component up, without touching any other server, and this way we can scale the complete Sitecore system.

Horizontal Scaling:

Though we can scale each component of the System, by following vertical Scaling, but what about if we have just one Content delivery server and because of some server issue, we lost all data from that server, just can’t imagine right?

In this specific case even we have scaled up the content delivery server by upgrading the size,RAM and all other component(s) as per the requirements, but such thing can’t help us out if something goes wrong with that specific server, which will ultimately results in data loss.

In this scenario, we can resolve the issue by deploying multiple servers for the same components, which includes:

  • Multiple content management servers
  • Multiple content delivery servers
  • Multiple MongoDB(Analytics) servers
  • Separate session state server.

This type of setup helps in resolving the issue of, one server going down for some reason, From MongoDB presepective, we can achieve this by adding multiple servers for Analytics, we do it via adding Replica sets.

By means of replication we achieve following:

  • Availability
  • MongoDB provides high data availability with replica sets.
  • A replica set consists of two or more copies of the same data.

What happens in Replica set is, we setup the environment which defines a primary server, which will be used to read and write the Analytics  information, at the same time all data from replicaset-1 will get copied to replicaset-2 and replicaset-3, all the servers are always in sync.

From here, if something goes wrong to replicaset-1 server, MongoDB internally makes either replicaset-2 or replicaset-3 as a primary source of reading and writing the information, this we can always make sure data availability.

horizontal-scalibility

Introduction to Contacts:

  • In xDB a contact is an individual visitor.
  • This visitor may be anonymous or he may have been authenticated.
  • A contact is a combination of facets.
  • Contact Includes:
    • Identifiers
    • Personal Information
    • Email
    • Phone Number
    • Addresses

contacts

Identifying Contacts:

  • Contact identification is the process of connecting the current session, device and contact session to an identifier. This is implemented using the Identify() method which is part of the Sitecore Analytics tracker namespace.
  • Sitecore.Analytics.Tracker.Current.Session.Identify(identifier)
  • A contact is always identified by an identifier, identifier is an string value which uniquely identifies a contact in relation to website and this value is always provided by contact itself.
  • Identifiers can be one of the following:
    • User login
    • User id from third party system and/or
    • Email address

Here is the sample snippet which shows how we can validate the use in MongoDB:

mongo-validate-user

MongoDB Queries:

Let’s look into the sample two queries, which is used to fetch data from out of the collections.

Consider a case where we have millions of records in “Contacts” collection, and wants to get specific contact record, we can add a filter where we can pass “FirstName”, and we use “Personal.Firstname” Facet for this.

db.getCollection(‘Contacts’).find({“Personal.FirstName”:”Ankit Joshi”})

FirstNameFilter

Another example, if we want to find an identifier based on specific Id, we can use this query:

db.getCollection(‘Identifiers’).find({“_id”:”ANKIT”})

IdentifyFilter

In the same way we can also create custom collections, and add documents to it using Mongo Shell.

We can create custom collections using Mongo Shell, and the beauty of this is, when we try to create a new collection, and if that collection doesn’t exists it will create it automatically, and documents of the collections can have different structure, which makes it more flexible.

Let me know your feedback and comments if any?

References:

https://doc.sitecore.net/sitecore_experience_platform/setting_up_and_maintaining/xdb/platform/scalability_options

Happy learning 🙂