Sitecore 9 Forms – Google reCAPTCHA form element

Hello Sitecorians,

Hope you’re playing around with Sitecore 9 and with the newly introduced cool features in it. Recently, I’ve been exploring Sitecore Forms and implemented the Google Re-captcha form element within the Sitecore forms. Re-captcha is the basic requirement for the form submission process and in order to implement this, we’ve to write some code to it.

Before you go through with this post in detail, you must know the basics of Sitecore Forms.

We will go through step by step process to implement the Google Recaptcha with Sitecore Forms using the custom form element. And will create a generic component which you can use for any other form as well.

Read More

Remove Sitecore Personalization Rules in Bulk

Hello Sitecorians,

Do you need to remove the Personalization Rules for many items in bulk or you would like to learn about that on how to do that quickly? If yes then you are at right place. 🙂

Recently there was a requirement to add personalization throughout the site. So the team did that at the template level for the required pages. But they didn’t want that for the specific node and all it’s child items and wanted to keep that away from all personalization and also it will never be applied to that items. There were approx. 1000+ items, so if they remove it manually for each item — it will take ample amount of time. In order to save the time we created a utility that will do the job in few minutes.

We need the following:

  • Parent Item ID – So that we can iterate through all the child items
  • Rendering/Sub-layout ID
  • Data-Source Value – If you would like to add/update
	
	// Remove Personalization Rules in Bulk and Update Data-Source
 
	// Default device from Sitecore
	var deviceItem = db.GetItem("{FE5D7FDF-89C0-4D99-9AA3-B5FBD009C9F3}");
	DeviceItem device = new DeviceItem(deviceItem);
 
  //Iterate through all the items
  foreach (Item childItem in parent.Axes.GetDescendants())
  {
    if (childItem.Fields != null && childItem.Fields["__Renderings"] != null && childItem.Fields["__Renderings"].ToString() != string.Empty)
    {
      // Get the rendering where personalization is needed to be removed.
	    var renderings = childItem.Visualization.GetRenderings(device, true).Where(r => r.RenderingID == Sitecore.Data.ID.Parse(txtRenderingID.Text)).ToArray();
 
      if (renderings.Count() == 0)
	    {
        string log = "Item not updated: " + childItem.Paths.Path + ", Info: Rendering with ID " + txtRenderingID.Text + " not found in the item presentation!";
        Sitecore.Diagnostics.Log.Info(log, this);
        continue;
      }
 
	     // Get the layout definitions and the device
       Sitecore.Data.Fields.LayoutField layoutField = new Sitecore.Data.Fields.LayoutField(childItem.Fields[Sitecore.FieldIDs.LayoutField]);
 
       if (!string.IsNullOrEmpty(layoutField.Value))
       {
 
          Sitecore.Layouts.LayoutDefinition layoutDefinition = Sitecore.Layouts.LayoutDefinition.Parse(layoutField.Value);
 
		      Sitecore.Layouts.DeviceDefinition deviceDefinition = layoutDefinition.GetDevice(device.ID.ToString());
 
		      var renderingItem = deviceDefinition.GetRendering(renderingID);
 
          if (renderingItem != null)
          {
            // Remove all the personalization rules
            renderingItem.Rules.RemoveAll();
 
            // Update the renderings datasource value
            renderingItem.Datasource = txtNewDataSourceID.Text;
 
            // Save the layout changes
            childItem.Editing.BeginEdit();
            layoutField.Value = layoutDefinition.ToXml();
            childItem.Editing.EndEdit();
          }
        }
    }
  }

Let me explain to you what I did in above code.

  1. Get the default device from Sitecore
  2. Iterate through all the child items
  3. Proceed further if an item has any rendering assigned to it
  4. Proceed further if an item has the required rendering, if not then continue with next item
  5. Get the layout definition and the device
  6. Get the rendering and remove all the rules
  7. Save the changes

This is the quickest and easiest solution I found, as there was no Sitecore Powershell installed. If you know any other better solution then do comment below.

Happy Sitecoring!

Add Sitecore Rendering in Bulk

Hey Sitecorians,

Hope you’re doing well with Sitecore.

Many a time we need an option to have some utility that will do the job easily and save the time for the content team to get things done faster and take the bunch out of the plate. One such requirement was for adding the sub-layout/rendering in bulk. I quickly built a utility that will do the job easier for the content team.

A simple utility that will do the following:

  1. Add Rendering
  2. Assign placeholder key to a newly added rendering
  3. Add appropriate data-source value

The utility will ask for the following:

  1. Parent-item ID
  2. Template ID – Which items, defined from the given template need to have the rendering.
  3. Rendering ID
  4. Data-Source Value
  5. Placeholder Key

Read More