November Happy Hour will be moved to Thursday December 5th.

Programmatically save block?

Vote:
 

How do I go about saving a block programmatically? I though it would be the same way as saving a page but I get: Argument type 'EPiServer.Core.BlockData' is not assinable to parameter type 'EPiServer.Core.IContent'

My code looks like this:

            var widget =
                EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance()
                    .Get(contentAreaItem.ContentLink) as MyBaseBlock;
            var clone = widget.CreateWritableClone();
            clone.MyProperty = "SomeValue";
            contentRepository.Save(clone, SaveAction.Publish, AccessLevel.Administer);
#118929
Mar 17, 2015 17:43
Vote:
 

Hi Andreas,

For the Save mthod simply cast your clone object as IContent.

e.g.

contentRepository.Save( (IContent)clone, SaveAction.Publish, AccessLevel.Administer);
#118937
Mar 17, 2015 21:25
Vote:
 

The reason for this is because a BlockData can either be a shared block (IContent) or a local block (a set of properties on a page). So you need to tell EPiServer that this block is actually a IContent.

Joel Abrahamsson have a great article about the the differences.

/T

#118943
Mar 18, 2015 0:23
Vote:
 

Ok wouldn't it be better to have an overload that accepts blockdata then? :)

#118959
Mar 18, 2015 8:47
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.