More in this section

Forums / Developing with Sitefinity / GetDataItem() returns the wrong type

GetDataItem() returns the wrong type

2 posts, 0 answered
  1. Rich
    Rich avatar
    24 posts
    22 Oct 2012
    07 Nov 2014
    Link to this post

    I stumbled across what appears to be a bug in Sitefinity 7.1.5200.  I was making a call to DynamicContent.GetDataItem(Type, Guid), and accidentally passed in an incorrect guid from a different item type.  I expected GetDataItem to return null, since the guid does not exist for the Type that I passed in.  Instead, it found the guid in the system under a different item type, and returned that one.  I drilled down to the code for GetDataItem and found that the Type parameter is not being used.

    public override DynamicContent GetDataItem(Type itemType, Guid id)
      DynamicContent dataItem = Queryable.SingleOrDefault<DynamicContent>(Queryable.Where<DynamicContent>(SitefinityQuery.Get<DynamicContent>((DataProviderBase) this, MethodBase.GetCurrentMethod()), (Expression<Func<DynamicContent, bool>>) (p => p.ApplicationName == this.ApplicationName && p.Id == id)));
      if (dataItem == null)
        throw new ItemNotFoundException(Res.Get<ErrorMessages>().ItemNotFound);
      dataItem.Provider = (object) this;
      this.ApplyViewFieldPermissions((IDataItem) dataItem);
      this.ApplyFilters((IDataItem) dataItem);
      return dataItem;

    Can someone please verify that this is incorrect behavior?

  2. Nikola Zagorchev
    Nikola Zagorchev avatar
    424 posts
    19 Apr 2017
    12 Nov 2014
    Link to this post
    Hi Rich,

    The method will indeed return the item even when a different object type is passed. The items are not filtered by the type parameter, which could lead to unexpected behavior. I have logged a bug regarding the issue, which can be reviewed here. I have also updated your Telerik points. As a workaround you can check  whether the returned item type is of the one you expect, or make the query to get all items of those type and filter by the id and then to get the item in the memory.

    Nikola Zagorchev
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Sitefinity CMS Ideas&Feedback Portal and vote to affect the priority of the items
2 posts, 0 answered