CRUD & Data Examples

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

CRUD

The following example shows how to create, retrieve, update and delete a Task entity.

For this example, let's first create a POCO for Task:

namespace Clarizen.POCO
{
    public class Task
    {
        public string id { get; set; }
        public string Name { get; set; }

        public Task() { }

        public Task(string id, string name)
        {
            this.id = id;
            this.Name = name;
        }

        public Task(string name)
        {
            this.Name = name;
        }

    }
}

We can then use this object to create a new Task in Clarizen and perform RUD operations on that Task.

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

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

            // Create a new task
            Task task = new Task("Here is a new task");
            string newTaskId = String.Empty;
            objects_put putObject = ClarizenAPI.CreateObject("/Task", task);
            if (putObject.IsCalledSuccessfully)
            {
                newTaskId = putObject.Data.id;
                Console.WriteLine("New task id: {0}", newTaskId);
            }
            else
                Console.WriteLine(putObject.Error);
            
            if (!String.IsNullOrEmpty(newTaskId))
            {
                // Get task
                objects_get objects = ClarizenAPI.GetObject(newTaskId, new string[] { "Name" });
                if (objects.IsCalledSuccessfully)
                    Console.WriteLine("Task Id: {0}", objects.Data.id);
                else
                    Console.WriteLine(objects.Error);

                // Update task
                Task updateTask = new Task();
                updateTask.Name = "Here is the updated task name";
                objects_post postObject = ClarizenAPI.UpdateObject(newTaskId, updateTask);
                if (postObject.IsCalledSuccessfully)
                    Console.WriteLine("Task updated successfully");
                else
                    Console.WriteLine(postObject.Error);

                // Delete task
                objects_delete deleteObject = ClarizenAPI.DeleteObject(newTaskId);
                if (deleteObject.IsCalledSuccessfully)
                    Console.WriteLine("Task deleted successfully");
                else
                    Console.WriteLine(deleteObject.Error);
            }

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

    }
}

CreateAndRetrieve

For this method we could use the same Task POCO we created above.

namespace Clarizen.POCO
{
    public class Task
    {
        public string id { get; set; }
        public string Name { get; set; }

        public Task() { }

        public Task(string id, string name)
        {
            this.id = id;
            this.Name = name;
        }

    }
}

We can then send this POCO to Clarizen to create a Task and immediately retrieve the newly created entity.

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

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

            Task task = new Task("/Task", "Here is a new task");

            createAndRetrieve op = ClarizenAPI.CreateAndRetrieve(task, new string[] { "Name" });
            if (op.IsCalledSuccessfully)
            {
                Console.WriteLine("\t{0}\t{1}", op.Data.entity.id, op.Data.entity.Name);
            }
            else
                Console.WriteLine(op.Error);

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

GetTemplateDescriptions

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

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

            string typeName = "Project";
            getTemplateDescriptions search = ClarizenAPI.GetTemplateDescriptions(typeName);
            if (search.IsCalledSuccessfully)
            {
                Console.WriteLine("{0} templates found for {1}", search.Data.templates.Length, typeName);
                if (search.Data.templates.Length > 0)
                {
                    foreach (string template in search.Data.templates)
                    {
                        Console.WriteLine("\t{0}", template);
                    }
                }
            }
            else
                Console.WriteLine(search.Error);

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

CreateFromTemplate

This example uses the Project POCO:

namespace Clarizen.POCO
{
    public class Project
    {
        public string id { get; set; }
        public string Name { get; set; }

        public Project(string id, string name)
        {
            this.id = id;
            this.Name = name;
        }

        public Project(string name)
        {
            this.Name = name;
        }
    }
}

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

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

            Project project = new Project("/Project", "Ekin's test project");
            string templateName = "Your First Project";
            string parentId = String.Empty;

            createFromTemplate op = ClarizenAPI.CreateFromTemplate(project, templateName, parentId);
            if (op.IsCalledSuccessfully)
                Console.WriteLine("New entity created. Id: {0}", op.Data.id);
            else
                Console.WriteLine(op.Error);

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

ChangeState

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

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

            string newState = "Completed";  // Could be Draft, for example

            // First we will get all tasks with due dates earlier than yesterday that are not Completed
            string yesterday = DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd");
            IClarizenQuery query = new Query()
                                       .Select("Name,Work,State")
                                       .From("Task")
                                       .Where(String.Format("DueDate<{0} AND State<>\"{1}\"", yesterday, newState));

            Clarizen.API.V2_0.Data.query CZQuery = ClarizenAPI.ExecuteQuery(query);
            if (CZQuery.IsCalledSuccessfully)
            {
                if (CZQuery.Data.entities.Length > 0)
                {
                    // Next, change the state of those tasks to Completed
                    // Note that this does not work for tasks in multiple projects
                    Clarizen.API.V2_0.Data.changeState cs = ClarizenAPI.ChangeState(CZQuery.Data.GetEntityIds(), newState);
                    if (cs.IsCalledSuccessfully)
                    {
                        Console.WriteLine("{0} tasks set to {1}", CZQuery.Data.entities.Length, newState);
                    }
                    else
                        Console.WriteLine(cs.Error);
                }
                else
                    Console.WriteLine("No tasks found with due dates earlier than yesterday");

                if (CZQuery.Data.paging.hasMore)
                    Console.WriteLine("There are more records than the ones processed here");
            }
            else
                Console.WriteLine(CZQuery.Error);

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

CreateDiscussion

This example uses the DiscussionPost POCO:

namespace Clarizen.POCO
{
    public class DiscussionPost
    {
        public string id { get; set; }
        public string Body { get; set; }
        public string Container { get; set; }
        public string Via { get; set; }

        public DiscussionPost(string id, string Body, string Container, string Via)
        {
            this.id = id;
            this.Body = Body;
            this.Container = Container;
            this.Via = Via;
        }
    }
}

using System;
using Clarizen.API.V2_0;
using Clarizen.POCO;

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

            string parentTaskId = "/Task/4batkam0jt4xn0l3cdx4y8ueg4";
            DiscussionPost discussionPost = new DiscussionPost("/DiscussionPost", "This discussion is created by API", parentTaskId, "Integration Tool");
            Clarizen.API.V2_0.Data.createDiscussion op = ClarizenAPI.CreateDiscussion(discussionPost);
            if (op.IsCalledSuccessfully)
                Console.WriteLine("Discussion created successfully");
            else
                Console.WriteLine(op.Error);

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

Comments