Metadata Examples

Shortcuts:
Clarizen .Net wrapper: http://clarizen.caglar.com
Sample .Net project: Download samples.zip or browse/fork on GitHub

DescribeMetadata

Here is how to get the fields (metadata) of an entity:

using System;
using System.Configuration;
using Clarizen.API.V2_0;
using Clarizen.API.V2_0.Metadata;

namespace ClarizenSamples.Metadata
{
    class DescribeMetadata_Entity
    {
        static DescribeMetadata_Entity()
        {
            API ClarizenAPI = new API();
            if (!ClarizenAPI.Login("username", "password"))
                return;

            string entityName = "User";  // Could be Customer, Project, User, UserGroup, Task, DiscussionPost, etc.
            describeMetadata metadata = ClarizenAPI.DescribeMetadata(new string[] { entityName }, new string[] { "relations", "fields" });
            if (metadata.IsCalledSuccessfully)
            {
                Console.WriteLine("Fields for the {0} object:", entityName);
                metadata.Data.entityDescriptions[0].SortFields();
                foreach (fieldDescription field in metadata.Data.entityDescriptions[0].fields)
                {
                    Console.WriteLine("\t{0} ({1}) {2}", field.name, field.label, field._type);
                }
            }
            else
                Console.WriteLine(metadata.Error);

            if (ClarizenAPI.Logout())
                Console.WriteLine("{0} API calls made in this session", ClarizenAPI.TotalAPICallsMadeInCurrentSession);
        }

    }
}

GetAllEntities

The following sample uses the ListEntities method to get a list (metadata) of all the entities available to the user:

using System;
using Clarizen.API.V2_0;
using Clarizen.API.V2_0.Metadata;

namespace ClarizenSamples.Metadata
{
    class GetAllEntities
    {
        static GetAllEntities()
        {
            API ClarizenAPI = new API();
            if (!ClarizenAPI.Login("username", "password"))
                return;

            listEntities metadata = ClarizenAPI.ListEntities();
            if (metadata.IsCalledSuccessfully)
            {
                metadata.Data.SortTypeNames();
                Console.WriteLine("{0} entities found", metadata.Data.typeNames.Length);
                // Note that the following query will be truncated at 2000 characters (RestClient.GET limitation)
                describeEntities entities = ClarizenAPI.DescribeEntities(metadata.Data.typeNames);
                if (entities.IsCalledSuccessfully)
                {
                    foreach (entityDescription description in entities.Data.entityDescriptions)
                    {
                        description.SortFields();
                        Console.WriteLine("Fields for the {0} object:", description.typeName);
                        foreach (fieldDescription field in description.fields)
                        {
                            Console.WriteLine("\t{0} ({1}) {2}", field.name, field.label, field._type);
                        }
                        Console.WriteLine("");
                    }
                }
                else
                    Console.WriteLine(entities.Error);
            }
            else
                Console.WriteLine(metadata.Error);

            if (ClarizenAPI.Logout())
                Console.WriteLine("{0} API calls made in this session", ClarizenAPI.TotalAPICallsMadeInCurrentSession);
        }
    }
}

GetAllEntityRelations

The following sample uses the DescribeEntityRelations method to get a list (metadata) of all the relationships for all the entities available to the user:

using System;
using Clarizen.API.V2_0;
using Clarizen.API.V2_0.Metadata;

namespace ClarizenSamples.Metadata
{
    class GetAllEntityRelations
    {
        static GetAllEntityRelations()
        {
            API ClarizenAPI = new API();
            if (!ClarizenAPI.Login("username", "password"))
                return;

            listEntities metadata = ClarizenAPI.ListEntities();
            if (metadata.IsCalledSuccessfully)
            {
                metadata.Data.SortTypeNames();
                Console.WriteLine("{0} entities found", metadata.Data.typeNames.Length);
                // Note that the following query will be truncated at 2000 characters (RestClient.GET limitation)
                describeEntityRelations entities = ClarizenAPI.DescribeEntityRelations(metadata.Data.typeNames);
                if (entities.IsCalledSuccessfully)
                    WriteEntityRelationsDescription(entities.Data.entityRelations);
                else
                    Console.WriteLine(entities.Error);
            }
            else
                Console.WriteLine(metadata.Error);

            if (ClarizenAPI.Logout())
                Console.WriteLine("{0} API calls made in this session", ClarizenAPI.TotalAPICallsMadeInCurrentSession);
        }

        static void WriteEntityRelationsDescription(entityRelationsDescription[] entityRelations)
        {
            foreach (entityRelationsDescription description in entityRelations)
            {
                description.SortRelations();
                Console.WriteLine("Relationships for the {0} object:", description.typeName);
                foreach (relationDescription relation in description.relations)
                {
                    Console.WriteLine("\t{0} ({1}) {2}-{3} {4}", relation.name, relation.label, relation.sourceFieldName, relation.relatedTypeName, relation.linkTypeName);
                }
                Console.WriteLine("");
            }
        }
    }
}

Comments