Wildcard Item in Sitecore

If the Sitecore item name is an asterisk (*) then the item is called a wildcard. So, Wild Card is a special page in our Sitecore content tree represented by the item name "*".

Reason to create Wild Card Page: If we need to create large numbers of the content page using the same page template then we must apply the same rendering and presentation details on every content page. It’s very hectic for a content editor to create several pages and manages those pages into a content tree. To overcome this issue Wild Card Page is the best solution in which our all the data source we reside outside of the home node, somewhere in Global folder and we will have only a single wild card page which will serve all the data source pages.

Let’s quick start, I have some products Under the Global/Product Folder, and I am creating a single Wild card page for all the products, that will display the information on the product:

  1. Create a Wild Card page into the home node under the Products page and named it as "*", you can assign any display name. I am giving it the display name "All Products". Wild_Card_Sitecore_1
  2. Create a Product list under the Global folder, I am creating under the Global/Product folder: Wild_Card_Sitecore_2
    Here you can see that both the page and data source are separate. The product page is created under the Home node and Product List data source for each product created underneath Global/Products.
  3. In a normal scenario, we create a page for each product listed under the Global/Products folder and presentation details having the rendering with the respective product data source. But here we will not assign any data source into the rendering to display the product information, now the question is how information of the product will be a displayed or how will you assign the data source?
    The answer is dynamic, we will assign the data source to the rendering. As we are aware that rendering depends on the RenderingContext.Current.Rendering.DataSource and instead of assigning the data source in presentation details rendering we will assign it dynamically.
  4. To achieve this you need to write one pipeline mvc.getRenderer and assign the data source runtime by overriding the process method of the pipeline based on the URL requested. So once the user will hit the All products page URL with the product code or name, we will get that product item from the Global/Products folder and assign it to the rendering data source, if it does not found then we will redirect it to the 404 pages.
  5. In the configuration file, we will define the rendering name as well, so in the process method, we can identify the name of the rendering. My rendering name is ProductDetail. Here my configuration path file:
      <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <sitecore>
        <pipelines>
          <mvc.getRenderer>
            <processor type="Helixbase.Feature.Products.Pipelines.WildcardDatasource, Helixbase.Feature.Products"                  patch:before="processor[@type='Sitecore.Mvc.Pipelines.Response.GetRenderer.GetViewRenderer, Sitecore.Mvc']">
              <renderingname>ProductsDetail</renderingname>
            </processor>
          </mvc.getRenderer>
        </pipelines>
      </sitecore>
    </configuration>
    
  6. In the process method, first, we will get the rendering name and check that The rendered page name is "*" and the applied rendering name is the same as we defined into the configuration file then only we will be procced. After that, we will get the product code or name from the Request URL segment and find the item with the same name or code into the product list i.e. Global/Product folder. Finally, we will assign that item to the rendering data source. Code for the configuration patch file:
    namespace Helixbase.Feature.Products.Pipelines
    {
        public class WildCardDataSource : GetRendererProcessor
        {
            private string RenderingName { get; set; }
            public override void Process(GetRendererArgs args)
            {
                var productsDetailRendering = RenderingName;
                if (args.PageContext.Item.Name != "*" 
                    || !args.Rendering.RenderingItem.Name.Equals(productsDetailRendering, StringComparison.InvariantCultureIgnoreCase))
                    return;
    
                if (args.PageContext.RequestContext.HttpContext.Request.Url == null) return;
    
                const string datasourceFolder = "/sitecore/content/Helixbase/Global/Products";
                var dataSourcePath =
                    $"{datasourceFolder}/{args.PageContext.RequestContext.HttpContext.Request.Url.Segments.Last()}";
    
                var dataSourceItem = Sitecore.Context.Database.GetItem(dataSourcePath);
                if (dataSourceItem != null)
                {
                    args.Rendering.DataSource = dataSourcePath;
                }
                else
                {
                    // 404 page not found
                }
            }
        }
    }  
  7. Now hit the URL like https://helixbasesc.dev.local/ Products/{product-Name} example https://helixbasesc.dev.local/ Products/Books and you will get your Book product detail page.

Happy Sitecoreing ðŸ˜Š

Comments

Post a Comment

Popular posts from this blog

Sitecore Installation Error: Failed to Start Service 'Sitecore Marketing Automation Engine'

Import CSV Data in Sitecore Using PowerShell: Part-3

Sitecore Technology MVP Journey 2022