Developer forum

Forum » Development » Create items using API

Create items using API

Sten Hougaard
Reply

Hi,

I want to create items from an XML file. I have tried the data integration but did not succede so now I want to know if there is a way of doing it through the API?

I have also tried to generate a file with SQL insert, which allows me to add items directly to the itemType_myItem table. However I cannot add a page which then uses the imported item. Somewhere here on forum I read that I only needed to add content to these columns in the page table like this (for instance):

insert into page (PageParentPageID,PageActive, pagareaid,PageMenuText,PageItemType,PageItemId)
values (2013, 1, 1, 'test', 'myItem', 2)

However that had the effect that the item container (2013) could not be opened in Dynamicweb backend (!).

So how would you suggest that I can import data as items into Dynamicweb?


Med venlig hilsen/Best regards,

Sten Hougaard
Webudvikler

E: sho@1stweb.dk
M: 29850818
A: København/Aarhus . W: www.1stweb.dk
@: netsi1964


Replies

 
Mikkel Ricky
Reply
This post has been marked as an answer

How did you not succeed with data integration? It should work. Can you share your xml file (or some of it)?

If you create pages and items directly in the database you have to flush the page cache after creating new database rows. This can be done by editing another page (just change something, save, change it back, save). You have to be careful with this approach as you will have to set a lot af values to make sure that the page is in a valid state, and you have to manually handle language versions of the page and item if you use that.

The best way to create items is by using the API. An example for creating an item of type "Page" under the parent page with id 6156:

var item = new Dynamicweb.Content.Items.Item("Page");
item["Name"] = string.Format("Page {0}", DateTime.Now);
item.Save();

var parentPage = Dynamicweb.Content.Page.GetPageById(6156);
var page = new Dynamicweb.Content.Page(parentPage.AreaID, parentPage.ID);
page.ItemId = item.Id;
page.ItemType = item.SystemName;

page.Save();

Best regards,
Mikkel

Votes for this answer: 2
 
Sten Hougaard
Reply

Hi Mikkel,

That looks great! I tried but I think that I need some more help.
Look at the image here:

 

Do I need some casting? Also the item have no name in the content tree.


Med venlig hilsen/Best regards,

Sten Hougaard
Webudvikler

E: sho@1stweb.dk
M: 29850818
A: København/Aarhus . W: www.1stweb.dk
@: netsi1964

 
Mikkel Ricky
Reply

The item field names ("name", "placering", …) must be item field system names with proper casing, i.e. if the field system name is "Name" (as in my example) the code must read

newItem["Name"] = "…";

not

newItem["name"] = "…";
newItem["nAME"] = "…";

or something even more exotic. Maybe that's why it doesn't work as expected.

To get a proper page name in the content tree, you should use the Use field for title setting on your item type. Alternatively, you can set page.MenuText before saving the page, but using an item field for the title is the recommended way to do it.

Best regards,
Mikkel

 
Sten Hougaard
Reply

Hi Mikkel,

Ofcause - field names are case sensetive. Fixed and it works.


Med venlig hilsen/Best regards,

Sten Hougaard
Webudvikler

E: sho@1stweb.dk
M: 29850818
A: København/Aarhus . W: www.1stweb.dk
@: netsi1964

 

You must be logged in to post in the forum