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);
}
}

Ram AllaPosted Aug 10, 2016, 6:26 AM
Hi Surya, Nice article. I try to upload docs to Alfresco using dotCMIS but getting errors like CmisObjectNotFound error. Please forward me if you have any sample files. DO we need any customization in web.config file?. Please update me. Thanks
Massimo BeniniPosted Sep 30, 2015, 8:24 AM
Could you help me about usine property cm:taggable to add a "tag" in the document?
Divya PalivelaPosted Jul 1, 2014, 2:13 AM
Could you please let me know about document versioning
Vikram SamalPosted Feb 20, 2013, 11:41 PM
Hi Surya, Wonderful article but still wondering how to set custom metadata using CMIS. Please help if you have any clue. Regards Vikram
lj bzPosted Dec 20, 2012, 8:25 PM
hi,i'm trying to upload a document with versioning,in here:(folder.CreateDocument(properties, contentStream, DotCMIS.Enums.VersioningState.CheckedOut);), it's will error:Conflict.How can I solve this problem? I'm using DotCMIS 0.4.0,thank you
Claudio RiccardiPosted Dec 11, 2012, 6:06 AM
Hi! thank you for this interesting article. I'm trying to upload a new pdf document into an Alfresco folder but when I call the function folder.CreateDocument(properties, contentStream, null); as you suggested in point 3 the following exception appears: Cannot access http://serveraddress:8000/alfresco/service/cmis/s/workspace:SpacesStore/i/d370940e-6288-4114-b830-0637a1add201/children: Chunked encoding upload is not supported on the HTTP/1.0 protocol. How can I solve this problem? I'm using DotCMIS 0.4.0 tnanks in advance.
asith wjkeditedPosted Oct 9, 2012, 6:39 AMEdited Oct 9, 2012, 6:44 AM
hi, how can i get list of uploaded documents(contents) which are in specific folder through the c# thank you