SaaS CMS has officially launched! Learn more now.

Pulling data from one block (via drag and drop or asset folder) and using that data in the view of another

Vote:
 

Hello, 

New to the forums and to Episerver in general. I have searched up and down Google as well as the Episerver training materials for the solution to this and haven't found one. Here is my situation: 

I have a block called BlogAuthorSpotlight. In this block, I want to grab the data from another block called BlogAuthorBlock. BlogAuthorBlock is meant to simply be a properties block. It is not meant to be rendered in a view. I want to use BlogAuthorBlock's data in several other block's views, and render that data in different ways. In this blog we have an Author Spotlight where the image and what is being shown is different, etc. My dillima is I haven't figured out a good way of doing this. 

I tried creating a variable for BlogAuthorData within BlogAuthorSpotlight using ContentReference and then pulling the properties through to the model of BlogAuthorSpotlight. That didn't work. I really just need some recommendations here, as a code example isn't really needed as this is a super simple issue, I just don't have the knowledge to figure out how to go about it. 

How do I pull the BlogAuthorBlock's data into the BlogAuthorSpotlight block's view in order to have it rendered? Remember, BlogAuthorBlock is simply meant to be properties that can be rendered in several different views. 

I hope I clarifyed this problem. Please let me know what you think. 

#206277
Aug 09, 2019 18:37
Vote:
 

Here are the blocks:

Blog Author Block:

using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.Find.Cms;
using EPiServer.Shell.ObjectEditing;
using EPiServer.SpecializedProperties;
using EPiServer.Web;

namespace AppliedSystems.Features.ContentBlocks.BlogAuthorBlock
{
[ContentType(DisplayName = "Blog Author Block",
GUID = "5f1144de-84ca-44e1-8613-6df80df91a34",
Description = "Use this block to store blog author data for Spotlights and Author Pages. ")]
public class BlogAuthorBlock : BlockData
{


[Display(
Name = "Author's Name",
Description = "Input the Blog Author's name here.",
GroupName = SystemTabNames.Content,
Order = 100)]
public virtual string BlogAuthorFirstName { get; set; }

[CultureSpecific]
[Display(
Name = "Author's Title",
Description = "Input the Blog Author's Title here.",
GroupName = SystemTabNames.Content,
Order = 200)]
public virtual string BlogAuthorTitle { get; set; }


[CultureSpecific]
[Display(
Name = "Author's Company",
Description = "Input the Blog Author's company name here.",
GroupName = SystemTabNames.Content,
Order = 300)]
public virtual string BlogAuthorCompany { get; set; }

[CultureSpecific]
[Display(
Name = "Author's Short Bio",
GroupName = SystemTabNames.Content,
Description = "Input the Blog Author's short bio here.",
Order = 400)]
[UIHint(UIHint.Textarea)]
public virtual string BlogAuthorShortBio { get; set; }


[CultureSpecific]
[Display(
Name = "Author's Long Bio",
GroupName = SystemTabNames.Content,
Description = "Testimonial mandatory",
Order = 500)]
[UIHint(UIHint.Textarea)]
public virtual string BlogAuthorLongBio { get; set; }

[Display(
Name = "Author's Headshot",
GroupName = SystemTabNames.Content,
Order = 600)]
[AllowedTypes(typeof(ImageData))]
public virtual ContentReference BlogAuthorImage { get; set; }


}

}

Blog Author Spotlight Block:

using System.ComponentModel.DataAnnotations;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using EPiServer.DataAnnotations;
using EPiServer.SpecializedProperties;
using EPiServer.Find.Cms;
using AppliedSystems.Features.ContentBlocks;
using EPiServer.Web;
using EPiServer;
using System.Collections.Generic;

namespace AppliedSystems.Features.ContentBlocks.BlogAuthorSpotlightBlock
{

[ContentType(DisplayName = "Blog Author Spotlight Block", GUID = "a3d76ad2-bb37-41ab-9eee-6daf7a278455", Description = "Use this block to create an Author Spotlight")]
public class BlogAuthorSpotlightBlock : BlockData
{


[Display(
Name = "Eyebrow Text",
Description = "Input eyebrow text here",
GroupName = SystemTabNames.Content,
Order = 100)]
public virtual string BlogAuthorSpotlightEyebrowText { get; set; }

 [Display(
Name = "Author Block",
Description = "Click and Drag Author Block here",
GroupName = SystemTabNames.Content,
Order = 200)]
[AllowedTypes(new[] { typeof(BlogAuthorBlock.BlogAuthorBlock) })]
public virtual ContentReference  BlogSpotlightAuthorData { get; set; }


[Display(
Name = "Call To Action Text",
GroupName = SystemTabNames.Content,
Order = 300)]
public virtual string BlogAuthorSpotlightCTAText { get; set; }

[Display(Name = "URL for CTA",
GroupName = SystemTabNames.Content,
Order = 500)]
public virtual Url RedirectUrl { get; set; }

}

}

I am trying to get the data from a dropped in BlogAuthorBlock into the view of BlogAuthorSpotlight

Hope this Clarification helped some. 

#206278
Aug 09, 2019 18:46
Vote:
 

Hi,

It looks like you'd need to convert the BlogSpotlightAuthorData ContentReference into a BlogAuthorBlock which you can do by using an instance of IContentLoader. As an Example:

var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
var authorBlock = contentLoader.Get<BlogAuthorBlock>(currentBlock.BlogSpotlightAuthorData);

You'll probably want to do that in a controller (BlogAuthorSpotlightBlockController) and pass the author block (or just it's property values) in a view model along with everything else you need to the view for rendering. You'll probably also want to do some null checking and get the IContentLoader via dependency injection rather than ServiceLocator as I've shown above.

#206279
Aug 09, 2019 19:18
* 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.