Hi guys,
I have a project where I am using an ItemPublisher to list Articles.
To take advantage of the focal points in the image, I need to transform the Item into an ItemViewModel.
What is the most efficient way of doing it?
Thank you,
Adrian
                                            
                                        
                                                    
                            
                                                    
                        							
                                                    
                                                    
                            
                    Hi guys,
I have a project where I am using an ItemPublisher to list Articles.
To take advantage of the focal points in the image, I need to transform the Item into an ItemViewModel.
What is the most efficient way of doing it?
Thank you,
Adrian
                                                            
                            
                        
															
								
								
															
															
															
								
								
								
						Hi Adrian
An item in the item publisher is either a page or a paragraph.
In the template, figure out if the current item you are rendering is a page or paragraph, then load the instance of that object - (Dynamicweb.Content.Services.Pages/Paragraphs) - and then call  Dynamicweb.Frontend.ContentViewModelFactory.CreatePageInfoViewModel/CreateParagraphViewModel respectively to get a viewmodel of the page or paragraph - both of them having an Item property contining the image property of the right viewmodel.
Dummy code:
@{
    // Get Page and Paragraph IDs from item fields
    var pageId = GetInteger("ItemPublisher:Item.Field.PageId");
    var paragraphId = GetInteger("ItemPublisher:Item.Field.ParagraphId");
    PageInfoViewModel pageModel = null;
    ParagraphViewModel paragraphModel = null;
    if (paragraphId > 0)
    {
        // It's a paragraph
        var paragraph = ParagraphService.Instance.GetParagraph(paragraphId);
        paragraphModel = ContentViewModelFactory.CreateParagraphViewModel(paragraph);
    }
    else if (pageId > 0)
    {
        // It's a page
        var page = PageService.Instance.GetPage(pageId);
        pageModel = ContentViewModelFactory.CreatePageInfoViewModel(page);
    }
    // Access image from the Item fields in the ViewModel
    var imagePath = paragraphModel?.Item?.GetFile("Image")?.Path 
                    ?? pageModel?.Item?.GetFile("Image")?.Path;
}
BR Niolai
                                                            
                            
                        
															
								
								
															
															
															
								
						Hi Nicolai,
Thank you, that is very useful.
I have tried first identifying the Item: GetItemById(string ItemType, string ItemId)
In this case, the ContentViewModelFactory.CreateItemViewModel is internal and I could not use it.
Going through the PageViewModel approach may work easier.
Thank you,
Adrian
You must be logged in to post in the forum