Last active
December 17, 2015 04:19
-
-
Save jonupchurch/5549874 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[SitecoreClass(TemplateId = "My-Guid-Here")] | |
public class DictionaryDomain : GlassBase {} | |
[SitecoreClass(TemplateId = "My-Guid-Here")] | |
public class DictionaryEntry : GlassBase | |
{ | |
[SitecoreField] | |
public virtual string Key { get; set; } | |
[SitecoreField] | |
public virtual string Phrase { get; set; } | |
} | |
[WebMethod] | |
public string MakeStates() | |
{ | |
ISitecoreContext context = new SitecoreContext(); | |
string[] states = System.IO.File.ReadAllLines(Server.MapPath("StateList.txt")); | |
DictionaryDomain dictionary = context.GetItem<DictionaryDomain>(Guid.Parse(Core.Constants.Dictionaries.States.DomainId)); | |
PopulateValues(states, dictionary); | |
return "Successful!"; | |
} | |
/// <summary> | |
/// Adds items to a dictionary given the dictionary domain | |
/// and the existing items | |
/// </summary> | |
/// <param name="items"></param> | |
/// <param name="existingItems"></param> | |
/// <param name="dictionary"></param> | |
private static void PopulateValues(IEnumerable<string> items, DictionaryDomain dictionary) | |
{ | |
var existingItems = | |
dictionary.GetChildren<DictionaryEntry>(Core.Constants.Dictionaries.DictionaryTemplateName).ToArray(); | |
foreach (string item in items) | |
{ | |
//Check to see if the item already exists before adding it | |
if (existing.Any(a => a.Key != null && a.Key.ToUpper() == item.ToUpper()) || string.IsNullOrEmpty(item)) continue; | |
ISitecoreService service = new SitecoreService("master"); | |
using (new SecurityDisabler()) | |
{ | |
DictionaryEntry entry = | |
service.Create(dictionary, | |
new DictionaryEntry | |
{ | |
Key = item.ToUpper(), | |
Phrase = item.ToUpper(), | |
Name = item.ToUpper() | |
}); | |
service.Save(entry); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment