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:
- Directly using Alfresco services (Or)
- 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.
- Create a folder
- Folder Navigation
- Create document
- 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:
- using DotCMIS;
- using DotCMIS.Client.Impl;
- using DotCMIS.Client;
- using DotCMIS.Data.Impl;
- 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:
-
- Dictionary<string, string> parameters = new Dictionary<string, string>();
-
-
- parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;
-
-
- parameters[DotCMIS.SessionParameter.AtomPubUrl] = "https://localhost:8080/alfresco/service/cmis";
-
-
- parameters[DotCMIS.SessionParameter.User] = "admin";
-
-
- parameters[DotCMIS.SessionParameter.Password] = "w4rth0g!";
-
-
- SessionFactory factory = SessionFactory.NewInstance();
-
-
- 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:
- private void ConnectingUsingAtomPub_CreateFolder() {
-
- Dictionary < string, string > parameters = new Dictionary < string, string > ();
-
-
- parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;
-
-
- parameters[DotCMIS.SessionParameter.AtomPubUrl] = "https://localhost:8080/alfresco/service/cmis";
-
-
- parameters[DotCMIS.SessionParameter.User] = "admin";
-
-
- parameters[DotCMIS.SessionParameter.Password] = "admin";
-
-
- SessionFactory factory = SessionFactory.NewInstance();
-
-
- ISession session = factory.GetRepositories(parameters)[0].CreateSession();
-
-
- IFolder root = session.GetRootFolder();
-
-
- Response.Write("<br /> Creating 'NewFolder: Surya' in the root folder");
-
-
- IDictionary < string, object > properties = new Dictionary < string, object > ();
-
-
- properties.Add(PropertyIds.Name, "Surya");
-
-
- properties.Add(PropertyIds.ObjectTypeId, "cmis:folder");
-
-
- IFolder newFolder = root.CreateFolder(properties);
-
-
- IItemEnumerable < ICmisObject > childrens = root.GetChildren();
-
-
- Response.Write("Now finding the following objects in the root folder:-");
- foreach(ICmisObject item in childrens) {
- Response.Write("<br /> –Name: " + item.Name);
- }
- }
2. Folder Navigation:
- private void ConnectingUsingAtomPub_FolderNavigation()
- {
- Dictionary<string, string> parameters = new Dictionary<string, string>();
-
- parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;
- parameters[DotCMIS.SessionParameter.AtomPubUrl] = "https://localhost:8080/alfresco/service/cmis";
- parameters[DotCMIS.SessionParameter.User] = "admin";
- parameters[DotCMIS.SessionParameter.Password] = "admin";
-
- SessionFactory factory = SessionFactory.NewInstance();
-
-
- ISession session = factory.GetRepositories(parameters)[0].CreateSession();
-
-
- IFolder root = session.GetRootFolder();
-
-
- foreach (ITree<IFileableCmisObject> t in root.GetDescendants(-1))
- {
- PrintTree(t);
- }
- }
-
- private void PrintTree(ITree<IFileableCmisObject> tree)
- {
- Response.Write("<br />Descendant " + tree.Item.Name + "ID" + tree.Item.Id);
- if (tree.Children != null)
- {
- foreach (ITree<IFileableCmisObject> treeItem in tree.Children)
- {
- PrintTree(treeItem);
- }
- }
- }
3. Create Document in Repository
- private void ConnectingUsingAtomPub_CreateDocument() {
- Dictionary < string, string > parameters = new Dictionary < string, string > ();
- parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;
- parameters[DotCMIS.SessionParameter.AtomPubUrl] = "https://localhost:8080/alfresco/service/cmis";
- parameters[DotCMIS.SessionParameter.User] = "admin";
- parameters[DotCMIS.SessionParameter.Password] = "admin";
- SessionFactory factory = SessionFactory.NewInstance();
- ISession session = factory.GetRepositories(parameters)[0].CreateSession();
-
- IOperationContext oc = session.CreateOperationContext();
- oc.IncludeAcls = true;
-
- IFolder folder = session.GetRootFolder();
-
-
- string formattedName = "SuryaPrakashTesting.doc";
-
-
- IDictionary < string, object > properties = new Dictionary < string, object > ();
- properties.Add(PropertyIds.Name, formattedName);
-
-
- properties.Add(PropertyIds.ObjectTypeId, "cmis:document");
-
-
-
- ContentStream contentStream = new ContentStream {
- FileName = formattedName,
- MimeType = "application/msword",
- Length = fileUpload1.FileBytes.Length,
- Stream = new MemoryStream(fileUpload1.FileBytes)
- };
-
-
- folder.CreateDocument(properties, contentStream, null);
- }
4. Create a document with versioning: The code will be as:
-
Authenticate & Create a session object
-
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>
-
Checkout the document
-
Create/update the document
-
Checkin the document
- private void ConnectingUsingAtomPub_CreateDocument_Version() {
- Dictionary < string, string > parameters = new Dictionary < string, string > ();
- parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;
- parameters[DotCMIS.SessionParameter.AtomPubUrl] = "https://localhost:8080/alfresco/service/cmis";
- parameters[DotCMIS.SessionParameter.User] = "admin";
- parameters[DotCMIS.SessionParameter.Password] = "admin";
- SessionFactory factory = SessionFactory.NewInstance();
- ISession session = factory.GetRepositories(parameters)[0].CreateSession();
-
-
- Document doc = ((Document) session.GetObject("workspace://SpacesStore/c38e8efa-04dd-46e1-938c-a20884dff5ee"));
- string objId = string.Empty;
- try {
-
-
- IObjectId Iobj = doc.CheckOut();
- objId = Iobj.Id;
-
- Response.Write("Checked out doc ID: " + objId);
- } catch (Exception cbe) {
-
- doc.CancelCheckOut();
- Response.Write("Exception checking out; must be checked out already: " + cbe.Message);
- }
-
-
- IDictionary < string, object > properties = new Dictionary < string, object > ();
- properties.Add(PropertyIds.Name, "test.doc");
- properties.Add(PropertyIds.ObjectTypeId, "cmis:document");
- properties.Add(PropertyIds.ObjectId, objId);
- IFolder folder = session.GetRootFolder();
-
-
- ContentStream contentStream = new ContentStream {
- FileName = "test.doc",
- MimeType = "application/msword",
- Length = fileUpload1.FileBytes.Length,
- Stream = new MemoryStream(fileUpload1.FileBytes)
- };
-
-
- folder.CreateDocument(properties, contentStream, DotCMIS.Enums.VersioningState.CheckedOut);
-
-
- IObjectId ob = doc.CheckIn(true, properties, null, "testing prakash checking");
- }
Happy Coding... Hope this helps!