Search code examples
tridiontridion-2011

Get type of metadata fields of a Metadata schema


I want to get the all fields along with type/datatype of the metadata fields of a Metadata schema.

I have written below sample code to achieve the functionality and I am able to get Name, Description etc but could not find any property with type/dataType. If anyone of you have any idea, please suggest...

var client = new SessionAwareCoreService2010Client();

client.ClientCredentials.Windows.ClientCredential.UserName = "myUserName";
client.ClientCredentials.Windows.ClientCredential.Password = "myPassword";
client.Open();

if (client.State == System.ServiceModel.CommunicationState.Opened)
{
    var schemaUri = "tcm:1-47-8";
    var fields= client.ReadSchemaFields(schemaUri, true, new ReadOptions());
    var fieldName =   fields.MetadataFields[0].Name;
}

Solution

  • To know the type of a field, you only need to examine the .NET type of the field. I typically use an "is" check, but you can also call GetType if you want.

    For example:

    var client = new SessionAwareCoreService2010Client();
    
    client.ClientCredentials.Windows.ClientCredential.UserName = "myUserName";
    client.ClientCredentials.Windows.ClientCredential.Password = "myPassword";
    client.Open();
    
    if (client.State == System.ServiceModel.CommunicationState.Opened)
    {
        var schemaUri = "tcm:1-47-8";
        var fields= client.ReadSchemaFields(schemaUri, true, new ReadOptions());
        foreach (var field in fields.MetadataFields)
        {
            if (field is SingleLineTextFieldDefinitionData)
            {
                // Do something specifically for single-line text fields
            }
        }
    }