Alfresco Integration With .NET Using CMIS

In this article, we will see how to upload documents to an Alfresco portal from a .NET application.
 
Alfresco, by default, exposes services for uploading documents & these services can be directly used by client applications or the client application can use the CMIS dll for uploading documents.
 
So in other words we can upload documents to Alfresco from the application by:
  1. Directly using Alfresco services (Or)
  2. Use CMIS to upload
Before we jump on to code building let us look into the following standard definitions.
 
Alfresco: Alfresco is the open-source alternative for Enterprise Content Management (ECM), providing Document Management, Collaboration, Records Management, Knowledge Management, Web Content Management, and Imaging.
 
CMIS: Content Management Interoperability Services (CMIS) is an open standard that defines an abstraction layer for controlling diverse document management systems and repositories using web protocols. CMIS defines a domain model plus Web Services and Restful AtomPub (RFC5023) bindings that can be used by applications.
 
Alfresco portal URL: https://localhost:8080/
 
CMIS: https://localhost:8080/alfresco/service/cmis
 
For now CMIS is available in .NET, JAVA & Python languages.
 
To work with our example, download CMIS (DotCMIS.dll ) from https://chemistry.apache.org/dotnet/dotcmis.html & add a referrrence to this dll to your web application.
 
In this article I will show cases following operations on Alfresco from a .NET web application using CMIS AtomPub binding. CMIS provides a couple of bindings through which we can connect to the Alfresco portal. We will explore the following features using AtomPub binding.
  1. Create a folder
  2. Folder Navigation
  3. Create document
  4. Create a document with Versioning
Note: Add DotCMIS.dll to your solution.
 
Add a demo.aspx page to your application & add the following statements for the namespaces in the DotCMIS dll that we just downloaded:
  1. using DotCMIS;  
  2. using DotCMIS.Client.Impl;  
  3. using DotCMIS.Client;  
  4. using DotCMIS.Data.Impl;  
  5. using DotCMIS.Data.Extensions; 
Before we do any operations on Alfresco using CMIS we need to first authenticate the user for which we need to script the following statements:
  1. // define dictonary with key value pair  
  2. Dictionary<stringstring> parameters = new Dictionary<stringstring>();  
  3.   
  4. // define binding type, in our example we are using ATOMPUB as stated above  
  5. parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;  
  6.   
  7. // define CMIS available path which is already available under alfresco  
  8. parameters[DotCMIS.SessionParameter.AtomPubUrl] = "https://localhost:8080/alfresco/service/cmis";  
  9.   
  10. // alfresco portal admin user name  
  11. parameters[DotCMIS.SessionParameter.User] = "admin";  
  12.   
  13. // alfresco portal admin password  
  14. parameters[DotCMIS.SessionParameter.Password] = "w4rth0g!";  
  15.   
  16. // define session factory  
  17. SessionFactory factory = SessionFactory.NewInstance();  
  18.   
  19. // using session factory get the default repository, on this repository we would be performing actions & create session on this repository  
  20. ISession session = factory.GetRepositories(parameters)[0].CreateSession(); 
Once you have a session object, we will be able to do operations using this session object only. You would be seeing the above code again & again in our following code blocks.
 
1. Create a folder in the Repository:
 
Find the inline code comments:
  1. private void ConnectingUsingAtomPub_CreateFolder() {  
  2.     // define dictonary with key value pair  
  3.     Dictionary < stringstring > parameters = new Dictionary < stringstring > ();  
  4.   
  5.     // define binding type, in our example we are using ATOMPUB as stated above  
  6.     parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;  
  7.   
  8.     // define CMIS available path which is already available under alfresco  
  9.     parameters[DotCMIS.SessionParameter.AtomPubUrl] = "https://localhost:8080/alfresco/service/cmis";  
  10.   
  11.     // alfresco portal admin user name  
  12.     parameters[DotCMIS.SessionParameter.User] = "admin";  
  13.   
  14.     // alfresco portal admin password  
  15.     parameters[DotCMIS.SessionParameter.Password] = "admin";  
  16.   
  17.     // define session factory  
  18.     SessionFactory factory = SessionFactory.NewInstance();  
  19.   
  20.     // using session factory get the default repository, on this repository we would be performing actions & create session on this repository  
  21.     ISession session = factory.GetRepositories(parameters)[0].CreateSession();  
  22.   
  23.     // get the root folder in which we would be creating a new folder  
  24.     IFolder root = session.GetRootFolder();  
  25.   
  26.     // print statement before creating a folder  
  27.     Response.Write("<br /> Creating 'NewFolder: Surya' in the root folder");  
  28.   
  29.     // define dictory with key value pair for new folder that we are going to create  
  30.     IDictionary < stringobject > properties = new Dictionary < stringobject > ();  
  31.   
  32.     // below property define name  
  33.     properties.Add(PropertyIds.Name, "Surya");  
  34.   
  35.     // below propert define object type & object types can be folder, document etc.., in our case it is folder  
  36.     properties.Add(PropertyIds.ObjectTypeId, "cmis:folder");  
  37.   
  38.     // create a folder in root folder which we defined above @ session.GetRootFolder()  
  39.     IFolder newFolder = root.CreateFolder(properties);  
  40.   
  41.     // check to see did it work?  
  42.     IItemEnumerable < ICmisObject > childrens = root.GetChildren();  
  43.   
  44.     // writing all folders  
  45.     Response.Write("Now finding the following objects in the root folder:-");  
  46.     foreach(ICmisObject item in childrens) {  
  47.         Response.Write("<br /> –Name: " + item.Name);  
  48.     }  
2. Folder Navigation:
  1. private void ConnectingUsingAtomPub_FolderNavigation()  
  2.  {  
  3.      Dictionary<stringstring> parameters = new Dictionary<stringstring>();  
  4.   
  5.      parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;  
  6.      parameters[DotCMIS.SessionParameter.AtomPubUrl] = "https://localhost:8080/alfresco/service/cmis";  
  7.      parameters[DotCMIS.SessionParameter.User] = "admin";  
  8.      parameters[DotCMIS.SessionParameter.Password] = "admin";  
  9.   
  10.      SessionFactory factory = SessionFactory.NewInstance();  
  11.   
  12.      // create a session object for default repository  
  13.      ISession session = factory.GetRepositories(parameters)[0].CreateSession();  
  14.   
  15.      // get all folders  
  16.      IFolder root = session.GetRootFolder();  
  17.   
  18.      // print each object  
  19.      foreach (ITree<IFileableCmisObject> t in root.GetDescendants(-1))  
  20.      {  
  21.          PrintTree(t);  
  22.      }  
  23.  }  
  24.   
  25.  private void PrintTree(ITree<IFileableCmisObject> tree)  
  26.  {  
  27.       Response.Write("<br />Descendant " + tree.Item.Name + "ID" + tree.Item.Id);  
  28.       if (tree.Children != null)  
  29.       {  
  30.           foreach (ITree<IFileableCmisObject> treeItem in tree.Children)  
  31.           {  
  32.               PrintTree(treeItem);  
  33.           }  
  34.       }  
  35.  } 
3. Create Document in Repository
  1. private void ConnectingUsingAtomPub_CreateDocument() {  
  2.     Dictionary < stringstring > parameters = new Dictionary < stringstring > ();  
  3.     parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;  
  4.     parameters[DotCMIS.SessionParameter.AtomPubUrl] = "https://localhost:8080/alfresco/service/cmis";  
  5.     parameters[DotCMIS.SessionParameter.User] = "admin";  
  6.     parameters[DotCMIS.SessionParameter.Password] = "admin";  
  7.     SessionFactory factory = SessionFactory.NewInstance();  
  8.     ISession session = factory.GetRepositories(parameters)[0].CreateSession();  
  9.   
  10.     IOperationContext oc = session.CreateOperationContext();  
  11.     oc.IncludeAcls = true;  
  12.   
  13.     IFolder folder = session.GetRootFolder();  
  14.   
  15.     // document name  
  16.     string formattedName = "SuryaPrakashTesting.doc";  
  17.   
  18.     // define dictionary  
  19.     IDictionary < stringobject > properties = new Dictionary < stringobject > ();  
  20.     properties.Add(PropertyIds.Name, formattedName);  
  21.   
  22.     // define object type as document, as we wanted to create document  
  23.     properties.Add(PropertyIds.ObjectTypeId, "cmis:document");  
  24.   
  25.     // read a empty document with empty bytes  
  26.     // fileUpload1: is a .net file upload control  
  27.     ContentStream contentStream = new ContentStream {  
  28.         FileName = formattedName,  
  29.             MimeType = "application/msword",  
  30.             Length = fileUpload1.FileBytes.Length,  
  31.             Stream = new MemoryStream(fileUpload1.FileBytes)  
  32.     };  
  33.   
  34.     // this statment would create document in default repository  
  35.     folder.CreateDocument(properties, contentStream, null);  
4. Create a document with versioning: The code will be as:
  1. Authenticate & Create a session object
  2. Identify the document which you would want to update with version (document is identified as : workspace://SpacesStore/c38e8efa-04dd-46e1-938c-a20884dff5ee
     
    i.e. workspace://SpacesStore/<documentId>
     
  3. Checkout the document
  4. Create/update the document
  5. Checkin the document
  1. private void ConnectingUsingAtomPub_CreateDocument_Version() {  
  2.  Dictionary < stringstring > parameters = new Dictionary < stringstring > ();  
  3.  parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;  
  4.  parameters[DotCMIS.SessionParameter.AtomPubUrl] = "https://localhost:8080/alfresco/service/cmis";  
  5.  parameters[DotCMIS.SessionParameter.User] = "admin";  
  6.  parameters[DotCMIS.SessionParameter.Password] = "admin";  
  7.  SessionFactory factory = SessionFactory.NewInstance();  
  8.  ISession session = factory.GetRepositories(parameters)[0].CreateSession();  
  9.   
  10.  // get / define the document, which you would want to update with versioning  
  11.  Document doc = ((Document) session.GetObject("workspace://SpacesStore/c38e8efa-04dd-46e1-938c-a20884dff5ee"));  
  12.  string objId = string.Empty;  
  13.  try {  
  14.   
  15.      // checkout the doc  
  16.      IObjectId Iobj = doc.CheckOut();  
  17.      objId = Iobj.Id;  
  18.   
  19.      Response.Write("Checked out doc ID: " + objId);  
  20.  } catch (Exception cbe) {  
  21.      // cancel the checkout incase of exception  
  22.      doc.CancelCheckOut();  
  23.      Response.Write("Exception checking out; must be checked out already: " + cbe.Message);  
  24.  }  
  25.   
  26.  // define dictionary for document  
  27.  IDictionary < stringobject > properties = new Dictionary < stringobject > ();  
  28.  properties.Add(PropertyIds.Name, "test.doc");  
  29.  properties.Add(PropertyIds.ObjectTypeId, "cmis:document");  
  30.  properties.Add(PropertyIds.ObjectId, objId);  
  31.  IFolder folder = session.GetRootFolder();  
  32.   
  33.  // define new document stream object  
  34.  ContentStream contentStream = new ContentStream {  
  35.      FileName = "test.doc",  
  36.          MimeType = "application/msword",  
  37.          Length = fileUpload1.FileBytes.Length,  
  38.          Stream = new MemoryStream(fileUpload1.FileBytes)  
  39.  };  
  40.   
  41.  // create/update the document with new stream & version  
  42.  folder.CreateDocument(properties, contentStream, DotCMIS.Enums.VersioningState.CheckedOut);  
  43.   
  44.  // finally check in the document  
  45.  IObjectId ob = doc.CheckIn(true, properties, null"testing prakash checking");  
Happy Coding... Hope this helps!