Checking If An Item Is In Workflow With Core Service

During your quest of making custom applications using Tridion’s Core Service API, you may have come across the need to check whether or not an item is currently in a Workflow Process. Luckily, unlike some other tasks you may need to do with the Core Service API, this task is easily done by checking the WorkflowInfo property that is included in the PageData and ComponentData classes.

// Open a page in Core Service.
PageData page = client.Read(yourPageUri, new ReadOptions()) as PageData;

if (page.WorkflowInfo != null)
{
    // Ladies and gentlemen, WorkflowInfo property was not null, this page is in workflow
}
else
{
    // WorkflowInfo property is null, the page is not in workflow
}

Great, but now that you know that your item is in workflow, you’ll probably be wanting to grab some Workflow related items. The good news is, the WorkflowInfo class contains most of the data you’ll need to interact with the Workflow for that item. Here are some common things you’ll probably do with this property:

// Get the ProcessInstanceData from the WorkflowInfo property
ProcessInstanceData processInstance = 
    (ProcessInstanceData)client.Read(page.WorkflowInfo.ProcessInstance.IdRef, null);

// Get the ProcessDefinitionData from the process instnace
ProcessDefinitionData processDefinition =
    (ProcessDefinitionData)client.Read(processInstance.ProcessDefinition.IdRef, null);

// Getting the ActivityInstanceData from the WorkflowInfo property...
ActivityInstanceData activityInstance = 
    (ActivityInstanceData)client.Read(page.WorkflowInfo.ActivityInstance.IdRef, null);

// Get the ActivityDefinitionData from the activity instance
ActivityDefinitionData activityDefinition =
    (ActivityDefinitionData)client.Read(activityInstance.ActivityDefinition.IdRef, null);

// Note that if you only wanted to get the ActivityDefinition's Description, you can get that from the WorkflowInfo's ActivityDefinitionDescription property.
if (page.WorkflowInfo.ActivityDefinitionDescription.Equals(activityDefinition.Description))
{
    // true of course...
}

// Check how long its been since the activity was started...
if (page.WorkflowInfo.StartDate.HasValue)
{
    TimeSpan timeSinceStart = DateTime.Now - page.WorkflowInfo.StartDate.Value;
}

Other Useful Properties of WorkflowInfo

Here are the other properties of WorkflowInfo that wasn’t included in the samples above:

ActivityState: The enum value of the activity’s current state.
Assignee: The Link<TrusteeData> of the assignee.
CreationDate: The Nullable<DateTime> that the activity was created.
FinishDate: The Nullable<DateTime> that the previsou activity instance was finished.
Performer: The Link<UserData> of the activity’s performer.
PreviousMessage: The FinishMessage of the previous Activity.

Crocodoc .NET API Library Released

For those of you reading the title and asking, “What is this Crocodoc???”, Crocodoc is a tool that “converts Microsoft Office and PDF documents to HTML5 so your users can view and collaborate right in your web app.” What that means is, you can present your customers documents such as Word docs, PDFs, and Power Point presentations… embedded and viewable within your web pages. Long are the days where your customers must download your PDFs in order to view them.

That being said, my colleague and friend, Frank Taylor, needed to interact with the Crocodoc v2 REST API, but discovered Crocodoc was missing the .NET API. So after working together, we are now happy to announce that the Beta version of 1.0 for a .NET Wrapper has been released on its Google Code site. If you just want to jump straight into the action, you can download the binaries here. For those of you who might have worked with the Crocodoc Java or PHP API’s, you should feel comfortable with the .NET API.

Although the project’s Google Code site has plenty of samples (and there’s even an Examples project you can download and run as well), here’s a quick sample of its usage.

Crocodoc.ApiToken = "your-unique-api-token-right-here";
string sessionID;

try
{
    // Here, documentUUID is the uuid for the Document that you want to create a session for.
    sessionID = CrocodocSession.Create(documentUUID);
}
catch (CrocodocException ex)
{
    // Log or show error message if the Crocodoc server threw a web exception.
}

The above creates a unique session for a given document so that you can then embed it onto your webpage. The session allows you to set properties such as copyrighted, admin privies, or even allow the writing of annotations for your editors.

Here’s an example of uploading a new document to crocodoc.

try
{
    string newDocUUID = CrocodocDocument.Upload(@"C:\Documents\YourPDF.pdf");
}
catch (CrocodocException ex)
{

}

Your Help?

We have released it as version 1.0 Beta as we would like some of your help and feedback with the library. If you use Crocodoc and have been looking for a .NET API, try it out, and let us know any issues that you run into, or if you have any feature requests.

Quick Links

Crocodoc API Reference
Crocodoc .NET
Crocodoc .NET Binaries