Sitecore BeginRouteForm posting raw view without layout

 I got an opportunity to work on a custom MVC form to check if user enters the password correct then redirect to other page otherwise show the error, and this was to be done without ajax.

So I created the controller rendering and in the controller created two method as GetPasswordProtectedLogin() and PostPasswordProtectedLogin(Model formData) Which looks like something as following

[HttpGet]
        public ActionResult GetPasswordProtectedLogin()
        {

            if (_botFinder.IsBotOrCrawler)
            {
                return this.View("/Views/.../..../ProtectedLoginForm.cshtml", null);
            }

            var vm = ProtectedLoginFormViewModel();
            
            vm.IsValid = true;

            return this.View("/Views/.../.../ProtectedLoginForm.cshtml", vm);
        }

        [HttpPost]
        public ActionResult PostPasswordProtectedLogin(ProtectedLoginFormViewModel formModel)
        {

            if (_botFinder.IsBotOrCrawler)
            {
                return this.View("/Views/.../.../ProtectedLoginForm.cshtml", null);
            }
            var vm = new ProtectedLoginFormViewModel
            {
                IsValid = true
            };
            vm = ProtectedLoginFormViewModel(formModel);
            string password = Sitecore.Globalization.Translate.Text("password");
            if (password.Equals(formModel.PasswordValue))
            {
                return this.Redirect(vm.FormSettings.SuccessRedirectionUrl.Url);
            }
            vm.IsValid = false;
            ModelState.AddModelError("Invalid Passcode", vm.FormSettings.PasswordErrorMessage);
            return this.View("/Views/.../..../ProtectedLoginForm.cshtml", vm);
        }

        private rotectedLoginFormViewModel ProtectedLoginFormViewModel(ProtectedLoginFormViewModel formModel = null)
        {
            var vm = new ProtectedLoginFormViewModel();
            FormSettings datasourceItem;
            using (new SitecoreContext(Sitecore.Context.Database))
            {
                datasourceItem = GetDataSourceItem<FormSettings>();
            }
            if (datasourceItem == null && Sitecore.Context.Database.Name.Equals(Master))
            {
                datasourceItem = this.GetDatasourceItem_ExperienceEditor<FormSettings>();
            }
            if (datasourceItem == null && formModel != null)
            {
                datasourceItem = _sitecoreContext.GetItem<FormSettings>(formModel.Guid);
            }
            if (datasourceItem != null)
            {
                vm = PasswordProtectedLoginPageViewModelMapper.Map(datasourceItem);
            }
            else
            {
                return null;
            }

            return vm;
        }

And, the view looks like follows:

@using Glass.Mapper.Sc.Web.Mvc
@inherits Glass.Mapper.Sc.Web.Mvc.GlassView<Website.ViewModels.ProtectedLoginFormViewModel>

@if (Model?.FormSettings != null)
{
    var isEditModeCSSClass = "";
    if (IsInEditingMode)
    {
        isEditModeCSSClass = "show-for-editmode";
    }
    using (Html.BeginRouteForm(Sitecore.Mvc.Configuration.MvcSettings.SitecoreRouteName, FormMethod.Post, new { enctype = "multipart/form-data", name = "Default", @class = "protected-login-form" }))
    {
        @Html.AntiForgeryToken()
         @Html.Sitecore().FormHandler("ControllerName", "PostPasswordProtectedLogin")

        <div class="blue-forms">
            <div class="container">
                <div class="row">
                    <div class="col-sm-12">
                        @*if (!Model.IsValid || IsInEditingMode)
                            {
                                <div class="section-headline form-content form-thankyou-content">
                                    @Editable(Model.FormSettings, x => x.PasswordErrorMessage)
                                </div>
                            }*@
                        @Html.ValidationSummary()
                    </div>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="login-form">
                            @Html.LabelFor(vm => vm.PasswordValue, Model.FormSettings.PasswordLabel)
                            @Html.PasswordFor(vm => vm.PasswordValue, new { @Value = "", Class = "elementfocus", id="passwd", placeholder= Model.FormSettings.PasswordPlaceholder })
                            @Html.ValidationMessageFor(vm => vm.PasswordValue)
                            @Html.HiddenFor(vm => vm.Guid)
                        </div>
                    </div>
                    <div class="col-sm-12">
                        @using (BeginEditFrame(Model.FormSettings, null, x => x.ButtonText))
                        {
                            <button type="submit" id="protectedLoginButton" disabled class="btn btn-light">@Model.FormSettings.ButtonText</button>
                        }
                    </div>
                </div>
            </div>
        </div>
    }

}

<script>
    $("#passwd").on('keyup', function () {
        let empty = false;

        empty = $(this).val().length == 0;
        if (empty)
            $('#protectedLoginButton').attr('disabled', 'disabled').removeClass("btn-primary").addClass("btn-light");
        else
            $('#protectedLoginButton').attr('disabled', false).removeClass("btn-light").addClass("btn-primary");
    });
</script>

Everything works fine but when I was submitting the form then it was giving me the view only instead of the complete layout.

Then I checked this issue was faced by many of the Sitecore guys but I found the root cause of this was due to formHandler as per the StackExchange answer here. So to simply it I did three steps

  1. Removed the formHandler from the view.
  2. Renamed my both Get and Post methods to be the same name as follows:
  3. Updated the method name in the controller rendering and after that when I posted the form it was working fine.

[HttpGet]
        public ActionResult ProtectedLogin()
        
[HttpPost]
        public ActionResult ProtectedLogin(ProtectedLoginFormViewModel formModel)


My Final view was as it is shown above just removed/commented this line completely
@*@Html.Sitecore().FormHandler("ControllerName", "PostPasswordProtectedLogin")*@

Redirect to login page if user is not logged in using Sitecore forms Authenctication Manager

 The scenario is as follows which covers all the cases in the code:

  1. When the user visits the page, check if user is at the /login page then keep him on login page if he is not logged in.
  2. When the user visits the page, and the user is trying to access the page other than /login and user is not logged in,  then redirect him to /login page.
  3. When user tries to login with credential and user doesn't exist then show error. And user can click on the Register button to redirect to register page.
  4. When user enters the correct credentials then redirect him to its specific country page (https://domain/countries/<country>) that user would have selected in the dropdown at the time of registration.

using Sitecore;
using Sitecore.Data.Fields;
using Sitecore.Data.Items;
using Sitecore.Links;
using Sitecore.Pipelines.HttpRequest;
using Sitecore.Security.Authentication;
using Sitecore.Security.Domains;
using Sitecore.Web;
using System.Web;

namespace Feature.Forms.Pipelines
{
    public class CheckUserSession : HttpRequestProcessor
    {
        public override void Process(HttpRequestArgs args)
        {
            var url = HttpContext.Current.Request.Url.AbsoluteUri.ToLower();
            //If Sitecore CMS URL then skip check
            if (url.Contains("sitecore") || Context.Site.IsBackend || Context.Site.Domain.Equals(Domain.GetDomain("sitecore")) || url.Contains("register"))
            {
                return;
            }
            //Here Home Page is login page.
            var loginPage = Context.Database.GetItem(Constant.LoginPage);
            var isLoginPage = loginPage.ID.Equals(Context.Item.ID);

            if (Context.Item != null)
            {
                //loginPage = no and authenicate = yes or //loginPage = yes and authenicate = no
                if ((!isLoginPage && Context.User.IsAuthenticated) || (isLoginPage && !Context.User.IsAuthenticated))
                    return;
                //loginPage = no and authenicate = no
                if (!isLoginPage && !Context.User.IsAuthenticated)
                    RedirectToLoginPage(loginPage);
                //loginPage = yes and authenicate = yes
                if (isLoginPage && Context.User.IsAuthenticated)
                    RedirectToItsCountryPage();
            }


        }
        #region Private Methods
        /// <summary>
        /// Check user session, if not valid redirect to configure page.
        /// </summary>
        private void RedirectToLoginPage(Item loginPage)
        {
            string redirectUrl = LinkManager.GetItemUrl(loginPage);
            RedirectUrl(redirectUrl);
        }
        /// <summary>
        /// If logged-in user trying to open url in browser different window/tab, user will redirect to landing/welcome page
        /// </summary>
        private void RedirectToItsCountryPage()
        {
            var user = AuthenticationManager.GetActiveUser();
            var countryBaseUrl = ((LinkField)(Context.Database.GetItem(Constant.CountryFolder)?.Fields[Constant.CountryPageField])).GetFriendlyUrl();
            var userCountryUrl = $"{HttpContext.Current.Request.Url?.Scheme}://{HttpContext.Current.Request.Url?.Host}{countryBaseUrl}/{user.Profile.GetCustomProperty(Constant.Country)}";
            RedirectUrl(userCountryUrl);
        }

        private void RedirectUrl(string url)
        {
            WebUtil.Redirect(url);
        }

        #endregion Private Methods
    }
}


I tried to test the code with the above mentioned points but still there may be some gaps (not major) please feel free to modify in your code repo. :-)

The Config file:

<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <httpRequestBegin>
        <processor patch:after="processor[@type='Sitecore.Pipelines.HttpRequest.ItemResolver, Sitecore.Kernel']" type="Feature.Forms.Pipelines.CheckUserSession, Feature.Forms" />
      </httpRequestBegin>
    </pipelines>
  </sitecore>
</configuration>

Login user using Sitecore forms through Authentication Manager

 In this, we will check if the user exists and make him log in otherwise give an error. There is one Register button also that redirects to the Registration page.


The logic for login user is as:

using Foundation.Helper.Utility;
using Sitecore;
using Sitecore.Data.Fields;
using Sitecore.Diagnostics;
using Sitecore.ExperienceForms.Models;
using Sitecore.ExperienceForms.Processing;
using Sitecore.ExperienceForms.Processing.Actions;
using Sitecore.Security.Accounts;
using Sitecore.Security.Authentication;
using System.Linq;
using System.Web;

namespace Feature.Forms.FormActions
{
    public class LoginUser : SubmitActionBase<string>
    {
        public LoginUser(ISubmitActionData submitActionData) : base(submitActionData)
        {
        }

        protected override bool TryParse(string value, out string target)
        {
            target = string.Empty;
            return true;
        }

        protected override bool Execute(string data, FormSubmitContext formSubmitContext)
        {
            Assert.ArgumentNotNull(data, nameof(data));
            Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));

            var fields = GetFormFields(data, formSubmitContext);

            Assert.IsNotNull(fields, nameof(fields));

            if (UsernameOrPasswordFieldIsNull(fields))
            {
                return AbortForm(formSubmitContext);
            }

            var user = Login(fields.Username, fields.Password);

            if (user == null)
            {
                return AbortForm(formSubmitContext);
            }

            //Redirect to external URL
            var countryBaseUrl = ((LinkField)(Context.Database.GetItem(Constant.CountryFolder)?.Fields[Constant.CountryPageField])).GetFriendlyUrl();
            var userCountryUrl = $"{HttpContext.Current.Request.Url?.Scheme}://{HttpContext.Current.Request.Url?.Host}{countryBaseUrl}/{user.Profile.GetCustomProperty(Constant.Country)}";

            formSubmitContext.RedirectUrl = userCountryUrl;
            formSubmitContext.RedirectOnSuccess = true;

            return true;
        }

        protected virtual User Login(string userName, string password)
        {
            var accountName = string.Empty;
            var domain = Context.Domain;
            if (domain != null)
            {
                accountName = domain.GetFullName(userName);
            }

            var result = AuthenticationManager.Login(accountName, password);
            if (!result)
            {
                return null;
            }

            var user = AuthenticationManager.GetActiveUser();
            return user;
        }

        private LoginUserFormFields GetFormFields(string data, FormSubmitContext formSubmitContext)
        {
            Assert.ArgumentNotNull(data, nameof(data));
            Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));

            return new LoginUserFormFields
            {
                Username = Helper.GetValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals("Username"))),
                Password = Helper.GetValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals("Password")))
            };
        }

        private bool UsernameOrPasswordFieldIsNull(LoginUserFormFields field)
        {
            Assert.ArgumentNotNull(field, nameof(field));
            return field.Username == null || field.Password == null;
        }

        private bool UsernameOrPasswordValueIsNull(LoginUserFieldValues values)
        {
            Assert.ArgumentNotNull(values, nameof(values));
            return string.IsNullOrEmpty(values.Username) || string.IsNullOrEmpty(values.Password);
        }

        private bool AbortForm(FormSubmitContext formSubmitContext)
        {
            formSubmitContext.Abort();
            return false;
        }

        internal class LoginUserFormFields
        {
            public string Username { get; set; }
            public string Password { get; set; }

            public LoginUserFieldValues GetFieldValues()
            {
                return new LoginUserFieldValues
                {
                    Username = Helper.GetValue(Username),
                    Password = Helper.GetValue(Password)
                };
            }
        }

        internal class LoginUserFieldValues
        {
            public string Username { get; set; }
            public string Password { get; set; }
        }
    }
}

using System.Collections.Generic;

namespace Foundation.Helper.Utility
{
    public class Helper
    {
        public static string GetValue(object field)
        {
            return field?.GetType().GetProperty("Value")?.GetValue(field, null)?.ToString() ?? string.Empty;
        }
        public static string GetDropdownValue(object field)
        {
            var value = field?.GetType().GetProperty("Value")?.GetValue(field, null);
            return ((List<string>)value)[0] ?? string.Empty;
        }
    }
}


Registration form using Sitecore forms

We can use the below code to create the registration for the user. I have created the following fields:

1. Full Name - Single Line Text

2. Email - Email

3. Password & ConfirmPassword - Confirm Password

4. Country - Dropdown

5. Submit button - submit

we will attach an action on the submit button that will execute the logic and register this user.

We are saving the form in the Sitecore forms DB.

using Foundation.Helper.Utility;
using Sitecore;
using Sitecore.Diagnostics;
using Sitecore.ExperienceForms.Models;
using Sitecore.ExperienceForms.Processing;
using Sitecore.ExperienceForms.Processing.Actions;
using Sitecore.Security.Accounts;
using System;
using System.Linq;

namespace Feature.Forms.FormActions
{
    public class Registration : SubmitActionBase<string>
    {
        public Registration(ISubmitActionData submitActionData) : base(submitActionData)
        {
        }

        protected override bool TryParse(string value, out string target)
        {
            target = string.Empty;
            return true;
        }

        protected override bool Execute(string data, FormSubmitContext formSubmitContext)
        {
            Assert.ArgumentNotNull(data, nameof(data));
            Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));

            var fields = GetFormFields(formSubmitContext);

            Assert.IsNotNull(fields, nameof(fields));

            if (EmailOrPasswordFieldsIsNull(fields))
            {
                return AbortForm(formSubmitContext);
            }

            var result = Register(fields.Email, fields.Password, fields.FullName, Guid.NewGuid().ToString(), fields.Country);

            if (!result)
            {
                return AbortForm(formSubmitContext);
            }

            return true;
        }

        protected virtual bool Register(string email, string password, string name, string profileId, string country)
        {
            Assert.ArgumentNotNullOrEmpty(email, nameof(email));
            Assert.ArgumentNotNullOrEmpty(password, nameof(password));

            try
            {
                var user = User.Create(Context.Domain.GetFullName(email), password);
                user.Profile.Email = email;

                if (!string.IsNullOrEmpty(profileId))
                {
                    user.Profile.ProfileItemId = profileId;
                    user.Profile.SetCustomProperty(Constant.Country, country);
                }

                user.Profile.FullName = name;
                user.Profile.Save();
            }
            catch (Exception ex)
            {
                Log.SingleError("Register user failed", ex);
                return false;
            }

            return true;
        }

        private RegisterUserFormFields GetFormFields(FormSubmitContext formSubmitContext)
        {
            Assert.ArgumentNotNull(formSubmitContext, nameof(formSubmitContext));

            return new RegisterUserFormFields
            {
                Email = Helper.GetValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals("Email"))),
                Password = Helper.GetValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals("PasswordConfirmation"))),
                FullName = Helper.GetValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals("FullName"))),
                Country = Helper.GetDropdownValue(formSubmitContext.Fields.FirstOrDefault(f => f.Name.Equals("Country")))
            };
        }

        private bool EmailOrPasswordFieldsIsNull(RegisterUserFormFields field)
        {
            Assert.ArgumentNotNull(field, nameof(field));
            return field.Email == null || field.Password == null;
        }

        private bool EmailOrPasswordsIsNull(RegisterUserFieldValues values)
        {
            Assert.ArgumentNotNull(values, nameof(values));
            return string.IsNullOrEmpty(values.Email) || string.IsNullOrEmpty(values.Password);
        }

        private bool AbortForm(FormSubmitContext formSubmitContext)
        {
            formSubmitContext.Abort();
            return false;
        }

        internal class RegisterUserFormFields
        {
            public string Email { get; set; }
            public string Password { get; set; }
            public string FullName { get; set; }
            public string Country { get; set; }

            public RegisterUserFieldValues GetFieldValues()
            {
                return new RegisterUserFieldValues
                {
                    Email = Helper.GetValue(Email),
                    Password = Helper.GetValue(Password),
                    FullName = Helper.GetValue(FullName),
                    Country = Helper.GetValue(Country)
                };
            }
        }

        internal class RegisterUserFieldValues
        {
            public string Email { get; set; }
            public string Password { get; set; }
            public string FullName { get; set; }
            public string Country { get; set; }
        }
    }

}

using System.Collections.Generic;

namespace Foundation.Helper.Utility
{
    public class Helper
    {
        public static string GetValue(object field)
        {
            return field?.GetType().GetProperty("Value")?.GetValue(field, null)?.ToString() ?? string.Empty;
        }
        public static string GetDropdownValue(object field)
        {
            var value = field?.GetType().GetProperty("Value")?.GetValue(field, null);
            return ((List<string>)value)[0] ?? string.Empty;
        }
    }
}

The form looks like:






Sitecore roles for various kind of authors

 Content author with read access only and section like PowerShell, reporting tools, template manager

sitecore\Sitecore Client Users
sitecore\Designer
sitecore\Author

with this, the content tree and editing option will look like something:



 Content author with read access only and with Experience Analytics access.

sitecore\Sitecore Client Users
sitecore\Designer
sitecore\Author
sitecore\Developer

with this, the content tree and editing option will look like something:







Content author with read access only but with few items to be editable.

sitecore\Sitecore Client Users
sitecore\Designer
sitecore\Author

1. assign these roles to the new author.
2. Now go the security editor (by admin account), and ensure you have selected the author in the account box as shown in screenshot.

3. Now I wanted the author to edit the countries item so I checked the write and if you want its children also to be edited then select inheritance on parent (countries) so that the same permission can be inherited on the children.
 


Wildcard page in Sitecore - using rendering datasource resolver

 I hope you would have seen the article already regarding the wildcard using item resolver before reading this.

So, to resolve the wildcard using the data source resolver the idea is that this time we will apply the rendering, or presentation detail on the * item instead of the data source item unlike shown in the previous blog.

So our structure will be now as follows:

- Home
    - Countries
                    * (with presentation detail with your controller rendering available)

we will keep the data items in the global folder as

- Data
    - Countries
        India - (with no presentation details)
        USA - (with no presentation details)
        Russia - (with no presentation details)

Now, when URL URL https://[domain]/countries/india maps to the * item then we will run a processor to resolve the datasource dynamically on the basis of country passed in the URL.

So, in processor we will say that in the rendering dynamically pass the datasource as /sitecore/content/POC Site/New site/Data/Countries/{last segment of URL}. Here in the example last segment of the URL will be India so the complete datasource on runtime will be /sitecore/content/POC Site/New site/Data/Countries/india.

Here is the code below:

using Sitecore.Mvc.Pipelines.Response.GetRenderer;
using System;
using System.Linq;

namespace Feature.WeatherForecast.Pipelines
{
    public class CountryDatasource : 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/POC Site/New site/Data/Countries";
            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
            {
                var notFoundItem =                     Sitecore.Context.Database.GetItem("/sitecore/content/YourSite/NotFoundPage");

                if (notFoundItem != null)
                {
                    // Set the 404 page item as the current item
                    Context.Item = notFoundItem;
                }
                else
                {
                    // Log a warning if the 404 page item is not found
                    Log.Warn("404 Page Not Found item not found in Sitecore content tree.", this);
                }
            }
        }
    }
}
Finally patch your changes:


<?xml version="1.0"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <pipelines>
      <mvc.getRenderer>
        <processor type="Feature.WeatherForecast.Pipeliness.CountryDatasource, Feature.WeatherForecast" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Response.GetRenderer.GetViewRenderer, Sitecore.Mvc']">
          <renderingname>WeatherForecast</renderingname>
        </processor>
      </mvc.getRenderer>
    </pipelines>
  </sitecore>
</configuration>

Hope it helps!

Troubleshoot personalization on xm cloud

Here are the troubleshooting steps below that helped personalization to work. We checked the events analytics in the page builder but it was...