Skip to main content

Posts

Programatically get datasources of all renderings of a page in Sitecore

In Sitecore, one can easily get the list of all the datasources that are linked to renderings on the page. Why do we need this? During development, business logic may require to look for a value in all the datasources that are linked to a page.  I have seen people writing methods as below, but this can be done way more easily in one line - private List<Item> GetRenderingDataSourceItems(Item item)         {             var items = new List<Item>();             var renderings = item.Visualization.GetRenderings(Sitecore.Context.Device, true);             foreach (var rendering in renderings)             {                 //This check ensures only items are added, not queries                 if (Sitecore.Data.ID.IsID(rendering.Settings.DataSource))                 {                     var dsItem = item.Database.SelectSingleItem(rendering.Settings.DataSource);                     if (dsItem != null)                     {                         //Add the datasource item        

Sitecore Powershell script to get all pages that contain the keyword

In this post, I am going to share a script which can help you find all the pages which contain a keyword in its content. This requirement came from the business team who wanted some insights about pages which use keywords like finance, commerce etc. One can easily get this information from search functionality on Sitecore site or client. Or one can always create a powershell script and generate a report that can be shared with business users (yes, they love reports). We had a few assumptions -  We looked for keyword only in Rich text fields. You can modify script to include more field types. We needed the page links rather than Sitecore content path as the business users were not familiar with Sitecore. Mostly, the keyword was present in content items placed within _content folder under the page. For such items, we preferred to resolve the URL to the page ancestor to the content item. For this, we checked if the item path contains _content . If yes, then we limited the URL to page ite

Sitecore Link Database Quick Tips

The Link database saves all links between Sitecore items across the databases (core, master, web) and across the language versions. This database helps to find out not only the list of items, which the specific item refers to but also the items which refer this item. For example, a page created from a specific template contains a reference to that template and vice versa.  In content editor, this database is used in “Navigate -> Links“. or when u delete an item, it asks what you should do with the existing links to the item to be deleted -  The link database is primarily a table (dbo.Links) within our core db configured as - < LinkDatabase type = "Sitecore.Data.$(database).$(database)LinkDatabase, Sitecore.Kernel" >        < param connectionStringName = "core" /> </ LinkDatabase > Link db gets updated on following events - item:saved item:deleted item:copied item:versionRemoved Link db can be rebuilt as - Same can be done programmatically as -

Clean Coding Principles in CSharp

A code shall be easy to read and understand. In this post, I am outlining basic principles  about clean coding after researching through expert recommended books, trainings and based on my experience. A common example to start with is a variable declaration like - int i  The above statement did not clarify the purpose of variable i. However,  the same variable can be declared as -  int pageNumber The moment we declared the variable as int pageNumber, our brain realized that the variable is going to store the value for number of pages. We have set the context in our brain now and it is ready to understand what the code is going to do next with these page numbers. This is one of the basic advantages of clean coding. Reasons for clean coding -  • Reading clean code is easier - Every code is revisited after certain amount of time either by the same or different developer who created it. In both the cases, if the code is unclean, its difficult to understand and update it. • To avoid s