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:
- 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
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;
-
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(); }
-
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; }
-
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; }
-
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.
References
Notes
-
How do I get Category inheritance or set no Categories when creating a node?
Category inheritance
Method 1: pass in a null for the Metadata parametertheMatrix = 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). This is useful when Metadata contains AttributeGroups that are not Categories.AttributeGroup catAttrGroup = new AttributeGroup(); catAttrGroup.Type = "Category"; Metadata metadata = new Metadata(); metadata.AttributeGroups = new AttributeGroup[] { catAttrGroup }; 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 AttributeGroups that are not Categories.Metadata metadata = new Metadata(); // myCustomAttributeGroup is an AttributeGroup that is not a Category AttributeGroup metadata.AttributeGroups = new AttributeGroup[] { myCustomAttributeGroup }; theMatrix = docManClient.CreateDocument(ref otAuth, parentID, "The Matrix", null, false, metadata, attachment);