Try our conversational search powered by Generative AI!

Problems setting property value programmatically when page has internal/local block

Vote:
 

Hi!

I am trying to build a plugin that will make it possible to copy all the pages/blocks from one language to another - to avoid the hazzle of copy-paste when creating a language like UK english from US. At the moment it is working with folders, shared blocks and pages. But somehow I can't get the internal blocks values to get copied alongside the page. I've tried debugging it and the value get's set on the new language branch of the page. But somehow when it is saved, the values are not kept. I've tried so many solutions now that I feel lost in space :)

I've removed some code for simplicity. This snippet is only for the pages.

// Loop through all pages and save em'.
            foreach (var page in pages)
            {
                var pageData = contentTypeRepository.Load(page.PageTypeID);
                var originalPage = page;

                var newBranchPage = contentRepository
                                    .CreateLanguageBranch<PageData>(originalPage.ContentLink, destinationLanguageSelector)
                                    .CreateWritableClone();

                // Here newBranchPage.Property["MyInternalBlock"] will have null values on CultureSpecific properties.

                newBranchPage.PageName = page.PageName;

                foreach (var propertyDefinition in pageData.PropertyDefinitions)
                {
                    if (propertyDefinition.LanguageSpecific)
                    {
                        // Regular property
                        newBranchPage.Property[propertyDefinition.Name].Value = originalPage.Property[propertyDefinition.Name].Value;
                    }
                    else if (propertyDefinition.Type.DefinitionType == typeof(PropertyBlock<BlockData>))
                    {
                        // We are dealing with an internal block...
                        var originalBlockData = originalPage.Property[propertyDefinition.Name].Value as BlockData;
                        var blockData = originalBlockData.CreateWritableClone();
                        newBranchPage.Property[propertyDefinition.Name].Value = blockData;

                        // Here newBranchPage.Property["MyInternalBlock"] will have correct values.
                    }
                }

                contentRepository.Save(newBranchPage, SaveAction.Publish, AccessLevel.NoAccess);
            }

    

Thanks for your help. Cheers!

#80843
Feb 03, 2014 17:29
Vote:
 

Hey Bjorn,

If I understand you correctly, you need to save internal block values. In this case, you are not supposed to create a writable clone. I think you are supposed to loop through the properties of blockData and set them on the newBranchPage (like newPage.Block.Property[xx] = oldPage.Block.Property[yy]). Since the properties of a local block exist as a part of currentPage, they need to be set on the page.

Let me know if this helps.

#80931
Feb 05, 2014 10:27
Vote:
 

I have noticed the same behaviour when you use typed pages as well:

I.E. when setting the local block directly, like:

MyCustomPage.MyLocalBlockProperty = CreateNewBlock();

The values will not persist through save.

But setting the values separately, like:

MyCustomPage.MyLocalBlockProperty.Property1 = Property1Value;

Will work fine.

So, as Marija says above, don't set local blocks on page-property level.


                        
#122058
May 26, 2015 10:33
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.