Developer forum

Forum » Development » Querying Items.

Querying Items.

Yasharth Tahiliani
Reply

Hi, 

I am attempting the query items using the instructions given on the following page:

http://developer.dynamicweb.com/documentation/for-developers/item-based-structure/querying-items.aspx

The commands that I am using are:

    Dynamicweb.Content.Items.Item i = Dynamicweb.Content.Items.ItemManager.Storage.GetById("Trip", id);

    string n = i["Trip_Short_Summary"].ToString();

The item type I am trying to access is called Trip. 

It is breaking at the first line (Saying that it is a NullReferenceException).

Can somone please advise what I may be doing wrong here?

Thanks & Regards,

Yash.


Replies

 
Morten Bengtson
Reply

In what context are you trying to do this? Is it in a razor template?

 
Yasharth Tahiliani
Reply

Both, in a Razor Template, and in ASP.NET

It is returning the same error in both places.

 
Morten Bengtson
Reply

It's hard to tell anythnig from a few lines of code. Your second line of code looks more problematic to me, but you are certain that the error is caused on the first line?

You need to make sure that the itemtype and itemid is correct - can you find a record in the database table "ItemEntry_Trip" with the id you pass in?

What do you mean by "in ASP.NET"? If this is a handler (.ashx) or anything like that, then you might need to enable session state by adding the interface IRequiresSessionState and call ItemManager.Initialize() before your query.

 
Nicolai Høeg Pedersen
Reply

And we need the entire stack trace to help.

 
Yasharth Tahiliani
Reply

Hi Morten and Nicolai,

I am trying to achieve this in the template of the weighted search of the site that I am building. 

TemplateTags() in the search shows me many things returned, and one of them is the Id of the page item. I just want to get some fields from the item to display in the search result, and also ommit some searches.

The template looks like this:

 

@{
    string id = (string) @GetValue("DwSearchResultID");
//Dynamicweb.Content.Items.Item i = Dynamicweb.Content.Items.ItemManager.Storage.GetById("Trip", id);

//string n = i["Trip_Short_Summary"].ToString();

}

<div class="result">
  <div class="result-heading">
    <h4><a href="@GetValue("DwSearchResultHref")">@GetValue("DwSearchResultTitle")</a></h4>
      <span class="url"></span>
  </div>


  <p class="excerpt">
    @GetValue("DwSearchResultSummary")
  </p>
  <div class="button left primary-blue-background"><a href="@GetValue("DwSearchResultHref")">Read more</a></div>
</div>

Looking forward to hearing from you :) 

Thanks and Best Regards,

Yash.

 
Yasharth Tahiliani
Reply

Hi guys,

Did you get a chance to look at this?

Regards,

Yash.

 
Morten Bengtson
Reply

I think the problem is that you are taking the page id (DwSearchResultID) and using it as the item id. You can get the item values in several ways. Here are two approaches...

1) Raw values

@{ 
    var pageId = GetInteger("DwSearchResultID");
    var page = Dynamicweb.Content.Page.GetPageById(pageId);
}

@if(!string.IsNullOrEmpty(page.ItemType) && !string.IsNullOrEmpty(page.ItemId))
{
    var item = Dynamicweb.Content.Items.Item.GetItemById(page.ItemType, page.ItemId);
    <div>
    @item["Trip_Short_Summary"]
    </div>
}
else
{
    <div>Regular page</div>
}

2) Render item (you probably want this. It's easier to work with) - See template documentation (You'll need to implement an extra template and use it as DetailsTemplate in RenderItem)

@{ 
    var pageId = GetInteger("DwSearchResultID");
    var page = Dynamicweb.Content.Page.GetPageById(pageId);
}

@if(!string.IsNullOrEmpty(page.ItemType) && !string.IsNullOrEmpty(page.ItemId))
{
  <div>
    @RenderItem(new{ ItemType = page.ItemType, SourceItemEntry = page.ItemId, ItemFields = "*", DetailsTemplate = "ItemPublisher/Details/Details.html"})
  </div>
}
else
{
  <div>Regular page</div>
}

There is also some new index and search functionality in DW, which might be even better. I have very little experience with that though. See webinars here: http://engage.dynamicweb.com/training-certification/webinars-1

 

 
Yasharth Tahiliani
Reply

Thank you so much guys.

I am facing a problem when using the same within a user control (.ascx file). 

Attached is a screen grab and I am also copying the code that I am using. It worked perfectly in the Razor Template however.May be I am missing something that is always there in the razor template.

                int tripId = Convert.ToInt32(row["Id"].ToString());

                ItemManager.Initialize();

                Item i = ItemManager.Storage.GetById("Item", tripId.ToString());

                string id = i.Page.ID.ToString();

                string url = Dynamicweb.Frontend.SearchEngineFriendlyURLs.GetFriendlyUrl(id);

                result.url = url;

 

ItemManager.jpg
 
Morten Bengtson
Reply

Is your item type named "Item"?

 
Yasharth Tahiliani
Reply

Haha no, it is of type Trip. I feel really stupid now.

Apologies, and thank you so much for suggesting this way, it is really nice!

Yash.

 

You must be logged in to post in the forum