How to get field names

Posted by Community Admin on 04-Aug-2018 16:43

How to get field names

All Replies

Posted by Community Admin on 27-Oct-2015 00:00

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.

Posted by Community Admin on 30-Oct-2015 00:00

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
 

Posted by Community Admin on 30-Oct-2015 00:00

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
 

Posted by Community Admin on 30-Oct-2015 00:00

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.

Posted by Community Admin on 02-Nov-2015 00:00

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
 

Posted by Community Admin on 03-Nov-2015 00:00

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.

This thread is closed