Developer forum

Forum » CMS - Standard features » Get items by paragraph

Get items by paragraph

Umar Farooq
Reply

Hi Everyone, I am Trying to Get a specific item by Paragraph id  and my code looks like this  

 

@{
    var id = GetGlobalValue("Global:Page.ID");
    var pageid = int.Parse(id);

    foreach (Dynamicweb.Content.Paragraph i in Dynamicweb.Content.Paragraph.GetParagraphsByPageID(pageid)) {
        var itemtype = i.ItemType;
        var paragraph = i.ID.ToString();
        var paragraphid = int.Parse(paragraph);


      Collection<Item> items = ItemManager.Storage.GetByParagraphId(itemtype, paragraphid);

      foreach (Item n in items) {
           @n.Names
      }

}

}

 

But when i render this I get the following error 

 

Exception in template (Designs\Paragraph\ItemNav.cshtml): System.ArgumentException: System name of the target item cannot be an empty string.
Parameter name: metadata
   at Dynamicweb.Content.Items.Queries.Repository..ctor(ItemType metadata)
   at Dynamicweb.Content.Items.Queries.StorageFactory.CreateRepository(ItemType metadata)
   at Dynamicweb.Content.Items.Queries.StorageManager.GetByParagraphId(String systemName, Int32 paragraphId)
   at CompiledRazorTemplates.Dynamic.edefddfdfffdaaaaca.Execute()
   at RazorEngine.Templating.TemplateBase.RazorEngine.Templating.ITemplate.Run(ExecuteContext context)
   at RazorEngine.Razor.Parse[T](String razorTemplate, T model, String cacheName)
   at Dynamicweb.Rendering.Template.Output()

What can i do in order to fix this error? or if there is any better way of getting the items from paragraph it will be great.

Please help and many thanks in advance.

 

Best Regards

 

  


Replies

 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer

Hi Umar

You are looping all paragraphs on that page. some of those paragraphs does not have an item, hence no system name. So you have to add a conditional that checks if your variable "itemtype" is not empty.

Be carefull with this code. It seems like something that could perform badly.

BR Nicolai

Votes for this answer: 1
 
Umar Farooq
Reply

Hi Nicolai,

Thanks for your quick responce it solved my problem And I have already considered changing the code.

Best Regatrds,

Umar 

 

You must be logged in to post in the forum