/// <summary> /// Creates the type. /// </summary> /// <param name="type">The type.</param> /// <param name="content">The content.</param> /// <param name="isLazy">if set to <c>true</c> [is lazy].</param> /// <param name="inferType">if set to <c>true</c> [infer type].</param> /// <param name="constructorParameters">Parameters to pass to the constructor of the new class. Must be in the order specified on the consturctor.</param> /// <returns></returns> /// <exception cref="System.NotSupportedException">Maximum number of constructor parameters is 4</exception> public object CreateType(Type type, IContent content, bool isLazy, bool inferType, params object[] constructorParameters) { if (content == null) { return(null); } if (constructorParameters != null && constructorParameters.Length > 4) { throw new NotSupportedException("Maximum number of constructor parameters is 4"); } var creationContext = new UmbracoTypeCreationContext { UmbracoService = this, RequestedType = type, ConstructorParameters = constructorParameters, Content = content, InferType = inferType, IsLazy = isLazy }; var obj = InstantiateObject(creationContext); return(obj); }
/// <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); }