Try our conversational search powered by Generative AI!

ContenArea modification does not work

Vote:
 

I m trying to modify content area programmaticly, but can not get it work. By hooking save event on episerver, I should be able to modify the content area on savingContent.

this simple logic is to delete content items on contentarea but does not save and refresh backend page.

 var page = e.Content as StandardPage;
            if (page.MyContentArea != null && page.MyContentArea.Items.Any())
            {
                var clone = page.CreateWritableClone() as StandardPage;
                foreach (var contenAreaItem in page.MyContentArea.Items)
                {
                    clone.MyContentArea.Items.Remove(contenAreaItem);
                }

                DataFactory.Instance.Save(clone, SaveAction.Save, AccessLevel.NoAccess);
            }


any help appreciated

#133315
Sep 01, 2015 15:08
Vote:
 

Can you please provide full code (interested in part how you subscribe to event)?

#133329
Sep 01, 2015 22:35
Vote:
 

Hi,

Maybe you need to assign contentArea to property

var page = e.Content as StandardPage;
if (page.MyContentArea != null && page.MyContentArea.Items.Any())
{
	var contentArea = page.MyContentArea;
    var clone = page.CreateWritableClone() as StandardPage;
    foreach (var contenAreaItem in contentArea.Items)
    {
        contentArea.Items.Remove(contenAreaItem);
    }
	
	// assign contentArea to property
	page.MyContentArea = contentArea; 

    DataFactory.Instance.Save(clone, SaveAction.Save, AccessLevel.NoAccess);
}
#133332
Sep 01, 2015 23:38
Vote:
 

[Valdis] : this is the event that u subscribe.  DataFactory.Instance.SavingContent += SavingContent;

[Grzegorz] : yes, you right I needed to assing contentArea to property.

Thank you for your help.

#133341
Sep 02, 2015 10:38
Vote:
 

If you do SavingContent (before the actual save) you shouldn't need to save yourself.

If you do SavedContent (after saving has been done) you need to.

#133345
Sep 02, 2015 11:15
Vote:
 

I wasn't aware that you need to re-assign CA to the property to get it populated. Weird

#133346
Sep 02, 2015 11:19
Vote:
 

That is the most common way I've seen to trigger IsModified on the CA.

EPiServer doesn't care about the value if IsModified is false

#133347
Sep 02, 2015 11:21
Vote:
 

I look at CA as observable collection and any modification should set IsDirty/IsModified/IWantToSave/Whatever flag to true, so EPiServer storage engine knows what to do. Thanks for tip.

#133348
Sep 02, 2015 11:24
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.