Try our conversational search powered by Generative AI!

Want to show a blocks visitor groups on a contentarea

Vote:
 

Hi

I am trying to render a blocks visitor groups. Anybody know how to do this? Since Blocks will be personalised for every contentarea they are used on. I have created a Ignoe field on the block model that i want to render a string showing the personalization informasjon.

        [Ignore]
        public String VisitorGroups { get; set; }

 

I am thinking that i can get the information i need in the block controller

    /// 
    /// Default block controller that pass all blocks to the default repo for block-view with the model
    /// 
    public class BaseBlockController : BlockController
    {
        public override ActionResult Index(SiteBlockData currentContent)
        {

            //I manage to get the visitor groups on the page. But how do i get the visitor group used on a spesific block on a contentarea
            var currentContentArea = ControllerContext.ParentActionViewContext.ViewData.Model as ContentArea;
            if (currentContentArea != null)
            {

                var visitorGroupRepo = ServiceLocator.Current.GetInstance();
                var visitorGroupUsage = ServiceLocator.Current.GetInstance();
                var contentRepo = ServiceLocator.Current.GetInstance();


            }
            //I want something like this in the field
            currentContent.VisitorGroups = "Visible to: First time visitors, Returning visitors, Uk visitors";

            return PartialView(string.Format("~/Views/Shared/Blocks/{0}.cshtml", currentContent.GetOriginalType().Name), currentContent);
        }

Anybody?

#136975
Sep 18, 2015 20:46
Vote:
 

I iterate all vistor groups used in my Visitor Group Usage gadget. Take a look at:

https://github.com/davidknipe/VisitorGroupUsage/blob/master/VisitorGroupUsage/Impl/VisitorGroupUsageImpl.cs

For some inspiration.

#136977
Sep 18, 2015 21:50
Vote:
 

Hi

Thanks for the inspiration. Now i have installed your gadget. I hope my editors will like it. I think it is very helpful.

Thanks to you i now have a code that works. But my code uses currentContentArea.Contents witch is obsolute. Did not find a good solution for that. Do you have a solution?

        public override ActionResult Index(SiteBlockData currentContent)
        {
            if (PageEditing.PageIsInEditMode)
            {
                var currentContentArea = ControllerContext.ParentActionViewContext.ViewData.Model as ContentArea;
                if (currentContentArea != null)
                {
                    var visitorGroupRepo = ServiceLocator.Current.GetInstance<IVisitorGroupRepository>();

                    //Find currentContent in content area. Any better way of doing this?
                    int index = currentContentArea.Contents.IndexOf(currentContent as IContent);
                    var item = currentContentArea.Items[index];

                    if (item.AllowedRoles != null)
                    {
                        var names = String.Empty;
                        var counter = 0;
                        foreach (var group in item.AllowedRoles)
                        {
                            counter++;
                            var vg = new Guid(group);
                            var loadedVg = visitorGroupRepo.Load(vg);
                            if (loadedVg != null)
                            {
                                if (counter == 1)
                                {
                                    names = visitorGroupRepo.Load(vg).Name;
                                }
                                else if (counter < item.AllowedRoles.Count())
                                {
                                    names = names + ", " + visitorGroupRepo.Load(vg).Name;
                                }
                                else
                                {
                                    names = names + " and " + visitorGroupRepo.Load(vg).Name;
                                }
                            }
                        }
                        currentContent.VisitorGroups = names;
                    }
                }
            }
            return PartialView(string.Format("~/Views/Shared/Blocks/{0}.cshtml", currentContent.GetOriginalType().Name), currentContent);
        }

View

@using EPiServer.Editor
@model SiteBlockData

@if (PageEditing.PageIsInEditMode && !String.IsNullOrEmpty(Model.VisitorGroups))
{
    <p>Visible to: @Model.VisitorGroups</p>
}

Any other feedback?

#136981
Sep 19, 2015 0:54
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.