Saturday, April 23, 2022

How to use the command in ribbon section

Find my use case here

I was asked to create a new menu in the ribbon section and when my admin clicked on the menu I have to do some action. In my case, I have to open one popup and pass some data to the popup modal. So here I have defined the steps to create a custom button and call my commend template in a button click.

Create the custom editor button 

Create a Chunk

Path : /sitecore/content/Applications/Content Editor/Ribbons/Chunks


Template path : /sitecore/templates/System/Ribbon/Chunk

 

View in End Result

The header value is connected with this ribbon label.

Create a button

Path :/sitecore/content/Applications/ContentEditor/Ribbons/Chunks/SitecoreShades


I have created the Large button, But we have a lot of options, 


View in End Result

Create Strip

Path :/sitecore/content/Applications/Content Editor/Ribbons/Strips



View in End Result


The header is reflected in the ribbon section.

Path :/sitecore/content/Applications/Content Editor/Ribbons/Strips/SitecoreShade


Updated the chunks here

Create a Ribbon


Path :/sitecore/content/Applications/Content Editor/Ribbons/Ribbons/Default

Updated the strips here

View in End Result

When you click on this option it will automatically debug the code which you mentioned in the config.


 You can go with your own implementation.


How to create the command template

Code Part

Create a base command

using Sitecore.Data.Items;

using Sitecore.Diagnostics;

using Sitecore.Shell.Framework.Commands;

namespace Feature.CommandTemplate.Commands

{

    public abstract class CommandBase : Command

    {

        public override CommandState QueryState(CommandContext context)

        {

            Assert.ArgumentNotNull((object)context, nameof(context));

            Item[] items = context.Items;

            if (items.Length != 1)

                return CommandState.Hidden;

            Item obj = items[0];

            return CommandState.Enabled;

        }

    }

}

Implement our command template

using Sitecore.Data.Items;

using Sitecore.Diagnostics;

using Sitecore.Shell.Framework.Commands;

using Sitecore.Web.UI.Sheer;

using System;

 

namespace Feature.CommandTemplate.Commands

{

    public class SampleCommand : CommandBase

    {

        public override void Execute(CommandContext context)

        {

            Assert.ArgumentNotNull(context, nameof(context));

            if (context.Items.Length < 1)

            {

                return;

            }

            Item obj = context.Items[0];

            Sitecore.Context.ClientPage.Start("sample.ui");

        }

    }

}

You can start your processor, I have used the sample.ui as the processor name.

Add your configuration

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:set="http://www.sitecore.net/xmlconfig/set/">

    <sitecore>

        <commands>

            <command name="sitecoreshades:ribbon:sample" type="Feature.CommandTemplate.Commands.SampleCommand, Feature.CommandTemplate" />

        </commands>

    </sitecore>

</configuration>

Let's see how to use it

Command templates

 What are command templates?

 ØA command template (also known as a command template definition item) defines a class and method to be called during an insert operation.

ØYou can create a command template that does not display a user interface, a command template that uses a JavaScript prompt to collect an item name or a command template that displays an ASP.NET user interface.

ØCommand templates typically invoke a wizard application that collects information from a user and then programmatically creates an appropriate set of items.

Where should I define it?

ØA command template can be assigned as an insert option to items and standard values. The command template insert option appears identical to the data template and branch template insert options.

ØThe only difference is that the command template insert option triggers a Sitecore UI command. You can assign command templates along with data templates and branch templates using insert options.

Example

ØFor example, you can use a command template to invoke a wizard that gathers information from the user before creating items.

ØUnlike data templates and branch templates, which consist of predefined structures, command templates reference Sitecore UI commands to invoke wizards or other logic used to create new items.

Let's see how to create the command template Check out here

Thursday, April 21, 2022

How to add custom validation

 Case :

We have a start and end date in the field section. I would like to allow my user to select only the future date. how we can do that?

Create a custom class:

using Sitecore.Data.Validators;

using Sitecore.Data.Fields;

using System;

using System.Runtime.Serialization;

 

namespace Foundation.Validation.Validators

{

    public class FutureDateValidator : StandardValidator

    {

        public FutureDateValidator() : base()

        {

        }

 

        public FutureDateValidator(SerializationInfo info, StreamingContext context)

            : base(info, context)

        {

        }

 

        protected override ValidatorResult Evaluate()

        {

            Field field = this.GetField();

            DateTime time = Sitecore.DateUtil.IsoDateToDateTime(field.Value).Date;

            if (time >= DateTime.UtcNow)

            {

                return ValidatorResult.Valid;

            }

            this.Text = this.GetText("The field \"{0}\" should contain a future date", field.DisplayName);

            return ValidatorResult.CriticalError;

        }

        protected override ValidatorResult GetMaxValidatorResult() { return GetFailedResult(ValidatorResult.CriticalError); }

        public override string Name => "Date In Future Validator";

    }

}

Create the rule definition item in Sitecore



Add your rules to your fields


Test the validation


Updated the past date and try to save the page

Steps to follow when using a PowerShell script to modify the goals in Sitecore

I have previously utilized PowerShell for item creation, modification, deletion, and presentation details in Sitecore.   Ø Recently, I attem...