Developer forum

Forum » Development » Filter Items list in 8.9

Filter Items list in 8.9

Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi guys,

I need to use the API to get a list of items with a specific SystemName from the current Area.

I am not sure if using 

 Dynamicweb.Content.Items.ItemManager.Metadata.GetItemType("News")

will return only the Pages from the current Area.
Can anybody clarify it? Or suggest a way to get only the ItemTypes for the current Area?

Thank you

Adrian


Replies

 
Morten Bengtson Dynamicweb Employee
Morten Bengtson
Reply

Hi Adrian,

If you need to render the items on a page I would suggest using the ItemPublisher app or the RenderItem / RenderItemList template methods.

If you are not in the context of a page view then you can use something like this...

var itemType = Dynamicweb.Content.Items.ItemManager.Metadata.GetItemType("News");
 
if (itemType != null)
{
    var areaId = 1;
    var query = new Dynamicweb.Content.Items.Queries.Query() { Amount = 100 };
    var includeInheritedItems = false;
    var includeParagraphItems = false;
    var checkPermissions = true;
 
    IEnumerable<Dynamicweb.Content.Items.Item> items = null;
 
    using (var repository = Dynamicweb.Content.Items.ItemManager.Storage.Open(itemType))
    {
        items = repository.SelectByAreaId(areaId, query, includeParagraphItems, checkPermissions, includeInheritedItems);
    }
 
    if (items != null)
    {
        foreach (var item in items)
        {
            // do something
        }
    }
}

The code is for DW9, but it should be similar for DW8.

Best regards,
Morten

 
Adrian Ursu Dynamicweb Employee
Adrian Ursu
Reply

Hi Morten,

In my situation, the GetItemType is just for rendering all values available in a field on the item with the purpose of using them as filters.

I am basically using:

Dynamicweb.Content.Items.ItemManager.Metadata.GetItemType("News").Fields

I cannot use the QueryPublisher functionality, therefore I have to rely on alternate methods.

The current one is rendering the values but the values are retrived for the whole set of the Items not just the subset for the current area. This means that the filter values will render 0 results. And I wanted to avoid that.

Thank you very much for the clarification. I will give it a try.

Thank you,
Adrian

 

 

You must be logged in to post in the forum