/// <summary> /// Gets the child I ds template. /// </summary> /// <param name="template">The template.</param> /// <param name="itemDefinition">The item definition.</param> /// <param name="context">The context.</param> /// <param name="sqlProvider">The SQL provider.</param> /// <returns> /// IDList. /// </returns> private IDList GetChildIDsTemplate(SitecoreTypeConfiguration template, ItemDefinition itemDefinition, CallContext context, DataProvider sqlProvider) { var fields = new IDList(); var processed = new List <string>(); var sections = template.Properties .Where(x => x.PropertyInfo.DeclaringType == template.Type) .OfType <SitecoreFieldConfiguration>() .Select(x => new { x.SectionName, x.SectionSortOrder }); //If sitecore contains a section with the same name in the database, use that one instead of creating a new one var existing = sqlProvider.GetChildIDs(itemDefinition, context).OfType <ID>().Select(id => sqlProvider.GetItemDefinition(id, context)) .Where(item => item.TemplateID == SectionTemplateId).ToList(); foreach (var section in sections) { if (processed.Contains(section.SectionName) || section.SectionName.IsNullOrEmpty()) { continue; } var record = SectionTable.FirstOrDefault(x => x.TemplateId == itemDefinition.ID && x.Name == section.SectionName); if (record == null) { var exists = existing.FirstOrDefault(def => def.Name.Equals(section.SectionName, StringComparison.InvariantCultureIgnoreCase)); var newId = GetUniqueGuid(itemDefinition.ID + section.SectionName); const int newSortOrder = 100; record = exists != null ? new SectionInfo(section.SectionName, exists.ID, itemDefinition.ID, section.SectionSortOrder) { Existing = true } : new SectionInfo(section.SectionName, new ID(newId), itemDefinition.ID, newSortOrder); SectionTable.Add(record); } processed.Add(section.SectionName); if (!record.Existing) { fields.Add(record.SectionId); } } //we need to add sections already in the db, 'cause we have to foreach (var sqlOne in existing.Where(ex => SectionTable.All(s => s.SectionId != ex.ID))) { SectionTable.Add(new SectionInfo(sqlOne.Name, sqlOne.ID, itemDefinition.ID, 0) { Existing = true }); } return(fields); }