It is possible via both a custom field implementation or field validation. Below is the code for both ways.
Solution 1 - A Custom Validation
You should implement a class which will extend the StandardValidator
. The code snippet is provided below to create a custom validation for the droplist.
[Serializable]
public class UserValidation : StandardValidator
{
protected override ValidatorResult Evaluate()
{
var selectedValue = ControlValidationValue;
var username = Context.User.LocalName;
if (!selectedValue.IsNullOrEmpty() && selectedValue.Equals(username))
{
Text = GetText($"Field {GetFieldDisplayName()} cannot select your own name");
return ValidatorResult.FatalError;
}
return ValidatorResult.Valid;
}
protected override ValidatorResult GetMaxValidatorResult()
{
return GetFailedResult(ValidatorResult.Error);
}
public override string Name => "User Validator";
}
In the code above, I have use the LocalName
property. This property will return me only the name. Example, Admin.
Note that this piece of code will validate the username against the item name. It is feasible check the username against a field on the items from the list but it will require additional code to retrieve the selected item then read the field.
Once you have deployed the above code, you will need to create a new validation item under the path /sitecore/system/Settings/Validation Rules/Field Rules
.

Change the Type
to match your namespace. Also, you need to assign the newly created validation rule to the field.
Solution 2 - A Custom Field
You can implement a custom droplist which extend the ValueLookupEx
from the namespace Sitecore.Shell.Applications.ContentEditor
in the Sitecore.Kernel.dll
. The purpose will be to remove the current user from the list of items if it matches.
public class CustomDroplist : ValueLookupEx
{
protected override Item[] GetItems(Item current)
{
var items = base.GetItems(current);
var username = Sitecore.Context.User.LocalName;
var resultItems = items.Where(w => !w.Name.Equals(username)).ToArray();
return resultItems;
}
}
Once implemented, deploy the code and load the content editor. Then, switch the database to core
and navigate to the path /sitecore/system/Field types/List Types
and create a new field using the template Template field type
.

Update the assembly and class with yours.
Outcome
