.NET Example: Add file in repository

Top Previous Topic Next Topic  Print this topic

This tutorial offers a .NET example on how the Repository Web Service can be accessed to add a new file to the Ecrion Omni System repository.

 

NOTE: In order to have the correct bindings for the external usage of Ecrion Omni System web services, you will need to add the host name in IIS  for the Ecrion Omni System web site. See Post Installation Steps

 

1. A new Console Application Project must be created in Visual Studio 2010. Any name can be set. For example, "WebServiceSample". .NET Framework 4 must be selected before continuing.

 

ws_net_sample_1

 

2. In Solution Explorer, "WebServiceSample" must be right-clicked and Add Reference selected.

 

ws_net_sample_2

 

3. In the Add Reference dialog-box, the .NET tab located at the left must be selected and the System.ServiceModel chosen

 

ws_net_sample_3

 

4. In Solution Explorer, "WebServiceSample" must be right-clicked and Add Service Reference selected.

 

ws_net_sample_4

 

5. In the Add Service Reference dialog-box, the address must be set to http://localhost:8091/REPWS/Ecrion.Repository.WebService.RepositoryWebService.svc. Replace localhost:8091 with the name of your host. Set the namespace to  "localhost”.

 

ws_net_sample_5

 

6. Now, the project will look like the following in Solution Explorer:

 

ws_net_sample_6

 

7. "Program.cs" must be opened and all the existing code lines must be replaced by the following:

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.ServiceModel;

using WebServiceSample.localhost;

 

namespace WebServiceSample

{

  class Program

   {

      static void Main(string[] args)

       {

          String username = "admin";

          String password = "admin";

          String institution = "Ecrion";

         

          String filePath = @"test.fo";

          String repositoryPath = @"Design\Sample";

          String fileName = "Sample.fo";

         

         

          try

           {

              // Set Login credentials

              LogIn credentials = new LogIn();

               credentials.UserName = username;

               credentials.Password = password;

               credentials.Institution = institution;              

             

              // Read file content

              byte[] fileContent = System.IO.File.ReadAllBytes(filePath);              

 

              // Initialize web service client

              EndpointAddress serviceAddress = new EndpointAddress("http://localhost:8091/REPWS/Ecrion.Repository.WebService.RepositoryWebService.svc");

              WebRepositoryClient webServiceEndpoint = new WebRepositoryClient(new BasicHttpBinding(), serviceAddress);

 

              //Check if the file already exists in repository                            

               localhost.File[] repoFiles = webServiceEndpoint.GetFiles(credentials, repositoryPath);

 

              bool fileFound = false;

              foreach (localhost.File file in repoFiles)

               {

                  if (file.Name == fileName)

                   {

                      // Lock the file

                       webServiceEndpoint.AquireEditingLock(credentials, repositoryPath, fileName);

                      try

                       {

                          // Add the file in repository as a new version of the existing one

                           webServiceEndpoint.Commit(credentials, repositoryPath, fileName, fileContent, null);

                       }

                      finally

                       {

                          // Unlock the file

                           webServiceEndpoint.ReleaseEditingLock(credentials, repositoryPath, fileName);

                       }

                       fileFound = true;

                      break;

                   }

               }

              if (fileFound == false)

               {

                  // Add file to repository

                   webServiceEndpoint.AddFile(credentials, repositoryPath, fileName, fileContent, null);

               }

 

           }

          catch (Exception e)

           {

              // Report errors

              Console.Out.WriteLine(e.Message);

              if (e.InnerException != null)

                  Console.Out.WriteLine(e.InnerException.ToString());                  

           }

       }

   }

}

Example 1: Adding a file to repository using the Repository Web Service

 

Notes:

In the above example, any FO file can be used. Below is the code for a sample fo, that must be copied into a .fo file. In this example, the file is named "test.fo".  Before running the application, the .fo file must be copied into the WebServiceSample\WebServiceSample\bin\Debug folder.
To specify a new address for the Repository Web Service, the URL in EndpointAddress must be modified.

 

 

<?xml version="1.0" encoding="utf-8"?>

<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xf="http://www.ecrion.com/xf/1.0">

 <fo:layout-master-set>

   <fo:simple-page-master master-name="all-pages" page-width="11in" page-height="8.5in">

     <fo:region-body region-name="xsl-region-body" margin="0.7in"/>

   </fo:simple-page-master>

 </fo:layout-master-set>

 <fo:page-sequence master-reference="all-pages">

   <fo:flow flow-name="xsl-region-body">

     <fo:block>

       Hello World

     </fo:block>

   </fo:flow>

 </fo:page-sequence>

</fo:root>

 

Example 2: Sample test.fo

 

 

8. The Ecrion Omni System web site can be accessed, to check the Repository.

 

If the file didn't exist before in the repository, a new file, named Sample.fo will now appear in the repository

 

ws_net_sample_7

 

 

If the file already existed in the repository, a new version of the file Sample.fo will appear in the Versions report.

 

ws_net_sample_8