OpenText | Content Web Services

Categories & Attributes (C#)

This tutorial walks you through all the steps needed to start using Categories and Attributes in Content Server using the CWS API with C# and Visual Studio.

Outline

Setup Your Project

The first thing you need to do is create a new project. In your project you need to add a service reference for CWS and point it to each service we will use. In this sample we will use the following services:

Create a New Project

  1. Open Visual Studio.

  2. Open the File menu and select New → Project... (Ctrl + Shift + N).

  3. Select Visual C# as the project type, select Console Application as the project template, enter a name for the project, and then select OK.

    Create New Project

Add a Service Reference

Next, we need to add a service reference for CWS and point it to each service we want to use. For this sample we need to use the following services:

  1. Open the Project menu and select Add Service Reference...

  2. Enter the URL of the Authentication service in the Address box and select Go.

  3. Enter CWS as the namespace and select OK.

    Add Service Reference

  4. Open the Project menu and make sure the Show All Files option is selected. In the Solution Explorer, open the Service References → CWS → Reference.svcmap file.

  5. Add a new MetadataSource element for each one of the services we need to use.

    ...
    <MetadataSources>
    	<MetadataSource Address="http://localhost/cws/Authentication.svc" Protocol="http" SourceId="1" />
    	<MetadataSource Address="http://localhost/cws/DocumentManagement.svc" Protocol="http" SourceId="2" />
    </MetadataSources>
    ...
    
  6. In the Solution Explorer, select the CWS service reference under the Service References folder. In the Project menu select Update Service Reference.

Write Your Code

These are the namespaces used for the program (replace "C# Project name" with the name of your project):

using <C# Project name>.CWS;
using System;
using System.ServiceModel;
using System.Diagnostics;
using System.IO;
  1. The first step is to authenticate a user. This sample uses basic Content Server authentication. The authentication token will be used by the other service clients. You will need to change the username and password to use valid credentials of a user on your system.

    // The user's credentials
    string username = "username";
    string password = "password";
    
    // Create the Authentication service client
    AuthenticationClient authClient = new AuthenticationClient();
    
    // Store the authentication token
    string authToken = null;
    
    // Call the AuthenticateUser() method to get an authentication token
    try
    {
    	Console.Write("Authenticating User...");
    	authToken = authClient.AuthenticateUser(username, password);
    	Console.WriteLine("Success!\n");
    }
    catch (FaultException e)
    {
    	Console.WriteLine("Failed!");
    	Console.WriteLine("{0} : {1}\n", e.Code.Name, e.Message);
    	return;
    }
    finally
    {
    	// Always close the client
    	authClient.Close();
    }
    
  2. Next, we will create a category to store information about a movie. The category will contain the following attributes:

    • Directed By (Text Field)
    • Starring (Text Field)
    • Release Date (Date Field)
    • Running Time (Integer Field)
    • Miscellaneous Info (Set)
      • Genre (Set Text Field)

    You can change the parentID to create the category in a different location, if needed.

    // The ID of the parent container to create the category in
    int parentID = 2000;
    
    // Create the DocumentManagement service client
    DocumentManagementClient docManClient = new DocumentManagementClient();
    
    // Create the OTAuthentication object and set the authentication token
    OTAuthentication otAuth = new OTAuthentication();
    otAuth.AuthenticationToken = authToken;
    
    // Directed By
    StringAttribute directedBy = new StringAttribute();
    directedBy.DisplayLength = 64;
    directedBy.DisplayName = "Directed By";
    directedBy.MaxLength = 64;
    directedBy.MaxValues = 5;
    directedBy.MinValues = 1;
    directedBy.ReadOnly = new BooleanObject();
    directedBy.ReadOnly.Value = false;
    directedBy.Required = false;
    directedBy.Searchable = true;
    
    // Starring
    StringAttribute starring = new StringAttribute();
    starring.DisplayLength = 64;
    starring.DisplayName = "Starring";
    starring.MaxLength = 64;
    starring.MaxValues = 20;
    starring.MinValues = 1;
    starring.ReadOnly = new BooleanObject();
    starring.ReadOnly.Value = false;
    starring.Required = false;
    starring.Searchable = true;
    
    // Release Date
    DateAttribute releaseDate = new DateAttribute();
    releaseDate.DisplayName = "Release Date";
    releaseDate.MaxValues = 1;
    releaseDate.MinValues = 1;
    releaseDate.ReadOnly = new BooleanObject();
    releaseDate.ReadOnly.Value = false;
    releaseDate.Required = false;
    releaseDate.Searchable = true;
    releaseDate.ShowTime = false;
    
    // Running Time
    IntegerAttribute runningTime = new IntegerAttribute();
    runningTime.DisplayName = "Running Time (minutes)";
    runningTime.MaxValues = 1;
    runningTime.MinValues = 1;
    runningTime.ReadOnly = new BooleanObject();
    runningTime.ReadOnly.Value = false;
    runningTime.Required = false;
    runningTime.Searchable = true;
    
    // Miscellaneous Info
    SetAttribute miscellaneousInfo = new SetAttribute();
    miscellaneousInfo.DisplayName = "Miscellaneous Info";
    miscellaneousInfo.MaxValues = 1;
    miscellaneousInfo.MinValues = 1;
    miscellaneousInfo.ReadOnly = new BooleanObject();
    miscellaneousInfo.ReadOnly.Value = false;
    miscellaneousInfo.Required = false;
    miscellaneousInfo.Searchable = true;
    
    // Genre (Text Field attribute in the Miscellaneous Info set)
    StringAttribute genre = new StringAttribute();
    genre.DisplayLength = 64;
    genre.DisplayName = "Genre";
    genre.MaxLength = 64;
    genre.MaxValues = 1;
    genre.MinValues = 1;
    genre.ReadOnly = new BooleanObject();
    genre.ReadOnly.Value = false;
    genre.Required = false;
    genre.Searchable = true;
    
    // Add the Genre into the Miscellaneous Info set as the first set attribute
    miscellaneousInfo.Attributes = new CWS.Attribute[] { genre };
    
    // Add the category attributes in the desired order
    CWS.Attribute[] attributes = new CWS.Attribute[] { directedBy, starring, releaseDate, runningTime, miscellaneousInfo };
    
    // Create the category
    Node movieInfo = null;
    try
    {
    	Console.Write("Creating Category...");
    	movieInfo = docManClient.CreateCategory(ref otAuth, parentID, "MovieInfo", null, attributes, null);
    	Console.WriteLine("Success!\n");
    }
    catch (FaultException e)
    {
    	Console.WriteLine("Failed!");
    	Console.WriteLine("{0} : {1}\n", e.Code.Name, e.Message);
    	docManClient.Close();
    	return;
    }
    
  3. Next, we will get the category template to make it easier to create objects using the category. This will save us having to create the object structure and allow us to just fill in the values for each attribute from the template.

    AttributeGroup categoryTemplate = null;
    try
    {
    	Console.Write("Getting Movie Info Category Template...");
    	categoryTemplate = docManClient.GetCategoryTemplate(ref otAuth, movieInfo.ID);
    	Console.WriteLine("Success!\n");
    }
    catch (FaultException e)
    {
    	Console.WriteLine("Failed!");
    	Console.WriteLine("{0} : {1}\n", e.Code.Name, e.Message);
    	docManClient.Close();
    	return;
    }
    
  4. Finally, we will create a document using the category we created. You will need to change the filePath to point to a file on your local system.

    // The local file path of the document to create
    string filePath = @"C:\temp\TheMatrix.jpg";
    
    FileInfo fileInfo = new FileInfo(filePath);
    byte[] contents = File.ReadAllBytes(filePath);
    
    Attachment attachment = new Attachment();
    attachment.Contents = contents;
    attachment.CreatedDate = fileInfo.CreationTime;
    attachment.FileName = fileInfo.Name;
    attachment.FileSize = contents.Length;
    attachment.ModifiedDate = fileInfo.LastWriteTime;
    
    StringValue directedByValue = categoryTemplate.Values[0] as StringValue;
    directedByValue.Values = new string[] { "Andy Wachowski", "Larry Wachowski" };
    
    StringValue starringValue = categoryTemplate.Values[1] as StringValue;
    starringValue.Values = new string[] { "Keanu Reeves", "Laurence Fishburne", "Carrie-Anne Moss", "Hugo Weaving", "Joe Pantoliano" };
    
    DateValue releaseDateValue = categoryTemplate.Values[2] as DateValue;
    releaseDateValue.Values = new DateTime?[] { new DateTime(1999, 3, 31) };
    
    IntegerValue runningTimeValue = categoryTemplate.Values[3] as IntegerValue;
    runningTimeValue.Values = new long?[] { 136 };
    
    StringValue genreValue = ((TableValue) categoryTemplate.Values[4]).Values[0].Values[0] as StringValue;
    genreValue.Values = new string[] { "Sci-Fi" };
    
    Metadata metadata = new Metadata();
    metadata.AttributeGroups = new AttributeGroup[] { categoryTemplate };
    
    Node theMatrix = null;
    try
    {
    	Console.Write("Creating Document With Category...");
    	theMatrix = docManClient.CreateDocument(ref otAuth, parentID, "The Matrix", null, false, metadata, attachment);
    	Console.WriteLine("Success!\n");
    	Console.WriteLine("New document uploaded with ID = {0}\n", theMatrix.ID);
    }
    catch (FaultException e)
    {
    	Console.WriteLine("Failed!");
    	Console.WriteLine("{0} : {1}\n", e.Code.Name, e.Message);
    }
    finally
    {
    	docManClient.Close();
    }
    

Run the Program

If everything is setup correctly you should be able to successfully run the program. You can double check that everything worked by looking at your Content Server instance in a browser and verifying the category and document were created successfully.

  1. Open the Debug menu and select Start Without Debugging (Ctrl+F5).

    Result

References

Notes