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:
- Authentication
- DocumentManagement
Create a New Project
-
Open Visual Studio.
-
Open the File menu and select New → Project... (Ctrl + Shift + N).
-
Select Visual C# as the project type, select Console Application as the project template, enter a name for the project, and then select OK.
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:
- Authentication
- DocumentManagement
-
Open the Project menu and select Add Service Reference...
-
Enter the URL of the Authentication service in the Address box and select Go.
-
Enter
CWS
as the namespace and select OK. -
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.
-
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> ...
-
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
-
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
andpassword
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(); }
-
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
// 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;
-
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; }
-
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).
Source Code
References
Notes
-
How do I get Category inheritance or set no Categories when creating a node with External Attributes?
Category inheritance
Method 1: pass in a null for the Metadata parameter. The External Attributes will be set to their default values (ex. null for dates, empty strings for strings).theMatrix = docManClient.CreateDocument(ref otAuth, parentID, "The Matrix", null, false, null, attachment);
Method 2: create a new AttributeGroup, set the 'Type' to 'Category' and pass it in when setting Metadata's AttributeGroups list (this 'empty' Category AttributeGroup must occur only once in the list, but it doesn't have to be the only thing in there). Use this when you're also specifying an 'ExternalAtt' Attribute Group.// Set up your External Attributes as described in the example AttributeGroup externalAttrGroup = docManClient.GetAttributeGroupTemplate(ref otAuth, "ExternalAtt", "ExternalAtt"); AttributeGroup catAttrGroup = new AttributeGroup(); catAttrGroup.Type = "Category"; Metadata metadata = new Metadata(); metadata.AttributeGroups = new AttributeGroup[] { catAttrGroup, externalAttrGroup }; theMatrix = docManClient.CreateDocument(ref otAuth, parentID, "The Matrix", null, false, metadata, attachment);
Set no Categories
Method 1: pass in an empty Metadata object for the Metadata parametertheMatrix = docManClient.CreateDocument(ref otAuth, parentID, "The Matrix", null, false, new Metadata(), attachment);
Method 2: do not pass in any Category AttributeGroups. This is useful when Metadata contains an External Attribute group.Metadata metadata = new Metadata(); // externalAttrGroup is an AttributeGroup of Type 'Externalatt' metadata.AttributeGroups = new AttributeGroup[] { externalAttrGroup }; theMatrix = docManClient.CreateDocument(ref otAuth, parentID, "The Matrix", null, false, metadata, attachment);