Virtual Happy Hour this month, Jun 28, we'll be getting a sneak preview at our soon to launch SaaS CMS!

Try our conversational search powered by Generative AI!

Deletion of Pages and blocks for a specific language


Hi Team,

Im using Episerver 11.12

I have designed a gadget which will allow the user to delete the content for the selected language. So when the user selects the content page in the dropdown and the language and click on delete below line of code executes in the backend and it deletes the page content for that language.

DataFactory.Instance.DeleteLanguageBranch(page.PageLink, language, AccessLevel.Delete);

But i noticed that it only deletes the page and not the block in that language which was inside that page. 

So for example: If i have a page1 in language(en, de) which has block1 in language(en, de) by the help of the above code only page1 de language gets deleted and if we look inside block1 i can see both languages present.

Can anyone help me on how i can delete the content inside those pages for that language.

Feb 20, 2020 15:29

Blocks / Content isn't directly linked to a page, they are held seperately

You need to when going to delete a page look at the content area(s) on that page and loop through and delete the blocks as well

The only issue with this is those blocks might be used on other pages, so you could cause issues doing this

Feb 20, 2020 15:43

Hi Swati,

Yikes—this sounds kinda risky. How’d you know a block is not being used elsewhere?

First thing, don’t use the DataFactory: you should be using the IContentRepository and dependency injection (DataFactory is legacy).

The closest approximation of what you want to do would be to use the IContentSoftLinkRepository, a very simple (read: untested) example would be:

// Get all the soft links for the specified page
var softLinks = _contentSoftLinkRepository.Load(page.PageLink).ToList();

// Use the Content Repository to load the content, filters to only the blocks
var blocks = _contentRepository.GetItems(softLinks.Select(x => x.ReferencedContentLink), new CultureInfo(language)).OfType<BlockData>();

Note that a block could be referenced a few different ways: in a ContentArea, in a list of ContentReferences, in a rich text area or as a ContentReference. The above example wouldn't return the block if it was reference via a ContentReference property (although this is potentially unlikely) because it wouldn't be a soft link.

Edited, Feb 20, 2020 16:03
Swati - Feb 21, 2020 7:48
Thank you so much Jack for the feedback , i have corrected my code accordingly and also tried your approach it does give me the list of block inside that page.
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.