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

Try our conversational search powered by Generative AI!

Copy Blocks in Episerver programmatically and saving then in the content folder "For this page"

Vote:
 

Hi everyone,

I'm trying to copy a block to another page that will be migrated. The "copied" block should be saved in the content folder "For this page"

What is the best approach?

var textContainerClone = textContainer.CreateWritableClone() as IContent;

textContainerClone.ContentGuid = Guid.Empty;
textContainerClone.ParentLink = null;

this._contentRepository.Save(textContainerClone);

I get the following error if I copy the block by code or in the cms:

Failed to copy page 'In the Field with GE Healthcare India' to 'In the Field With GE Healthcare India'.:[Importing content 71077_158727] Exception: Value cannot be null. Parameter name: source

There is another way to do this?

Thank you

#193734
Edited, Jun 04, 2018 14:57
Vote:
 

In short if you want to copy a block to some content asset folder I suggest you do as:

  1. Use ContentAssetHelper.GetOrCreateAssetFolder (will give you the "For this page" folder)
  2. Copy the block to the folder using IContentRepository.Copy method (where source is a reference to the block to copy and detination is a reference to the folder you get from step 1)
#193885
Jun 07, 2018 9:35
Vote:
 

Something like this

var repo = ServiceLocator.Current.GetInstance<IContentRepository>();
var forAllSites = ContentReference.GlobalBlockFolder;

var newBlockInstance = repo.GetDefault<BlockType>(parentLink: forAllSites) as IContent;

#214637
Jan 09, 2020 14:17
* 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.