Skip to content

Instantly share code, notes, and snippets.

@akeilox
Forked from biapar/InitArcheType
Created October 18, 2015 11:03
Show Gist options
  • Save akeilox/fbf7e724a3ea714f8e19 to your computer and use it in GitHub Desktop.
Save akeilox/fbf7e724a3ea714f8e19 to your computer and use it in GitHub Desktop.
How to read and write to Umbraco Archetype
var archetypeValueAsString = member.GetValue<string>("figli");
var member = Services.MemberService.GetById(user.Id);
if (archetypeValueAsString == null)
{
//var archetype2 = JsonConvert.DeserializeObject<ArchetypeModel>(archetypeValueAsString);
ArchetypeModel a = new ArchetypeModel();
a.Fieldsets = new List<ArchetypeFieldsetModel>(){
new ArchetypeFieldsetModel() {
Alias = "figlio",
Properties = new List<ArchetypePropertyModel>()
{
new ArchetypePropertyModel() {
Alias = "nome",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "cognome",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "codiceFiscale",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "dataNascita",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "luogoNascita",
Value = ""
}
},
Disabled = false,
},
new ArchetypeFieldsetModel() {
Alias = "figlio",
Properties = new List<ArchetypePropertyModel>()
{
new ArchetypePropertyModel() {
Alias = "nome",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "cognome",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "codiceFiscale",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "dataNascita",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "luogoNascita",
Value = ""
}
},
Disabled = false,
},
new ArchetypeFieldsetModel() {
Alias = "figlio",
Properties = new List<ArchetypePropertyModel>()
{
new ArchetypePropertyModel() {
Alias = "nome",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "cognome",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "codiceFiscale",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "dataNascita",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "luogoNascita",
Value = ""
}
},
Disabled = false,
},
new ArchetypeFieldsetModel() {
Alias = "figlio",
Properties = new List<ArchetypePropertyModel>()
{
new ArchetypePropertyModel() {
Alias = "nome",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "cognome",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "codiceFiscale",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "dataNascita",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "luogoNascita",
Value = ""
}
},
Disabled = false,
},
new ArchetypeFieldsetModel() {
Alias = "figlio",
Properties = new List<ArchetypePropertyModel>()
{
new ArchetypePropertyModel() {
Alias = "nome",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "cognome",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "codiceFiscale",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "dataNascita",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "luogoNascita",
Value = ""
}
},
Disabled = false,
},
new ArchetypeFieldsetModel() {
Alias = "figlio",
Properties = new List<ArchetypePropertyModel>()
{
new ArchetypePropertyModel() {
Alias = "nome",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "cognome",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "codiceFiscale",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "dataNascita",
Value = ""
},
new ArchetypePropertyModel() {
Alias = "luogoNascita",
Value = ""
}
},
Disabled = false,
}
};
a.SerializeForPersistence();
member.SetValue("figli", JsonConvert.SerializeObject(a));
Services.MemberService.Save(member);
}
var archetype = JsonConvert.DeserializeObject<ArchetypeModel>(archetypeValueAsString);
famiglia.figli.numero = archetype.Count();
foreach (var fieldset in archetype)
{
var figlio = new FiglioFormViewModel();
if (fieldset.GetValue<string>("codiceFiscale") != null)
figlio.CodiceFiscale = fieldset.GetValue<string>("codiceFiscale");
else
figlio.CodiceFiscale = "";
if (fieldset.GetValue<string>("nome") != null)
figlio.Nome = fieldset.GetValue<string>("nome");
else
figlio.Nome = "";
if (fieldset.GetValue<string>("cognome") != null)
figlio.Cognome = fieldset.GetValue<string>("cognome");
else
figlio.Cognome = "";
if (fieldset.GetValue<string>("dataNascita") != null)
figlio.dataNascita = fieldset.GetValue<string>("dataNascita");
else
figlio.dataNascita = "";
if (fieldset.GetValue<string>("luogoNascita") != null)
figlio.luogoNascita = fieldset.GetValue<string>("luogoNascita");
else
figlio.luogoNascita = "";
famiglia.figli.elenco.Add(figlio);
}
var archetype = JsonConvert.DeserializeObject<ArchetypeModel>(archetypeValueAsString);
foreach (var item in archetype.Fieldsets)
{
var itemProperty = item.Properties.FirstOrDefault(x => x.Alias == "nome");
if (itemProperty != null)
{
itemProperty.Value = model.figli.elenco[i].Nome;
}
var itemProperty2 = item.Properties.FirstOrDefault(x => x.Alias == "cognome");
if (itemProperty2 != null)
{
itemProperty2.Value = model.figli.elenco[i].Cognome;
}
var itemProperty3 = item.Properties.FirstOrDefault(x => x.Alias == "codiceFiscale");
if (itemProperty3 != null)
{
itemProperty3.Value = model.figli.elenco[i].CodiceFiscale;
}
var itemProperty4 = item.Properties.FirstOrDefault(x => x.Alias == "dataNascita");
if (itemProperty4 != null)
{
itemProperty4.Value = model.figli.elenco[i].dataNascita;
}
var itemProperty5 = item.Properties.FirstOrDefault(x => x.Alias == "luogoNascita");
if (itemProperty5 != null)
{
itemProperty5.Value = model.figli.elenco[i].luogoNascita;
}
i++;
}
member.SetValue("figli", JsonConvert.SerializeObject(archetype));
}
Services.MemberService.Save(member);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment