Developer forum

Forum » Development » Creating new Item in a item list

Creating new Item in a item list

Henning Jensen
Reply

Hi.

Im trying to insert a new item into an item list.

One way of doing this would be to insert it using SQL and updating the root item with one more id in the string list but is it possible to do with the api?

Note that its an item on a user and not a page item

Also; is there a way of getting the User itemtype ID for the a user, i cant find it on the user object

 

 


Replies

 
Nicolai Høeg Pedersen
Reply

Hi Henning

A Modules.UserManagement.User has an ItemType, ItemId and Item property with the information you need.

Br Nicolai

 
Henning Jensen
Reply

Hi Nicolai.

Ok thanks i found them now, its just not in the documents http://developer.dynamicweb-cms.com/api8/#Dynamicweb~Dynamicweb.Modules.UserManagement.User_properties.html

 

But i cant see the solution to the problem though. If i have an item with a list of items called Projects and call item["Projects"] i get a string, not a list of items.

My goal would be something like item["Projects"].Add(new Item()); item.save(); 

I just want to know the best way of doing this.

 

 
Nicolai Høeg Pedersen
Reply

Hi Henning

I've asked a developer to dig up some code for you. Will get back by tomorrow.

BR Nicolai

 
Vladimir
Reply

Hi Henning,

As I understand you have field of type: "Item relation list"  (Item list type is old type - considered as deprecated and not recommended to use)

Then you have the id of an item list in that field and you could manage its relation by functions:

     var myListId = (int)item["Projects"];
    
//ItemList.DeleteRelation(myListId, relationItemId);
     //ItemList.AddRelation(myListId, relationItemId, sortIndex);
     item.save();

Best regards,

Vladimir

 
Aki Ruuskanen
Aki Ruuskanen
Reply

Hi,

I'm strugguling a bit with this.

I have a codefirst item called "Return" with a field of type "Item Relation List"

If I create an entry in the backend I get the "Id" for the list.

But when I create a new entry with code I get "0" as the id.

 Here is a sample of how I create a new entry for the Item:

    var r = (Return)ItemEntry.Create("Return");
    r.street = Base.Request("address");
    r.zip = Base.Request("zip");
    r.city = Base.Request("city");
    r.Save();

 

Any ideas why?

 

Regards /Aki

 

 
Vladimir
Reply

Hi Aki,

In that case you should also create a "Item Relation List" for field:
    var r = (Return)ItemEntry.Create("Return");
    ...
    var  
itemList New ItemList("ItemType")
    itemList.Save();

    r.itemListField = itemList.ID;

    r.Save();

Kind regards,

Vladimir

 
Aki Ruuskanen
Aki Ruuskanen
Reply

Thanks Vladimir.

I managed to make it work but was not sure it was the right way. Now I am. :)

Regards / Aki

 

You must be logged in to post in the forum