More in this section

Forums / Developing with Sitefinity / Retrieve single DynamicContent item

Retrieve single DynamicContent item

3 posts, 0 answered
  1. BoaMike
    BoaMike avatar
    51 posts
    25 Sep 2009
    13 Oct 2013
    Link to this post
    I'm relatively new to working with DynamicContent and I'm wondering how to retrieve a single DynamicContent item when you don't know it's ID, only a FieldValue, Title, for example?  The GetDataItem method expects and ID, so I was trying to use GetDataItems like this:
    Type officeLocationType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.OfficeLocations.OfficeLocation");
    DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager();
    var loc = dynamicModuleManager.GetDataItems(officeLocationType).Where(i => i.GetValue<IList>("Title").Equals("Philadelphia")).FirstOrDefault();

    But I am getting this error:
    Database mapped field uses different type 'System.String'.
    Parameter name: methodCallExpression
    Actual value was re-i.FieldValue("Title").

    How does one get a single DynamicContent item when you don't know the ID?
  2. Jonathan
    Jonathan avatar
    119 posts
    19 Dec 2012
    14 Oct 2013 in reply to BoaMike
    Link to this post
    Under Administration >> Module Builder 

    Select your module.  Then on the right there is a Code Reference link which will get you exactly what you need. Should be something similar to this.

    var myFilteredCollection = dynamicModuleManager.GetDataItems(officeLocationType).Where(i => i.GetValue<string>("Title") == "Philadelphia").FirstOrDefault();

  3. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    18 Sep 2017
    16 Oct 2013
    Link to this post

    The sample query from Jonathan is the solution to the case the need to call GetValue<T>("FieldName") is due to the fact that in module builder there is no predefined fields available for each item one module can have Title field, but other module might have "SportID" field instead so its up to the user to enter the names for fields chosen in the module.

    Stanislav Velikov
    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 Public Issue Tracking system and vote to affect the priority of the items
3 posts, 0 answered