Developer forum

Forum » Dynamicweb 10 » DWAPI - sort order of paragraphs

DWAPI - sort order of paragraphs

Rene Poulsen
Rene Poulsen
Reply

Hi,

I'm working on a solution where we in several paragraphs use the DWAPI to get paragraph content (e.g.: /dwapi/content/paragraphs?AreaId=1&ItemType=Employee) (an we're moving more an more towards a headless setup). In some scenarios we need to be able to sort the data in the exact same way as they are sorted on the page in DW. I thought that it would be the default sorting, but that's not the case. Then, maybe naive, I thought I would be able to do the sorting myself, as the API offcourse would return a "sort" / "sortorder" / "order" field in the data returned. That's not the case either. The solution is running on a 10.9.0 and the content I'm trying to get - and sort - is paragraphs of a certain item type. The fields available on the paragraph is:

  • id
  • name
  • createdDate
  • updatedDate
  • pagId
  • text
  • image
  • imageFocalX
  • imageFocalY
  • imageLink
  • imageAlt
  • imageLinkTarget
  • imageCaption
  • colorScheme

I also have a lot of fields on the item - but no sort field. Is this a bug in 9.10? Or has it always been that way?


Replies

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Hi Rene

You are using the an endpoint with an areaid, and in that context the paragraphs have no sort order. Say each paragraph sits on each own page, they would all have the same sort (sort=1) and sorting would make no sense. If we added page sort to the mix, it would still not make much sense as pages have sort values relative to their parent causing many of them to have the same sort value. E.g. this would be the sorting values:

  • Page 1
    • Subpage 1
      • Sub-subpage 1
        • Paragraph 1
        • Paragraph 2
    • Subpage 2
      • Sub-subpage 1
        • Paragraph 1
        • Paragraph 2
    • etc....

So if they were just sorted by their sort values, that would still mess them around.

So what would you expect when they come out of this endpoint? Do you want them in 'tree order'?

If you call /dwapi/content/paragraphs with pageid instead of areaid, you will get them in the sort order of that page.

I have added sort to paragraph and page info viewmodels so they at least will be returned. But that will probably not help you a lot in this case.

 
Rene Poulsen
Rene Poulsen
Reply

Hi Nicolai

Of course. It makes sense. I'll have a look and see if we can fix it in some way.

The issue is, that we have departments (item based paragraphs) which we can choose on the employees (also paragraphs). To show the departments and employees we have a pretty generic module, where we can choose to just show all - or show specfic departments and / or employees. We have a few employees that can be under several departments, so we can't make departments as pages, with the employees as children to them (unless we tell the customer to create these employees multiple times - one under each department).

EDIT: So I guess, what I'm trying to say is that the sortorder COULD be usefull in the API ;-) Even when getting paragraphs by an areaID. But yeah - it would be unusefull, if the content is spread out on several pages. But as developers, we should be able to distinguish when it would make sense to use the sortorder field or not :-)

 
Rene Poulsen
Rene Poulsen
Reply

@Nicolai is it possible to extend the API's and supply additional data? Or will we need to create our own endpoint?

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

It is possible to extend - if it makes sense in the broader perspective.

I added Sort property to paragraph and page as they are not part of the viewmodels and that makes sense to be available.

Do you have other props you need?

 
Rene Poulsen
Rene Poulsen
Reply

Hi Nicolai,

How do you do that? I tried by creating a custom paragraph view model to see if added properties from that got added. It does, when using a template that inherits from the model, but they are not present in the API.I tried with these two:

public class CustomParagraphViewModel : ParagraphViewModel
{
    private object _instance;

    public CustomParagraphViewModel()
    {
        _instance = new object();
    }

    public int CustomSortOrder
    {
        get
        {
            ParagraphService paragraphService = new ParagraphService();
            Paragraph paragraph = paragraphService.GetParagraph(Item.ParagraphID);

            if (paragraph == null)
            {
                return 999;
            }

            return paragraph.Sort;
        }
    }
}

[AddInName("Employee")]
public class EmployeeViewModel : ParagraphViewModel
{
    private object _instance;

    public EmployeeViewModel()
    {
        _instance = new object();
    }

    public int CustomEmployeeSortOrder
    {
        get
        {
            ParagraphService paragraphService = new ParagraphService();
            Paragraph paragraph = paragraphService.GetParagraph(Item.ParagraphID);

            if (paragraph == null)
            {
                return 999;
            }

            return paragraph.Sort;
        }
    }
}
 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply

Where did you add the customization? To bin or addins in assets? And you might need to restart the site when you add your custom viewmodel as that type can be cached as not being extended after the application loads.

 
Rene Poulsen
Rene Poulsen
Reply

 @Nicolai it's put directly in an "Extending" folder in our website. I've attached a screenshot where you can see it.

I'm just running it locally in IisExpress through VS, so there's no application pool to recycle or website to restart.

As mentioned earlier, it works in a paragraph template, when inheriting from the CustomParagraphViewModel - and the EmployeeViewModel, when having a paragraph of itemtype Employee.

 

 

 
Rene Poulsen
Rene Poulsen
Reply

@Nicolai anything new on this? As mentioned, the CustomViewModel I've made (see above) works like a charm when in a regular razor template extending the viewmodel. It doesn't work in the API, where I would like it to work as well.

 
Rene Poulsen
Rene Poulsen
Reply

@Nicolai got an update on this?

 
Rene Poulsen
Rene Poulsen
Reply

Any news on this? :-)

 
Rene Poulsen
Rene Poulsen
Reply

@Nicolai - can I get an update in this? It sounds like it shoudl be possible and I've supplied you with our code. Is it - or will it be possible? I need a fix for our customer and would like to extend the viewmodel if possible. If not, I need to fix the issue in another way.

 
Nicolai Pedersen Dynamicweb Employee
Nicolai Pedersen
Reply
This post has been marked as an answer

We will add the sort properties as standard in a coming release.

About the customization:

When you extend the viewmodels the custom properties are currently not seralized out in Dynamicweb 10 - this is because in DW9 we are using newtonsoft and on DW10 we are using System.Text.Json which will not seralize the actual instance of an object (and its properties) but only the properties of the base type specifed on the property.

Consider this pseudo code:

ProductViewmodel {
    PriceViewmodel: Price{get;set;}
}

CustomPriceViewmodel: PriceViewmodel {
    string MyOwnProperty {get;set;}
}

When serializing objects in C# using System.Text.Json, if you have a base class and derived classes, the serializer may not include properties from the derived classes when the object is referenced by its base class type. This behavior occurs because the serializer uses the compile-time type to determine which properties to serialize.

Currently when we serialize a ProductViewmodel instance using System.Text.Json where the ProductViewmodel instance have an instance of CustomPriceViewmodel assigned to its Price property (which is perfectly fine), only the properties of PriceViewmodel gets serialized and not MyOwnProperty of the custom model eventhough it exists. 

We have it in our books to see if we can get this to work without breaking stuff - specifically looking at JsonUnknownDerivedTypeHandling.

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/polymorphism#handle-unknown-derived-types

Votes for this answer: 1
 
Rene Poulsen
Rene Poulsen
Reply

Sounds great that the sortorder will be part of the properties. Do you have a number as a reference so I can follow up on it in the release notes? :-)

Sound even better if the custom properties will be part of the data from the API as well. We are working more and more with custom viewmodels and if we're going to move to 100% headless, this would be a requirement for us.

 

You must be logged in to post in the forum