Skip to main content

Posts

Showing posts with the label CSharp

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

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        

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