OpenText | Content Web Services

Other Attributes (C#)

This tutorial walks you through all the steps needed to start using Other Attributes (such as External 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

  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. In this example we will be dealing with External Attributes:

    • 'External Create Date': Date external data object was created
    • 'External Identity': Account used to create external data object
    • 'External Identity Type': Type of account used to create external data object
    • 'External Modify Date': Date when external data object was last modified
    • 'External Source': Source type specified by creator of external data object
    We will need to create the document management service client and the auth token.

    // 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;
    
    
  3. Next, we will ask the document management client to get us an External Attribute template. You can do this by calling the Document Management's "GetAttributeGroupTemplate" method and pass in "ExternalAtt" for the 'Type' and 'Key' parameters (see the example below). This will save us having to create the object structure ourselves, and allow us to just fill in the values for each attribute from the template.

    AttributeGroup externalAttTemplate = null;
    try
    {
    	Console.Write("Getting External Attributes Template...");
    	externalAttTemplate = docManClient.GetAttributeGroupTemplate(ref otAuth, "ExternalAtt", "ExternalAtt");
    	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 and set its external attributes. 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;
    
    // External Create Date
    (externalAttTemplate.Values[0] as DateValue).Values[0] = fileInfo.CreationTime;
    
    // External Identity
    (externalAttTemplate.Values[1] as StringValue).Values[0] = "1000";
    
    // External Identity Type
    (externalAttTemplate.Values[2] as StringValue).Values[0] = "generic_userid";
    
    // External Modify Date
    (externalAttTemplate.Values[3] as DateValue).Values[0] = fileInfo.LastWriteTime;
    
    // External Source
    (externalAttTemplate.Values[4] as StringValue).Values[0] = "file_system";
    
    Metadata metadata = new Metadata();
    metadata.AttributeGroups = new AttributeGroup[] { externalAttTemplate };
    
    Node theMatrix = null;
    try
    {
    	Console.Write("Creating Document With External Attributes...");
    	
    	// the Node 'theMatrix' will have a Metadata section, which contains AttributeGroups 
    	// and one of them will be of Type 'ExternalAtt'
    	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 document was created successfully and it has the above external attributes (which can be found in the Function Menu > Properties page).

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

    Result

Source Code

References

Notes