Calling all developers! We invite you to provide your input on Feature Experimentation by completing this brief survey.

 

Export certain properties from page instance and importing on another page instance

Vote:
 

How do I copy only specified properties to another page?

E.g. we have a SettingPage which we want to copy declared properties to the StartPage (then delete the SettingPage).

I found there was a property exporter, but it doesn't seem to do what I thought it was.

var contentInfo = _contentRetriever.CreateRawContentInfo(settingsPage.ContentLink, cultureInfo.Name);
var propertyList = settingsPage.GetType()
				.GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
				.Select(x => x.Name)
				.ToList();

var targetPropertyList = contentInfo.RawContent.Property.Where(x => propertyList.Contains(x.Name)).ToList();
var rawContent = new RawContent();

_propertyExporter.ExportProperties(settingsPage, targetPropertyList, new PropertyExportContext
{
				TransferOptions = new TransferExportOptions
				{
					AutoCloseStream = true,
					ExcludeFiles = false,
					RequiredSourceAccess = AccessLevel.NoAccess,
					TransferType = TypeOfTransfer.Copying,
					IncludeReferencedContentTypes = false,
					ExportPropertySettings = false,
				},
				Output = rawContent,
				Source = settingsPage,
});

Any ideas?

#315360
Edited, Jan 09, 2024 21:35
Vote:
 

There's not really a way to copy the values from one page to another page out of the box. 

How many properties do you have, I'd suggest either writing a job if not many or just writing a SQL script to copy the values which shouldn't be too hard

#315464
Jan 11, 2024 16:28
Vote:
 

That I know. Which is why I experimented with the above code in a scheduled job (executed once).

There are quite a lot of properties, content areas, custom properties etc.

IContentCopyHandler would be nice if you could specifiy which properties :)

I don't really want to do a loop and custom checks for each type - but it doesn't sound like I have a choise.

#315471
Jan 11, 2024 22:12
Vote:
 

I found a way to do it for those whom are interested.

Inject IContentTypeRepository and...

var contentType = _contentTypeRepository.Load(sourcePage.ContentTypeID);

foreach (var definition in contentType.PropertyDefinitions)
{
				var propertyValue =
								sourcePage.GetPropertyValue<object?>(definition.Name);

				if (propertyValue is XhtmlString xhtmlString)
				{
								targetPage.SetPropertyValue(definition.Name,
									new XhtmlString(xhtmlString.ToInternalString()));
				}
				else
				{
								targetPage.SetPropertyValue(definition.Name,
									propertyValue);
				}
}

There are some special handling. E.g 'XhtmlString' and 'ContentArea' but it's doable this way.

#316128
Jan 24, 2024 9:09
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.