Query Examples

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

CZQL

using System;
using Clarizen.API.V2_0;
using Clarizen.API.V2_0.Data;
using Clarizen.API.V2_0.Interfaces;

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

            IClarizenQuery query = new Query()
                      .Select("Name,Work")
                      .From("Task")
                      .Where("StartDate>2016-12-31 AND StartDate<2017-12-31");
            query CZQuery = ClarizenAPI.ExecuteQuery(query);
            if (CZQuery.IsCalledSuccessfully)
            {
                Console.WriteLine("{0} results found", CZQuery.Data.entities.Length);
                foreach (dynamic entity in CZQuery.Data.entities)
                {
                    Console.WriteLine("\t{0}\t{1}", entity.id, entity.Name);
                }
                if (CZQuery.Data.paging.hasMore)
                    Console.WriteLine("There are more records than the ones shown here");
            }
            else
                Console.WriteLine(CZQuery.Error);

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

Count

using System;
using Clarizen.API.V2_0;
using Clarizen.API.V2_0.Data.Queries;
using Clarizen.API.V2_0.Data.Queries.Conditions;
using Clarizen.API.V2_0.Data.Queries.Expressions;

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

            // We will search this term in the DisplayName field of User entities
            string searchTerm = "ekin";

            // Create the search query
            entityQuery query = new entityQuery("User");
            query.where = new compare(new fieldExpression("DisplayName"), Operator.Like, new constantExpression(String.Format("%{0}%", searchTerm)));
            // Here is how to perform the same search using CZQL instead
            //query.where = new cZQLCondition(@"DisplayName LIKE ""%ekin%""");

            // Run the Count method with the query created above
            Clarizen.API.V2_0.Data.countQuery count = ClarizenAPI.Count(query);
            if (count.IsCalledSuccessfully)
                Console.WriteLine("{0} user(s) found with the display name {1}", count.Data.count, searchTerm);
            else
                Console.WriteLine(count.Error);

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

EntityQuery

Here is how you could get all Users:

using System;
using Clarizen.API.V2_0;
using Clarizen.API.V2_0.Data;

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

            entityQuery entityQuery = ClarizenAPI.GetAllEntities("User", new string[] { "FirstName", "LastName", "Email", "UserName" });
            if (entityQuery.IsCalledSuccessfully)
            {
                Console.WriteLine("{0} users found", entityQuery.Data.entities.Length);
                foreach (dynamic user in entityQuery.Data.entities)
                {
                    Console.WriteLine("\t{0}\t{1} {2}, {3} (Username: {4})", user.id, user.FirstName, user.LastName, user.Email, user.UserName);
                }
                if (entityQuery.Data.paging.hasMore)
                    Console.WriteLine("There are more records than the ones shown here");
            }
            else
                Console.WriteLine(entityQuery.Error);

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

And here is how you could get all entities of a type and display their Name property:

using System;
using Clarizen.API.V2_0;
using Clarizen.API.V2_0.Data;

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

            string entityName = "Task";  // Could be any entity that has the Name field, e.g. Project
            entityQuery entityQuery = ClarizenAPI.GetAllEntities(entityName, new string[] { "Name" });
            if (entityQuery.IsCalledSuccessfully)
            {
                Console.WriteLine("{0} {1} entities found", entityQuery.Data.entities.Length, entityName);
                foreach (dynamic entity in entityQuery.Data.entities)
                {
                    Console.WriteLine("\t{0}\t{1}", entity.id, entity.Name);
                }
                if (entityQuery.Data.paging.hasMore)
                    Console.WriteLine("There are more records than the ones shown here");
            }
            else
                Console.WriteLine(entityQuery.Error);

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

AggregateQuery

This is the example on https://api.clarizen.com/V2.0/services/data/AggregateQuery.

using System;
using Clarizen.API.V2_0;
using Clarizen.API.V2_0.Data.Queries;

namespace ClarizenSamples.Queries
{
    class AggregateQuery
    {
        static AggregateQuery()
        {
            API ClarizenAPI = new API();
            if (!ClarizenAPI.Login("username", "password"))
                return;
            
            aggregateQuery query = 
                new aggregateQuery("Task",
                                   new fieldAggregation[] { new fieldAggregation("Count", "Name", "Cnt") },
                                   new string[] { "State" },
                                   new orderBy[] { new orderBy("Cnt", "Descending") });
            Clarizen.API.V2_0.Data.aggregateQuery aggregateQuery = ClarizenAPI.AggregateQuery(query);
            if (aggregateQuery.IsCalledSuccessfully)
            {
                Console.WriteLine("{0} entities found", aggregateQuery.Data.entities.Length);
                foreach (dynamic entity in aggregateQuery.Data.entities)
                {
                    Console.WriteLine("\t{0}\t{1}\t{2}", entity.id, entity.State.id, entity.Cnt);
                }
                if (aggregateQuery.Data.paging.hasMore)
                    Console.WriteLine("There are more records than the ones shown here");
            }
            else
                Console.WriteLine(aggregateQuery.Error);

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

GroupsQuery

using System;
using Clarizen.API.V2_0;
using Clarizen.API.V2_0.Data;

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

            entityQuery entityQuery = ClarizenAPI.GetAllEntities("User", new string[] { "FirstName", "LastName", "Email", "UserName" });
            if (entityQuery.IsCalledSuccessfully)
            {
                Console.WriteLine("{0} users found", entityQuery.Data.entities.Length);
                foreach (dynamic user in entityQuery.Data.entities)
                {
                    Console.WriteLine("\t{0}\t{1} {2}, {3} (Username: {4})", user.id, user.FirstName, user.LastName, user.Email, user.UserName);
                }
                if (entityQuery.Data.paging.hasMore)
                    Console.WriteLine("There are more records than the ones shown here");
            }
            else
                Console.WriteLine(entityQuery.Error);

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

NewsFeedQuery

using System;
using Clarizen.API.V2_0;
using Clarizen.API.V2_0.Data;

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

            newsFeedQuery op = ClarizenAPI.NewsFeedQuery(newsFeedMode.All, new string[] { "Body", "Text", "likesCount" });
            if (op.IsCalledSuccessfully)
            {
                Console.WriteLine("Current user has {0} items in his/her news feed", op.Data.items.Length);
                foreach (postFeedItem item in op.Data.items)
                {
                    //Console.WriteLine(item.message);
                    Console.WriteLine("\t{0}\t{1}\t{2} likes", item.message.id, item.message.Body, item.message.likesCount);
                }
            }
            else
                Console.WriteLine(op.Error);

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

EntityFeedQuery

using System;
using Clarizen.API.V2_0;
using Clarizen.API.V2_0.Data;

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

            string taskId = "/Task/4batkam0jt4xn0l3cdx4y8ueg4";
            entityFeedQuery op = ClarizenAPI.EntityFeedQuery(taskId, new string[] { "Body", "Text", "likesCount" });
            if (op.IsCalledSuccessfully)
            {
                int itemCount = op.Data.items.Length;
                Console.WriteLine("This task has {0} items in its social feed:", itemCount);
                if (itemCount > 0)
                {
                    foreach (dynamic discussionPost in op.Data.items)
                    {
                        Console.WriteLine("\t{0}\t{1}\t{2} likes", discussionPost.message.id, discussionPost.message.Body, discussionPost.message.likesCount);
                    }
                }
            }
            else
                Console.WriteLine(op.Error);

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

RepliesQuery

using System;
using Clarizen.API.V2_0;
using Clarizen.API.V2_0.Data;

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

            string discussionPostId = "/DiscussionPost/msg-4npjkyrj89buagdjkxtr4ja1o";
            repliesQuery op = ClarizenAPI.RepliesQuery(discussionPostId, new string[] { "Body", "Text", "likesCount" });
            if (op.IsCalledSuccessfully)
            {
                int itemCount = op.Data.items.Length;
                Console.WriteLine("This DiscussionPost has {0} replies{1}", itemCount, itemCount > 0 ? ":" : "");
                if (itemCount > 0)
                {
                    foreach (dynamic discussionPost in op.Data.items)
                    {
                        Console.WriteLine("\t{0}\t{1}\t{2} likes", discussionPost.message.id, discussionPost.message.Body, discussionPost.message.likesCount);
                    }
                }
            }
            else
                Console.WriteLine(op.Error);

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

GetCalendarInfo

using System;
using Clarizen.API.V2_0;
using Clarizen.API.V2_0.Data;

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

            string userId = "/User/4dxj4hbs5pqvmp2utn4q3ggib3";
            getCalendarInfo search = ClarizenAPI.GetCalendarInfo(userId);
            if (search.IsCalledSuccessfully)
            {
                Clarizen.API.V2_0.Data.Result.getCalendarInfo result = (Clarizen.API.V2_0.Data.Result.getCalendarInfo)search.Data;
                Console.WriteLine("Week Starts On: {0}", result.weekStartsOn);
                Console.WriteLine("Weekday information:");
                foreach (dayInformation info in result.weekDayInformation)
                {
                    Console.WriteLine("\tisWorkingDay: {0}\ttotalWorkingHours: {1:0.##}\tstartHour: {2:0.##}\tendHour: {3:0.##}",
                        info.isWorkingDay, info.totalWorkingHours, info.startHour, info.endHour);
                }
                Console.WriteLine("Default working day:");
                Console.WriteLine("\tisWorkingDay: {0}\ttotalWorkingHours: {1:0.##}\tstartHour: {2:0.##}\tendHour: {3:0.##}",
                    result.defaultWorkingDay.isWorkingDay, result.defaultWorkingDay.totalWorkingHours, result.defaultWorkingDay.startHour, result.defaultWorkingDay.endHour);
            }
            else
                Console.WriteLine(search.Error);

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

Search

using System;
using Clarizen.API.V2_0;
using Clarizen.API.V2_0.Data;

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

            string q = "ekin";
            search search = ClarizenAPI.Search(q);
            if (search.IsCalledSuccessfully)
                Console.WriteLine("{0} entities found with {1}", search.Data.entities.Length, q);
            else
                Console.WriteLine(search.Error);

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

Comments