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.

 

You must be logged in to post in the forum