Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Maby a stupid question but...

Vote:
 

I am feeling a litte stupid now, but I cannot figure out how to create a instance of a blockdata and add it to a contentarea by code.

There are no problems adding blockdata to the content area but I have problem with how to create my new instance of my blockdata and save it to the database.

When I request the once existing there are all IContent, but when trying to create a new I get this error that it is not inheriting from IContent.

I define the blockdata like this:

namespace CUSTOMER.Web.Models.Blocks
{
     [ContentType(
        DisplayName = "My Block",
        GUID = "c2b0b458-6cc2-4633-8a5e-213ddd9c2191",
        Description = "Description on the block",
        GroupName = CustomGroupNames.CommonFunctions,
        Order = 100)]
    public class ApplicationStepBlock : BaseBlockData
    {
         [CultureSpecific]
         [Display(
             Name = "Headline",
             Description = "Headline",
             GroupName = SystemTabNames.Content,
             Order = 1)]
         public virtual string ApplicationStepTitle { get; set; }

         [CultureSpecific]
         [Display(
             Name = "MainBody",
             Description = "",
             GroupName = SystemTabNames.Content,
             Order = 2)]
         public virtual XhtmlString ApplicationStepBody { get; set; }

         [Display(Name = "Number",
          GroupName = SystemTabNames.Content,
          Order = 4)]
         public virtual Double ApplicationStepNumber { get; set; }
    }
}

Then I try to create a new instance of this like this:

var repository = ServiceLocator.Current.GetInstance();

var applicationStep= new ApplicationStepBlock();
                            annsokningssteg.ApplicationStepNumber = 0;
                            annsokningssteg.ApplicationStepTitle = "TITLE OF THIS";
                            annsokningssteg.ApplicationStepBody = "BIG body with info";

repository.Save(applicationStep, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);


Propably I am not supposed to use IContentRepository but I can not figure out what to use

Please help my empty brain this late in the week

#112922
Nov 07, 2014 14:33
Vote:
 

Use GetDefault to create the block. Cast it to IContent when saving.

            var myBlock = repository.GetDefault<MyBlockType>(parentReference);

            ((IContent) myBlock).Name = "Wasabi and stuff";

            repository.Save(myBlock as IContent, EPiServer.DataAccess.SaveAction.Publish, EPiServer.Security.AccessLevel.NoAccess);

If you want to get the page's folder reference as the parentRefence, you can do:

var contentAssetHelper = ServiceLocator.Current.GetInstance<contentassethelper>();
ContentAssetFolder folder = contentAssetHelper.GetOrCreateAssetFolder(myPage.ContentLink);

and then use the folder's contentLink property

#112924
Edited, Nov 07, 2014 14:47
Vote:
 

If you're wondering why BlockData does not implement IContent in the first place, you should check out this blogg: http://world.episerver.com/Blogs/Johan-Bjornfot/Dates1/2012/11/Shared-blocks--IContent/ 

#112925
Nov 07, 2014 15:00
Vote:
 

Thanks, I thought it was so simple but my friday afternoon brain did not figure it out.

#112926
Nov 07, 2014 15:11
Vote:
 

Luckily, the weekend is just around the corner :-)

#112927
Nov 07, 2014 15:17
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.