protected override IContent CreateNew(ContentRepresentation content) { //we cannot continue here if the mandatory items are empty (i.e. name, etc...) if (!ModelState.IsValid) { throw ValidationException(ModelState, content); } var contentType = Services.ContentTypeService.GetContentType(content.ContentTypeAlias); if (contentType == null) { ModelState.AddModelError("content.contentTypeAlias", "No content type found with alias " + content.ContentTypeAlias); throw ValidationException(ModelState, content); } //create an item before persisting of the correct content type var created = ContentService.CreateContent(content.Name, content.ParentId, content.ContentTypeAlias, Security.CurrentUser.Id); //Validate properties var validator = new ContentPropertyValidator <IContent>(ModelState, Services.DataTypeService); validator.ValidateItem(content, created); if (!ModelState.IsValid) { throw ValidationException(ModelState, content); } Mapper.Map(content, created); ContentService.Save(created); return(created); }
//create dummy list for testdata public void createDummyList() { //at first some dummy properties int userId = 0; int parentId = 1; DateTime creationDate = DateTime.UtcNow; string theListsname = "ToDo list for ToDo App"; //now begin with filling content var content = cs.CreateContent("List", parentId, "ToDoLists", userId); content.SetValue("List_Name", theListsname); content.SetValue("creationDate", creationDate); cs.SaveAndPublishWithStatus(content); }
public int CreateContentNode(string nodeName, string nodeTypeAlias, int parentNode, Dictionary <string, string> properties, List <string> roles = null) { try { //parentid = 1189 Int32 uid = 0; ContentService cs = (ContentService)ApplicationContext.Current.Services.ContentService; UserService us = (UserService)ApplicationContext.Current.Services.UserService; Umbraco.Core.Models.Membership.IUser usr = us.GetByUsername("*****@*****.**"); if (usr != null) { uid = usr.Id; } // Create the Node under Content parent = (Content)cs.GetById(parentNode); IContent c = (IContent)cs.CreateContent(nodeName, parent, nodeTypeAlias); foreach (string property in properties.Keys) { c.SetValue(property, properties[property].ToString()); } cs.SaveAndPublishWithStatus(c, uid); return(c.Id); } catch (Exception ex) { return(0); } }
/// <summary> /// Create and publish the document programatically /// </summary> /// <param name="nodeName"></param> /// <param name="properties"></param> /// <param name="documentType"></param> /// <param name="parentId"></param> /// <returns>node id</returns> public int CreateDetailContentNode(string nodeName, Dictionary <string, string> properties, List <string> roles = null) { try { //parentid = 1189 Int32 uid = 0; ContentService cs = (ContentService)ApplicationContext.Current.Services.ContentService; UserService us = (UserService)ApplicationContext.Current.Services.UserService; Umbraco.Core.Models.Membership.IUser usr = us.GetByUsername("*****@*****.**"); if (usr != null) { uid = usr.Id; } // Create the Node under Content parent = (Content)cs.GetById(1189); Content c = (Content)cs.CreateContent(nodeName, parent, "propertyDetail"); // Add values to the generic properties of the document if (properties != null) { foreach (string property in properties.Keys) { //c.getProperty(properties[property]).Value = properties[property]; //c.Properties. } } // Set the publish status of the document and there by create a new version /*if (roles != null) * { * int loginDocId = Constants.NODE_ID_HOME; * int errorDocId = Constants.NODE_ID_HOME; * umbraco.cms.businesslogic.web.Access.ProtectPage(false, d.Id, loginDocId, errorDocId); * foreach (string role in roles) * { * umbraco.cms.businesslogic.web.Access.AddMembershipRoleToDocument(d.Id, role); * } * }*/ // d.Publish(u); // Tell the runtime environment to publish this document cs.Publish(c, uid); return(c.Id); } catch (Exception ex) { return(0); } }
/// <summary> /// BlogHolder /// </summary> /// <param name="name"></param> /// <param name="parentId"></param> /// <param name="userId"></param> /// <returns></returns> private IContent BlogHolder(string name, int parentId, int userId) { var contentService = new ContentService(); var target = contentService.CreateContent( name, parentId, VelstandDocumentType.BlogHolder, userId ); try { // パンくず非表示フラグをtrueにする target.SetValue(VelstandProperty.IsHiddenBreadCrumb, true); } catch (Exception ex) { LogHelper.Error(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, ex.Message, ex); } return(target); }
/// <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> /// <typeparam name="TParent"></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, TParent>(TParent parent, T newItem) where T : class where TParent : 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); }