viernes, 1 de noviembre de 2013

ALFRESCO + CMIS + .NET

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DotCMIS;
using DotCMIS.Client.Impl;
using DotCMIS.Client;
using DotCMIS.Data.Impl;
using DotCMIS.Data.Extensions;
using System.Web;

namespace IntegracionCMIS
{
    class Program
    {
        static void Main(string[] args)
        {
            Program program = new Program();
            program.ConnectingUsingAtomPub_CreateFolder();
        }

        private void ConnectingUsingAtomPub_CreateFolder()
        {
            // define dictonary with key value pair
            Dictionary parameters = new Dictionary();
            // 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] = "http://192.168.100.44:8080/alfresco/service/cmis";
            // alfresco portal admin user name
            parameters[DotCMIS.SessionParameter.User] = "edmon";
            // alfresco portal admin password
            parameters[DotCMIS.SessionParameter.Password] = "edmon";
            // 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
            Console.Write("Creating 'NewFolder: Surya' in the root folder");
            // define dictory with key value pair for new folder that we are going to create
            IDictionary properties = new Dictionary();
            // below property define name
            properties.Add(PropertyIds.Name, "Surya");
            // below propert 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 childrens = root.GetChildren();
            // writing all folders
            Console.Write("Now finding the following objects in the root folder:-");
            foreach (ICmisObject item in childrens)
            {
                Console.Write("
Name: " + item.Name); } } // Navegación de la carpeta private void ConnectingUsingAtomPub_FolderNavigation() { Dictionary parameters = new Dictionary(); parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub; parameters[DotCMIS.SessionParameter.AtomPubUrl] = "https://localhost:8080/alfresco/service/cmis"; parameters[DotCMIS.SessionParameter.User] = "edmon"; parameters[DotCMIS.SessionParameter.Password] = "edmon"; 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 t in root.GetDescendants(-1)) { Console.Write(t); } } //Crear un documento private void ConnectingUsingAtomPub_CreateDocument() { Dictionary parameters = new Dictionary(); parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub; parameters[DotCMIS.SessionParameter.AtomPubUrl] = "https://localhost:8080/alfresco/service/cmis"; parameters[DotCMIS.SessionParameter.User] = "edmon"; parameters[DotCMIS.SessionParameter.Password] = "edmon"; 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 properties = new Dictionary(); properties.Add(PropertyIds.Name, formattedName); // define object type as document, as we wanted to create document properties.Add(PropertyIds.ObjectTypeId, "cmis:document"); // read a 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 statment would create document in default repository folder.CreateDocument(properties, contentStream, null); } // Crear una version del documento private void ConnectingUsingAtomPub_CreateDocument_Version() { Dictionary parameters = new Dictionary(); parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub; parameters[DotCMIS.SessionParameter.AtomPubUrl] = "https://localhost:8080/alfresco/service/cmis"; parameters[DotCMIS.SessionParameter.User] = "edmon"; parameters[DotCMIS.SessionParameter.Password] = "edmon"; 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; Console.Write("Checked out doc ID: " + objId); } catch (Exception cbe) { // cancel the checkout incase of exception doc.CancelCheckOut(); Console.Write("Exception checking out; must be checked out already: " + cbe.Message); } // define dictionary for document IDictionary properties = new Dictionary(); 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"); } } }