public FooterSettingsVM(IFooterSettings item, SitecoreContext context = null) { GlassContext = context ?? new SitecoreContext(); FooterSettings = item; Navigation = Sitecore.Context.Database.GetItem(Sitecore.Context.Site.StartPath) .Children.Select(c => GlassContext.Cast <IBasePage>(c)); }
/// <summary> /// Deletes an item /// </summary> /// <param name="item"></param> /// <typeparam name="T"></typeparam> /// <exception cref="MapperException"></exception> public void Delete <T>(T item) where T : class { var type = GlassContext.GetTypeConfiguration <UmbracoTypeConfiguration>(item) as UmbracoTypeConfiguration; var umbItem = type.ResolveItem(item, ContentService); if (umbItem == null) { throw new MapperException("Content not found"); } ContentService.Delete(umbItem); }
/// <summary> /// Writes to item. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="target">The target.</param> /// <param name="content">The content.</param> /// <param name="config">The config.</param> public void WriteToItem <T>(T target, IContent content, UmbracoTypeConfiguration config = null) { if (config == null) { config = GlassContext.GetTypeConfiguration <UmbracoTypeConfiguration>(target); } var savingContext = new UmbracoTypeSavingContext { Config = config, Content = content, Object = target }; //ME-an item with no versions should be null SaveObject(savingContext); }
/// <summary> /// Saves the specified target. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="target">The target.</param> /// <exception cref="System.NullReferenceException">Can not save class, could not find configuration for {0}.Formatted(typeof(T).FullName)</exception> /// <exception cref="MapperException">Could not save class, conent not found</exception> public void Save <T>(T target) { //TODO: should this be a separate context // UmbracoTypeContext context = new UmbracoTypeContext(); //TODO: ME - this may not work with a proxy var config = GlassContext.GetTypeConfiguration <UmbracoTypeConfiguration>(target); if (config == null) { throw new NullReferenceException("Can not save class, could not find configuration for {0}".Formatted(typeof(T).FullName)); } var item = config.ResolveItem(target, ContentService); if (item == null) { throw new MapperException("Could not save class, conent not found"); } WriteToItem(target, item, config); }
/// <summary> /// Creates a new Umbraco class. /// </summary> /// <typeparam name="T">The type of the new item to create. This type must have either a TemplateId or BranchId defined on the UmbracoClassAttribute or fluent equivalent</typeparam> /// <param name="parent">The parent of the new item to create. Must have the UmbracoIdAttribute or fluent equivalent</param> /// <param name="newItem">New item to create, must have the attribute UmbracoInfoAttribute of type UmbracoInfoType.Name or the fluent equivalent</param> /// <returns></returns> /// <exception cref="MapperException"> /// Failed to find configuration for new item type {0}.Formatted(typeof(T).FullName) /// or /// Failed to find configuration for parent item type {0}.Formatted(typeof(int).FullName) /// or /// Could not find parent item /// or /// The type {0} does not have a property with attribute UmbracoInfo(UmbracoInfoType.Name).Formatted(newType.Type.FullName) /// or /// Failed to create item /// </exception> public T Create <T>(int parent, T newItem) where T : class { UmbracoTypeConfiguration newType; try { newType = GlassContext.GetTypeConfiguration <UmbracoTypeConfiguration>(newItem); } catch (Exception ex) { throw new MapperException("Failed to find configuration for new item type {0}".Formatted(typeof(T).FullName), ex); } UmbracoTypeConfiguration parentType; try { parentType = GlassContext.GetTypeConfiguration <UmbracoTypeConfiguration>(parent); } catch (Exception ex) { throw new MapperException("Failed to find configuration for parent item type {0}".Formatted(typeof(int).FullName), ex); } var pItem = parentType.ResolveItem(parent, ContentService); if (pItem == null) { throw new MapperException("Could not find parent item"); } var nameProperty = newType.Properties.Where(x => x is UmbracoInfoConfiguration) .Cast <UmbracoInfoConfiguration>().FirstOrDefault(x => x.Type == UmbracoInfoType.Name); if (nameProperty == null) { throw new MapperException("The type {0} does not have a property with attribute UmbracoInfo(UmbracoInfoType.Name)".Formatted(newType.Type.FullName)); } string tempName = Guid.NewGuid().ToString(); var content = ContentService.CreateContent(tempName, pItem, newType.ContentTypeAlias); if (content == null) { throw new MapperException("Failed to create item"); } //write new data to the item WriteToItem(newItem, content); //then read it back var typeContext = new UmbracoTypeCreationContext { Content = content, UmbracoService = this }; newType.MapPropertiesToObject(newItem, this, typeContext); return(newItem); }
public GlassService(GlassContext db, IMapper mapper) { _db = db; _mapper = mapper; }