+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / How to get field names

How to get field names

6 posts, 2 answered
  1. Javier
    Javier avatar
    3 posts
    Registered:
    20 Oct 2015
    27 Oct 2015
    Link to this post
    Hello,

    I have a question about how to get field names. What I need is to get a list of fileds for a DynamicContent given. When I call DynamicContent. GetType().GetProperties() I get a list of all properties names, but I need to get only those created by user. Could you please tell me how to do this?

    Example:

    Users:
    Name: John
    Surname: Smith
    Birth date: 01/01/1975
    Address: 12, Commercial Road
    Postal Code: 1111
    City: London
    Country: England

    So, my question would be… How can I get a list of: “Name, Surname, Birth date, address, Postal Code, City, Country”.

    Thanks in advance.
  2. Bilyana Ivanova
    Bilyana Ivanova avatar
    34 posts
    Registered:
    21 Nov 2016
    30 Oct 2015
    Link to this post
    Hello Javier,

    Can you try this sample code?

    DynamicModuleType dmType = null;
    IQueryable<DynamicModuleField> dmFields = null;
    var providers = ModuleBuilderManager.ProvidersCollection;
    if (providers == null) providers = ModuleBuilderManager.GetManager().Providers;
     
    foreach (var provider in providers)
    {
    dmType = provider.GetDynamicModuleTypes().Where(t => t.TypeName.Equals(sheet.Name)).First();
    dmFields = provider.GetDynamicModuleFields().Where(f => f.FieldNamespace.Equals(dmType.GetFullTypeName()));
    }

    If you use a single provider, you can directly work with that one. You can then check the dmFields collection and use the Name property.

    Let me know is there are issues with the code.

    Regards,
    Bilyana Ivanova
    Telerik
     
    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
     
    Answered
  3. Bilyana Ivanova
    Bilyana Ivanova avatar
    34 posts
    Registered:
    21 Nov 2016
    30 Oct 2015
    Link to this post
    Hello Javier,

    Just to clarify, any structure defined with the module builder is 100% defined by the user, so there are no default fields.

    Regards,
    Bilyana Ivanova
    Telerik
     
    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
     
  4. Javier
    Javier avatar
    3 posts
    Registered:
    20 Oct 2015
    30 Oct 2015
    Link to this post

    Sorry, but this is not what I need to get. What I need is, given a Type, for example, "Telerik.Sitefinity.DynamicTypes.Model.Users", get its field names. In the example given in the first post, the result should be: "Name, Surname, Birth date, address, Postal Code, City, Country".

     

    The reason why I am trying to do this, is that I need to get dynamically fields and values of a given type in order to get data, for example, of users, providers, clients... From my database.

  5. Bilyana Ivanova
    Bilyana Ivanova avatar
    34 posts
    Registered:
    21 Nov 2016
    02 Nov 2015
    Link to this post
    Hello Javier,

    With the sample code that I provided if you have a dynamic content type called Store with these fields: Title, Phone, UserID, StoreImage, Address (see content_type.png), then executing this code

    DynamicModuleType dmType = null;
               IQueryable<DynamicModuleField> dmFields = null;
               var providers = ModuleBuilderManager.ProvidersCollection;
               if (providers == null) providers = ModuleBuilderManager.GetManager().Providers;
     
               foreach (var provider in providers)
               {
               dmType = provider.GetDynamicModuleTypes().Where(t => t.TypeName.Equals("Store")).First();
               dmFields = provider.GetDynamicModuleFields().Where(f => f.FieldNamespace.Equals(dmType.GetFullTypeName()));
               }

    will return Title, Phone, UserID, StoreImage, Address (see dmFields.png. Isn't this what you wanted to achieve?

    Regards,
    Bilyana Ivanova
    Telerik
     
    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
     
    Answered
  6. Javier
    Javier avatar
    3 posts
    Registered:
    20 Oct 2015
    03 Nov 2015 in reply to Bilyana Ivanova
    Link to this post

    Yes!! You are right, sorry. It works!!!

    I also got it with this code:

    MetadataManager metadataManager = MetadataManager.GetManager();
    MetaType contentMetaType = metadataManager.GetMetaType(type);
    return contentMetaType.Fields;

    Thank you very much for your help.

6 posts, 2 answered