Skip to content

Instantly share code, notes, and snippets.

@jonupchurch
Last active December 17, 2015 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonupchurch/5549874 to your computer and use it in GitHub Desktop.
Save jonupchurch/5549874 to your computer and use it in GitHub Desktop.
[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