Developer forum

Forum » CMS - Standard features » User ItemType as ItemViewModel

User ItemType as ItemViewModel

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

I am trying to access the data stored in a User ItemType.
I can get the ItemId and the ItemType but with the methods I have found I can only get an Item and not an ItemViewModel.

Although I can access some of the simple properties (Text, lists) I have some issues accessing a Product field. If I use the property in brackets ( e.g. item("Products") ), I only get a list of the strings and not a List<ProductViewModel>. 

I have tried using ContentViewModelFactory.CreateItemViewModel(item) but I get a 'ContentViewModelFactory' does not contain a definition for 'CreateItemViewModel' message

I am using DW  9.15.5.

Thank you,
Adrian


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Is in a template or what?

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

Yes. In a template.

Thank you,

Adrian

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Pls show me your code.

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

Here is an excerpt:

@inherits Dynamicweb.Rendering.ViewModelTemplate<Dynamicweb.Frontend.ParagraphViewModel>
@using Dynamicweb.Content.Items
@using Dynamicweb.Ecommerce.ProductCatalog;
@using Dynamicweb.Frontend
 
@{
    Dynamicweb.Content.Items.Item item = new Dynamicweb.Content.Items.Item();
 
    if(Pageview.User != null)
    {
        string userItemId = Pageview.User.ItemId;
        string userItemType = Pageview.User.ItemType;
        item = userItemId != "" ? Dynamicweb.Services.Items.GetItem(userItemType, userItemId) : new Dynamicweb.Content.Items.Item();
 
        if(item != null)
        {
            var productListId = Dynamicweb.Core.Converter.ToInt32(item["Licenses"]);
            var licenses = Dynamicweb.Content.Items.ItemList.GetItemListById(productListId);
            if(licenses != null)
            {
                foreach(var relation in licenses.Relations)
                {
                   
                    var license = new Dynamicweb.Content.Items.Item(relation);
                    var itemViewModel = ContentViewModelFactory.CreateItemViewModel(license);
                }
            }
        }



 
    }
}

 

"Licenses" is an ItemRelationList and it has a property of type "Product" with the SystemName "Product".

My purpose is to get the product from the "Product" field as a ProductViewModel (ideally).

My code fails now when using the ContentViewModelFactory.

Thank you,
Adrian

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Nicolai,

Can please you help me out with a suggestion here?

Thank you,
Adrian

 

You must be logged in to post in the forum