I really need some help setting a default value for a label property on my document types. After searching it has lead me to using events but im not having much luck.
I have two document types in umbraco called homeFixture & awayFixture and i would like to set the default value of a label (that will never change) when a new page is created. I have the following code which sets the value when i choose to create a new page
However, when i select save and publish the default values that i am seeing when i create the page are disappearing. Does anyone know how i can persist these values once saved and published?
I'm not suggesting you change anything if its working, just seeing if you are aware of a new feature i came across recently. Its so new i havent used is but it looked potentially interesting for this sort of thing.
Thanks for the tip! maybe something i can take a look into. I have it working but i still have an issue that you may be able to advise on?
so i have the following code that checks whether the teamType (DDL value) is set as either first or reserve and apends the label value accordingly.
public class DocTypeCreateEventHandler : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
ContentService.Saving += ContentService_Saving;
}
void ContentService_Saving(IContentService sender, SaveEventArgs<IContent> e)
{
foreach (var node in e.SavedEntities)
{
//get the team page to a list
var teamPage = node.Ancestors().Where(x => x.ContentType.Alias == "teamPage").ToList();
if (node.ContentType.Alias == "homeFixture")
{
foreach (var page in teamPage)
{
var teamType = umbraco.library.GetPreValueAsString(int.Parse(page.GetValue("teamType").ToString()));
if (teamType == "first")
{
node.SetValue("location", "Home");
node.SetValue("homeTeam", "Cefn Albion");
}
else
{
node.SetValue("location", "Home");
node.SetValue("homeTeam", "Cefn Albion Reserves");
}
}
}
if (node.ContentType.Alias == "awayFixture")
{
foreach (var page in teamPage)
{
var teamType = umbraco.library.GetPreValueAsString(int.Parse(page.GetValue("teamType").ToString()));
if (teamType == "first")
{
node.SetValue("location", "Away");
node.SetValue("awayTeam", "Cefn Albion");
}
else
{
node.SetValue("location", "Away");
node.SetValue("awayTeam", "Cefn Albion Reserves");
}
}
}
}
}
}
However, the following line of code returns 0 on the first save and publish and the values are not updated.
var teamPage = node.Ancestors().Where(x => x.ContentType.Alias == "teamPage").ToList();
once it has published and then i save and publish again the labels update as per the logic in my code. I suspect it has something to do with the face that the page doesnt exist yet?
Im not sure but that might be your problem exactly. You could see if there is anything else in the function arguments. I dont know if there would be a parentid property you could rely on i know at one point early on that is just -1 which wouldnt be any good. Path would also give you a parent to then use ancestor on if its populated yet. And then theres maybe looking at the request grabbing the id from the url when it looks like ...?create=true. If you look at the behaviour i think the id gets populated with the tree node clicked when creating which gives you some context.
Set default value of property using events
Hi everyone,
I really need some help setting a default value for a label property on my document types. After searching it has lead me to using events but im not having much luck.
I have two document types in umbraco called homeFixture & awayFixture and i would like to set the default value of a label (that will never change) when a new page is created. I have the following code which sets the value when i choose to create a new page
However, when i select save and publish the default values that i am seeing when i create the page are disappearing. Does anyone know how i can persist these values once saved and published?
I have tried using this code to but having the same issue. It will update whn i select publish but when i revisit the node it disappears.
Been on this for ages - I am using umbraco v7.4
Thanks
Not sure if its correct but using the Content.Saving did the trick.
Please let me know if there is a better way
Paul
I'm not suggesting you change anything if its working, just seeing if you are aware of a new feature i came across recently. Its so new i havent used is but it looked potentially interesting for this sort of thing.
EditorModelEventManager.SendingContentModel
Hi Ian,
Thanks for the tip! maybe something i can take a look into. I have it working but i still have an issue that you may be able to advise on?
so i have the following code that checks whether the teamType (DDL value) is set as either first or reserve and apends the label value accordingly.
However, the following line of code returns 0 on the first save and publish and the values are not updated.
once it has published and then i save and publish again the labels update as per the logic in my code. I suspect it has something to do with the face that the page doesnt exist yet?
Any advice?
Thanks Paul
Im not sure but that might be your problem exactly. You could see if there is anything else in the function arguments. I dont know if there would be a parentid property you could rely on i know at one point early on that is just -1 which wouldnt be any good. Path would also give you a parent to then use ancestor on if its populated yet. And then theres maybe looking at the request grabbing the id from the url when it looks like ...?create=true. If you look at the behaviour i think the id gets populated with the tree node clicked when creating which gives you some context.
Someone else may know better.
is working on a reply...