0

I want to check requested item id is a page item id or datasource item id through code. For more clarity, I have a scenario in SXA where we need to check requested id is page id or datasource id. If it is page item id then we will perform some activity or if it is datasource item id then we will perform some other activity.

1

You can use Sitecore.Context.Item.ID. This will return current page item ID. Use RenderingContext.Current.Rendering.Item.ID will return you the data source item ID. You can compare the ID with one of this and execute your code accordingly.

Regards, Girish

| improve this answer | |
  • I am getting item id but i am not sure how to identify this is page item id or datasource item id. I am using above mentioned code but i am getting error. – Sonu Jul 1 at 17:17
0

I guess you are trying to identify if the datasource item is a page or just a content item. Ideally in Sitecore an item can be called a page if there is any presentation details associated with that item and this can be checked by using below code.

item.Visualization.GetLayout(Sitecore.Context.Device);

Above line of code will tell you if any presentation detail is associated with the item or not. If not then its a simple datasource item otherwise its a page.

Hope this helps

| improve this answer | |
  • It is giving result null for the last datasource item only. However, if we have structure -: Page> Data> Test> Accordion Item> Data> Text 1 Then it is not giving the exact result. For this example the last "Text 1" showing null result. Here "Page" is a page item and other are datasource items – Sonu Jul 2 at 10:31
  • I suppose the presentation detail is only present on page item and for that you should get the layout item. But for other items like Data, Accordion Item and Text1 should give you null until you have assigned presentation detail to those items. – Atul Kumar Jul 2 at 16:51
  • i have resolved this issue by checking page template id. Thanks – Sonu Jul 3 at 10:58
0

Atul Kumar's answer should work for you. An alternate way is to get item by ID

Item item = Sitecore.Context.Database.GetItem(Sitecore.Data.ID.Parse(itemId));

and then pass the item to below func -

private bool HasPresentationDetails(Item item)
{
 return item.Fields[Sitecore.FieldIDs.LayoutField] != null
      && !String.IsNullOrEmpty(item.Fields[Sitecore.FieldIDs.LayoutField].Value);
}

If it returns true, the item is a page item. Else it is a datasource item.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.