Skip to main content

Posts

Showing posts from July, 2020

How to push items linked in rendering data sources to next workflow state in Sitecore

Workflow implementation is important for content governance. When content author updates a page in Sitecore experience editor, Sitecore notifies content author to submit the page to push it to next workflow state.  But what about the rendering data sources present on the page. Do they get pushed to next workflow state or not?  Sitecore does push the rendering data source items present on page to next workflow state if you have the below setting set to true in your configs (which is true by default Sitecore 8.2 onwards) -  <setting name="WebEdit.AffectWorkflowForDatasourceItems" value="true" patch:source="Sitecore.ExperienceEditor.config"/> However, that's not all. Some of the rendering data sources may be dependent on other items (we will call them linked items), especially for container renderings (which act as container for other data items). There is no setting in Sitecore OOB that can push the linked items to next workflow state. Content auth

Check for lock on Sitecore Item

A page in Sitecore consists of multiple renderings with multiple data sources. Content authors need to acquire lock on a page to edit the page. When content authors save the component and submits it to next stage in workflow, Sitecore makes a check whether the context user has enough rights and the lock on the item (data source or page) to push it to next workflow. This is needed to make sure that the content author doesn't push an item that is not locked by it to next workflow. This is done using the below condition -  if (item.Access.CanWrite() && (!item.Locking.IsLocked() || item.Locking.HasLock())) This condition is useful when we are building a customization related to workflow processing or experience editor's inline editing functionality. 

Custom Buttons in Sitecore Experience Editor

Experience editor gives WYSIWYG (What you see is what you get) experience to content authors in Sitecore. Most of the fields defined in templates for components are directly editable in the experience editor mode including images, text, links and many others (Thanks to the Sitecore field renderers implemented in the view!) However, there are scenarios in which some of your fields won’t be easily editable on the page in the experience editor. For example, if you want to provide spacing or width options in a component to content author, they are not directly editable within experience editor like the images and text. Such fields are usually created using droplinks or check boxes. In such cases, it is desirable to create a custom button that will let you edit such fields. This custom button will be visible in experience editor when user clicks on the rendering. On clicking this custom button, a dialog containing the fields that we want to edit in experience editor will open up.  Steps to

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