Excluding an item/page from search or index

This is a generic situation most of Sitecore geeks face during their development work. Though, you may find several solutions over the internet, but what I am going to put here is the most simple and robust solution.

Just a checkbox and few lines of code and you are done. Let’s do it now.

  1. add a checkbox field on the item template or on the base template ( your choice) , name it something meaningful like “ExcludeFromSearch” or “DoNotIndex”
  2. Create a custom item crawler. see the code below
    using Sitecore.ContentSearch;
    using Sitecore.Data;
    using Sitecore.Data.Items;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    namespace Learning.Framework.Search.Indexing.Crawler
    {
    public class CustomItemCrawler : SitecoreItemCrawler
    {
    protected override bool IsExcludedFromIndex(SitecoreIndexableItem
    indexable, bool checkLocation = false)
    {
    var isExcluded = base.IsExcludedFromIndex(indexable, checkLocation);
    if (isExcluded)
    return true;
    Item item = indexable;return (item[new ID(pass the checkbox field id here)] == "1") ? true: false;
    }
    protected override bool IndexUpdateNeedDelete(SitecoreIndexableItem
    indexable)
    {
    var needDelete = base.IndexUpdateNeedDelete(indexable);
    if (needDelete)
    return true;
    Item item = indexable;
    return (item[new ID(pass the checkbox field id here)] == "1") ? true:
    false;
    }
    }
    }
  3. Patch the above custom crawler to the config as shown belowExcludeFromSearch-config
  4. Build the solution and publish to the server. Check the checkbox on whichever item you want to exclude/remove from search.
  5. Once publish is done, rebuild the target index from control panel and you are done.

Note: There is a method in the above CustomItemCrawler , the method name is IndexUpdateNeedDelete

this is to delete the existing items/pages from the index.

I hope this post will help some of you.

Happy Coding !!!

How to use Google reCaptcha on WFFM with ASP.NET Layout

Recently I needed to have reCaptcha on my forms so that I can secure them from bots. My client specifically wants to use reCaptcha so that we can have “I’m not a robot” check on our web forms.

Imnotrobot.png

There is a guide provided by Sitecore on “How to use reCaptcha on forms”, but that is for MVC layouts (See that guide here), not for ASP.NET layouts. I have spent a lot on finding a way to use reCaptcha on ASP.NET layout and then I landed on here . but, the question was, how to use this for WFFM as we don’t have access to the code. So I thought of implementing this by creating a custom field type for WFFM and render that HTML and javascript via that new field type.

So let’s take a look at that implementation.

  1. Create a class and inherit it from Sitecore.Form.Web.UI.Controls.LabelreCaptcha-code
  2. use the below Javascript code for the button click event
    $(".scfSubmitButton").click(function() {
    /*read the tocken generated by reCaptcha , token will be generated only
    if the validation is successfull */   
        var rcres = grecaptcha.getResponse();
        if (rcres.length) {
            return true;
        } else {
            return false;
        }
    });
    
  3. Create a custom field type in Sitecore at this location – /Sitecore/system/Modules/Web Forms for Marketers/Settings/Field Types/Custom  custom-field-type.png
  4. Download the recaptcha-dotnet-1.0.5.0-binary.zip and place the Recaptcha.dll into your website’s bin folder.
  5. Now the new custom field type is ready. You just need to add a field on your form and select the field type as your new custom field (reCaptcha in my case). field-on-form.png

Publish your form and see the reCaptcha in action as shown in this screenshot.actual-use.png

I know not many people are using Sitecore on ASP.NET layouts, MVC is lightweight and far good for Sitecore. But there are some implementations on ASP.NET layouts as well, so Wrote this post in a hope that it will help someone like me.

How to hide Publish Site button in Sitecore

Publish Site –  yeah that’s the button which can cause you some serious failure in your live site if you don’t use this with extra care. What I mean to say is – if accidentally, a user hits the Publish Site, this can publish all the items from the content tree ( you might have items which are not supposed to go live). Not only this, if the content tree is large, this may cause a performance issue as well and of course, that will affect the end user. No one wants to make a bad impression on the audience of the site.

So, the million dollar question is – How can I hide/remove the Publish Site button for my users?

Well, I have got the answer in the next few lines.

Note- Publish Site is available in two locations, The Start Menu, and the Publish Menu in the content editor. So let’s do it-

  1. Login to your core database in Sitecore.
  2. Now, open the Security Editor either from start button or from the dashboard.
  3. Select the role “Sitecore Client Publishing” by clicking on the account button available on the top of the security editor window.selectrole
  4. To hide the Publish Site button from the start menu, Navigate to the sitecore > content > document and settings > all users > start menu > left > publish site item in the left pane and remove the read permission from there.hideinstartmenu
  5. To hide the Publish Site from the publish menu in the content editor, Navigate to sitecore > content > applications > content editor > menues > publish site item in the left pane and deny the read permission on this item.hideinpublishmenu

One last not-  The above steps will hide the Publish Site feature from your Sitecore instance for all the users except Admin users.

 

Creating and Modifying Sitecore Items programmatically

This post is dedicated to the newborn Sitecore developers. Generally, the newborn  Sitecore developers stuck in a situation when they need to create a new Sitecore item or modify the existing or new item programmatically.

This post will take them to their destination where they will find a piece of code which will help them in achieving their goal of creating/modifying Sitecore items programmatically.

Creating item programmatically

This below code will explain you the process of creating an item dynamically

//Disable the Security
using (new SecurityDisabler())
{
   //First get the desired DB where you want to create the item
   Database master = Sitecore.Configuration.Factory.GetDatabase("master");
   if (master != null)
   {
     //Here you need the Parent Item (new item will be child of this item)
     Item parentItem = master.GetItem(new ID("{4F67FC21-199E-414C-BE96-E861281F1413}"));
     //Get the template for the new item
     TemplateItem template = master.GetTemplate(new ID("{E5934ED6-8D4F-4FEB-AA1A-AA06C783195F}"));
     if (template != null)
     {
       //Now, use Parent item to add a new item underneath and pass the item name and template as parameter
       Item newItem = parentItem?.Add("NewItem", template);
     }
   }
}

 Modifying the Item content programmatically

the below code will help you understand the item alteration dynamically

//again disable the security
using (new SecurityDisabler())
{
    //get the item to modify the content
   //Get the DB to get the Item
   Database master = Sitecore.Configuration.Factory.GetDatabase("master");
   
   //get the Item from the DB to modify the content
   var item = master.getItem(new ID("{4F67FC21-199E-414C-BE96-E861281F1413}"));
   
   //Enter into the item editing state
   item.Editing.BeginEdit();
    
   //Now you can edit the item field values as belwo
    item["title"] = "some value"
   
   //once you are done with the editing, come out of editing satate then
   item.Editing.EndEdit();
}

So that’s the very basic of Sitecore API where you could create/modify the items dynamically from your C# code.

Happy Coding!!!

Complex Field Types – Part 2 (Link Types)

The second post in the series of Complex Field Types. Here, I will explain to you the “Link Types”.

There are six types of link types available in Sitecore. Find about them here…

LinkTypes

  1. Droplink: This field type is similar to the Droplist (Droplist is available as a List type see here). For the end user, it works as Droplist list only, but in backend, it refers to an item instead of rendering string like Droplist does. It references to items based on the source provided to the field in the template. The selection type is the Single item at a time. Droplink does not support the field render. The source of Droplink can be XPath query, GUID and Item Path.

    Droplink2

  2. Droptree: Droptree shows items as a tree-view instead of showing them as the dropdown. The user is allowed to select a single item at a time. It shows complete Sitecore tree if the source is not provided at the time of field declaration. The source can be XPath Query or GUID and Item Path. Droptree does not support field render.Droptree1

    Droptree-no-source
    If No Source is Provided when the field was created
  3. General Link: General link allows editors to insert a link to Sitecore Item or to an external URL or a custom anchor tag or Email address or even can insert a JavaScript function. This type is quite common across Sitecore developers and it’s being used frequently. General Link types support the field render.

    General-Link
    This is an example of inserting the javascript into the link field.
  4. General Link with Search: Similar to General Link type, this type comes with an extra feature “Search for a link”. This feature allows editors to search for an item which is referenced in the field. This field type supports field render.General-Link-search1

    General-Link-search2
    That’s the window you see when you click on Search for a link

Version Identity: and Version Link: field types are used by Sitecore for internal purposes.

For List Types, you can refer to my previous post.

References :

 

 

Complex Field Types – Part 1 ( List Types)

The first post in the series of Complex Field Types. In this post, I will explain to you the “List Types”.

The List types fields are used when a user needs to select one or more values from a list of available options. For example- A Multilist field allows the editors to choose multiple values from a list of available values, A Droplits allows the editors to choose a value from a list of available values.

ListTypes-1

So, let’s go ahead and explore them one by one

  1. Checklist: Checklist type allows editors to select more than one values from a list of available checklist values. Checklist stores values in as a pipe-separated list of IDs. The checklist doesn’t allow editors to order their selections. Checklist type field cannot be rendered so a developer has to read the selected values and display them on the page.Checklist
  2. Droplist: A Droplist stores the name of the items. It allows the editors to select one value from the dropdown list. Droplist is similar to Droplink, the only difference is – Droplist stores item name while the Droplink stores the ID of the item.Droplist
  3. Grouped Droplist: Grouped Droplist field type allows editors to select a single grandchild of the Item specified in the source of the field. This type cannot be rendered. It stores the value as a string (the name of the selected Item is the value).GroupedDroplist
  4. Grouped Droplink: Grouped Droplink field type allows editors to select a single grandchild of the item specified in the source of the field. It’s similar as Grouped Droplist, the only difference is – this field stores the Item ID while Grouped Droplist stores the name of the Item. This field type cannot be rendered.GroupedDroplink
  5. Multilist: Multilist field type allows editors to select the one or multiple values from the available values coming from the specified source of the field. It stores the IDs of the selected items. You can order the selected values. Multilist type cannot be rendered, so to display the selected values, a developer has to read the values using code.Multilist-1Multilist-2
  6. Multilist with Search: Similar to Multilist type, this field type allows editors to search the values. The selected values displayed with their path with parent item (Countries – India). Also, you get some other option there like Next, previous, Refresh etc. Other features remain the same as Multilist. This field cannot be rendered.Multilist-Search
  7. Name Value List: Name Value List type allows editors to store the value in the key-value pair. It inserts a new row when you enter the value in the first row. Also, it does not support any special character in the key field while in value you can use them. The values are stored separated by “&”. This field cannot be rendered.NameValueList
  8. Name Lookup Value List: This field type is an extended version of Name Value List type having many of the similar features. The only difference here is – Name Lookup Value List uses a Dropdown list to choose the value against a key. The editor is still required to type the key name, the dropdown is only for Value field. This field cannot be rendered.NameLookupValue
  9. Treelist: Treelist accommodate the selected items from the source provided in Field’s source property. If no source is provided, it will list the whole Sitecore tree and user editors will be able to select any item from there. Treelist stores the values in pipe-separated IDs. This field type cannot be rendered.Treelist
  10. TreelistEx: TreelistEx is very much similar to Treelist, except that for this field type, the selection option will be opened as a pop-up window. This field type cannot be rendered. TreelistEx is far better than Treelist when performance is the concern.TreelistEx

Above explained field types are List Types and in my next post to this series, I’ll come up with the Link Types. So stay tuned here for more on Field types.

References :

  1. https://sdn.sitecore.net/upload/sitecore6/65/data_definition_reference_sc64-65-usletter.pdf
  2. https://gopigujjula.com/2017/01/Saturday-Sitecore-Learning-Template-Part-3/
  3. https://trnktms.com/2016/10/15/sitecore-name-value-list-field-with-special-characters/