I have a string field (array) and some values that should be as default when the page is created. Can I add default values to a string field in code definition?
Litium version: 6
I have a string field (array) and some values that should be as default when the page is created. Can I add default values to a string field in code definition?
Litium version: 6
You can set a default for a single string value in a code definition, see for example the LoginPage definition in the accelerator which use it for many properties:
[Property(PropertyCollectionTypes.CONTENT, typeof(StringShortProperty), IsMandatory = true, Group = "Glömt lösenord")]
[Translation("Text för länk tillbaka till inloggningssidan", "sv-se")]
[Translation("Text link back to login page", "en-us")]
[Default("Back to login page")]
public virtual string BackToLoginPageLinkText { get; set; }
If you want to set default values for a string array you can export your pagetype in backoffice, set values in the XML and then import again:
<?xml version="1.0" encoding="UTF-8"?>
<PageType Name="Article" CanBeArchived="true" CanBeVersioned="true" CanBeMovedToTrashcan="true" CanBeInMenu="true" CanBeInSiteMap="true" CanBeInVisitStatistics="true" CanBeLinkedTo="true" CanBePrinted="true" CanBeSearched="true" AutoArchiveWeeks="0" VersionsToKeep="30" EditableInGUI="true" EditPage="" CanDeletePageType="true" CanBeMasterPage="true" PageTypeCategory="0" PossibleChildPageTypes="NewsList,ProductListPage,Sections,SiteMap,Article" PossibleParentPageTypes="Article,SearchResult,StartPage" WebSites="*" UseSecureConnection="false">
<Content>
...
<StringShortProperty Index="6" Name="Test" IsArray="true" IsMandatory="false" ShowInGuide="true" Group="">
<Values>
<StringShortValue Index="0">Test1</StringShortValue>
<StringShortValue Index="1">Test2</StringShortValue>
<StringShortValue Index="2">Test3</StringShortValue>
</Values>
</StringShortProperty>
</Content>
...
</PageType>
You can also set the values using a startup task to deploy it with code but outside of your pagetype definition:
public class ArticlePropertySetup : IStartupTask
{
public void Start()
{
var articlePageType = ModuleCMS.Instance.PageTypes.GetPageType(nameof(Article));
if (articlePageType == null)
throw new ArgumentNullException(nameof(articlePageType));
var property = articlePageType.Content[nameof(Article.StringArrayDefaultsTest)] as StringShortProperty;
if (property == null)
throw new Exception($"Property {nameof(Article.StringArrayDefaultsTest)} not found on pagetype {nameof(Article)}");
property.SetValue(0, "Default value 1", Solution.Instance.SystemToken);
property.SetValue(1, "Default value 2", Solution.Instance.SystemToken);
property.SetValue(2, "Default value 3", Solution.Instance.SystemToken);
}
}