Getting Started (C#)
This tutorial explains all the steps needed to get started developing applications that interact with Content Server using the CWS API with C# and Visual Studio.
Outline
- Create a New Project
- Add a Service Reference
- Using the Services
- Run the Program
- Source Code
- References
Create a New Project
The first thing we need is a new project in Visual Studio to work with.
-
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.
-
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 other CWS services you want to use.... <MetadataSources> <MetadataSource Address="http://localhost/cws/Authentication.svc" Protocol="http" SourceId="1" /> <MetadataSource Address="http://localhost/cws/Collaboration.svc" Protocol="http" SourceId="2" /> <MetadataSource Address="http://localhost/cws/ContentService.svc" Protocol="http" SourceId="3" /> <MetadataSource Address="http://localhost/cws/DocumentManagement.svc" Protocol="http" SourceId="4" /> <MetadataSource Address="http://localhost/cws/MemberService.svc" Protocol="http" SourceId="5" /> <MetadataSource Address="http://localhost/cws/WorkflowService.svc" Protocol="http" SourceId="6" /> </MetadataSources> ...
-
In the Solution Explorer, select the CWS service reference under the Service References folder. In the Project menu select Update Service Reference.
Using the Services
Now that the CWS services are ready to use we can go ahead and start writing some code. The first thing to do is to authenticate a user. To get started here we will use basic Content Server authentication using the CWS Authentication service. 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(); }
The other CWS services are all similar in the way they are used.
- Create a service client.
- Create an OTAuthentication object to pass along the authentication token with each request. You can use the same OTAuthentication object across all services so you do not have to create a separate one for each service.
- Use the service client to call operations on the service.
- Close the client.
Here is an example using the DocumentManagement service to get a user's favorites:
// 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; // Store the favorites Node[] favorites = null; // Call the GetAllFavorites() method to get the user's favorites try { Console.Write("Getting the user's favorites..."); favorites = docManClient.GetAllFavorites(ref otAuth); 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 docManClient.Close(); } // Output the user's favorites Console.WriteLine("User's Favorites:\n"); if (favorites != null) { foreach (Node node in favorites) { Console.WriteLine(node.Name); } } else { Console.WriteLine("No Favorites."); } Console.WriteLine();
Run the Program
If everything is setup correctly you should be able to successfully run the program.