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.
// Define a dictionary with key-value pairs
Dictionary<string, string> parameters = new Dictionary<string, string>();
// Define binding type (AtomPub in this case)
parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;
// Define CMIS service URL for Alfresco
parameters[DotCMIS.SessionParameter.AtomPubUrl] = "https://localhost:8080/alfresco/service/cmis";
// Define Alfresco admin username
parameters[DotCMIS.SessionParameter.User] = "admin";
// Define Alfresco admin password (consider using a more secure method for storing credentials)
parameters[DotCMIS.SessionParameter.Password] = "w4rth0g!";
// Create a session factory
SessionFactory factory = SessionFactory.NewInstance();
// Get the default repository using the session factory and parameters
// Create a session on the retrieved repository
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.
Step 1. Create a folder in the Repository.
Find the inline code comments.
private void ConnectingUsingAtomPub_CreateFolder()
{
// define dictionary with key value pair
Dictionary<string, string> parameters = new Dictionary<string, string>();
// define binding type, in our example we are using ATOMPUB as stated above
parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;
// define CMIS available path which is already available under alfresco
parameters[DotCMIS.SessionParameter.AtomPubUrl] = "https://localhost:8080/alfresco/service/cmis";
// alfresco portal admin user name
parameters[DotCMIS.SessionParameter.User] = "admin";
// alfresco portal admin password
parameters[DotCMIS.SessionParameter.Password] = "admin";
// define session factory
SessionFactory factory = SessionFactory.NewInstance();
// using session factory get the default repository, on this repository we would be performing actions & create session on this repository
ISession session = factory.GetRepositories(parameters)[0].CreateSession();
// get the root folder in which we would be creating a new folder
IFolder root = session.GetRootFolder();
// print statement before creating a folder
Response.Write("<br /> Creating 'NewFolder: Surya' in the root folder");
// define dictionary with key value pair for new folder that we are going to create
IDictionary<string, object> properties = new Dictionary<string, object>();
// below property define name
properties.Add(PropertyIds.Name, "Surya");
// below property define object type & object types can be folder, document etc., in our case it is folder
properties.Add(PropertyIds.ObjectTypeId, "cmis:folder");
// create a folder in root folder which we defined above @ session.GetRootFolder()
IFolder newFolder = root.CreateFolder(properties);
// check to see did it work?
IItemEnumerable<ICmisObject> childrens = root.GetChildren();
// writing all folders
Response.Write("Now finding the following objects in the root folder:-");
foreach (ICmisObject item in childrens)
{
Response.Write("<br /> –Name: " + item.Name);
}
}
Step 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();
// create a session object for default repository
ISession session = factory.GetRepositories(parameters)[0].CreateSession();
// get all folders
IFolder root = session.GetRootFolder();
// print each object
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);
}
}
}
Step 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();
// document name
string formattedName = "SuryaPrakashTesting.doc";
// define dictionary
IDictionary<string, object> properties = new Dictionary<string, object>();
properties.Add(PropertyIds.Name, formattedName);
// define object type as document, as we wanted to create document
properties.Add(PropertyIds.ObjectTypeId, "cmis:document");
// read an empty document with empty bytes
// fileUpload1: is a .NET file upload control
ContentStream contentStream = new ContentStream
{
FileName = formattedName,
MimeType = "application/msword",
Length = fileUpload1.FileBytes.Length,
Stream = new MemoryStream(fileUpload1.FileBytes)
};
// this statement would create document in default repository
folder.CreateDocument(properties, contentStream, null);
}
Step 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/<documented>
- 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();
// get/define the document, which you would want to update with versioning
Document doc = ((Document) session.GetObject("workspace://SpacesStore/c38e8efa-04dd-46e1-938c-a20884dff5ee"));
string objId = string.Empty;
try
{
// checkout the doc
IObjectId Iobj = doc.CheckOut();
objId = Iobj.Id;
Response.Write("Checked out doc ID: " + objId);
}
catch (Exception cbe)
{
// cancel the checkout in case of exception
doc.CancelCheckOut();
Response.Write("Exception checking out; must be checked out already: " + cbe.Message);
}
// define dictionary for document
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();
// define new document stream object
ContentStream contentStream = new ContentStream
{
FileName = "test.doc",
MimeType = "application/msword",
Length = fileUpload1.FileBytes.Length,
Stream = new MemoryStream(fileUpload1.FileBytes)
};
// create/update the document with new stream & version
folder.CreateDocument(properties, contentStream, DotCMIS.Enums.VersioningState.CheckedOut);
// finally check in the document
IObjectId ob = doc.CheckIn(true, properties, null, "testing prakash checking");
}
Happy Coding... Hope this helps!