I have to insert some content from database using c#. I am reading the data and placing it into models. Lets say 1 model is
public class Manufacturer
{
public int ManufacturerId { get; set; }
public string ManufacturerName { get; set; }
}
I have same Document type and 1 root document type Manufacturers. that can have multiple content of Manufacturer. Now I have read a list of manufacturers and I need to create a content for all of them. I have
foreach (var man in manufacturers)
{
InsertUmbracoManufacturer(man);
}
and if manufacturer with mamufacturerId(not same as UmbracoId) exists update in else insert new. So far I have:
public void InsertUmbracoManufacturer(Manufacturer man)
{
//get the manufacturer folder
IContentService contentService = ApplicationContext.Current.Services.ContentService;
var rootFolder =contentService.GetRootContent().Where(x => x.ContentType.Alias.Equals("Manufacturers")).First();
if (rootFolder!=null)
{
//get the node with manufacturerId =man.manufacturerId
var m=rootFolder.Children().Where(x=>x.ContentType.Alias.Equals("Manufacturer") && x.Children().Where(y=>y.Properties["manufacturerid"].Equals(man.ManufacturerId)) //not so sure please help
if (m!=null)
{
//if it does exist update it help here too
}
else
{
//if it doesn't exist insert help here too
}
}
I have updated your snippet and have added code as required. Do let me know for any clarification?
public void InsertUmbracoManufacturer(Manufacturer man)
{
//get the manufacturer folder
IContentService cs = ApplicationContext.Current.Services.ContentService;
var rootFolder =contentService.GetRootContent().Where(x => x.ContentType.Alias.Equals("Manufacturers")).First();
if (rootFolder!=null)
{
//get the node with manufacturerId =man.manufacturerId
var m=rootFolder.Children().Where(x=>x.ContentType.Alias.Equals("Manufacturer") && x.Children().Where(y=>y.Properties["manufacturerid"].Equals(man.ManufacturerId)) //not so sure please help
IContent saveNode = null;
if (m!=null)
{
//if it does exist update it help here too
saveNode = cs.GetById(m.Id);
}
else
{
//if it doesn't exist insert help here too
saveNode = cs.CreateContent(man.ManufacturerName, rootFolder.Id, "propertyDetails", 0);
}
.... update all properties
// propertyNode.SetValue("property", "value");
//
cs.SaveAndPublishWithStatus(propertyNode);
Create content from C#
Hi
I have to insert some content from database using c#. I am reading the data and placing it into models. Lets say 1 model is
I have same Document type and 1 root document type Manufacturers. that can have multiple content of Manufacturer. Now I have read a list of manufacturers and I need to create a content for all of them. I have
and if manufacturer with mamufacturerId(not same as UmbracoId) exists update in else insert new. So far I have:
Please help
I have updated your snippet and have added code as required. Do let me know for any clarification?
public void InsertUmbracoManufacturer(Manufacturer man) { //get the manufacturer folder IContentService cs = ApplicationContext.Current.Services.ContentService; var rootFolder =contentService.GetRootContent().Where(x => x.ContentType.Alias.Equals("Manufacturers")).First(); if (rootFolder!=null) { //get the node with manufacturerId =man.manufacturerId var m=rootFolder.Children().Where(x=>x.ContentType.Alias.Equals("Manufacturer") && x.Children().Where(y=>y.Properties["manufacturerid"].Equals(man.ManufacturerId)) //not so sure please help
IContent saveNode = null; if (m!=null) { //if it does exist update it help here too saveNode = cs.GetById(m.Id); } else { //if it doesn't exist insert help here too saveNode = cs.CreateContent(man.ManufacturerName, rootFolder.Id, "propertyDetails", 0); } .... update all properties // propertyNode.SetValue("property", "value"); // cs.SaveAndPublishWithStatus(propertyNode);
Thanks, it is something like that, just m is found more easily:
is working on a reply...