cancel
Showing results for 
Search instead for 
Did you mean: 

How to create documents with version using cmis in c#?

divyabharathipa
Champ in-the-making
Champ in-the-making
Hi Team,


I am trying to create documents with versioning. I did upto check out. While checking I am getting conflict. First I am creating documents from that method i am passing docid/path as parameter to createdocument_version method. In that I am performing checkout .Upto that it is working fine. Please help me to solve that one .

Sample Code :
private void ConnectingUsingAtomPub_CreateDocument_Version(string path,IFolder newFolder)
        {

            Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;
            parameters[DotCMIS.SessionParameter.AtomPubUrl] = "http://127.0.0.1:8080/alfresco/cmisatom";
            parameters[DotCMIS.SessionParameter.User] = "admin";
            parameters[DotCMIS.SessionParameter.Password] = "root";
            SessionFactory factory = SessionFactory.NewInstance();
            ISession session = factory.GetRepositories(parameters)[0].CreateSession();
          
            string formattedName = Path.GetFileName(fileUpload1.FileName);
          
            // get / define the document, which you would want to update with versioning
            Document doc = ((Document)session.GetObjectByPath(path));
            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 incase 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, formattedName);
            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 =formattedName,
                MimeType = "application/msword",
                Length = fileUpload1.FileBytes.Length,
                Stream = new MemoryStream(fileUpload1.FileBytes)
            };

            // create/update the document with new stream & version
            newFolder.CreateDocument(properties, contentStream, DotCMIS.Enums.VersioningState.CheckedOut);

            // finally check in the document
            IObjectId ob = doc.CheckIn(true, properties, null, "testing divya checking");

        }
4 REPLIES 4

divyabharathipa
Champ in-the-making
Champ in-the-making
I am unable to do with that one. But existing file is replaced with new one.Please let me know..
Please let me know where i am doing mistake.

   Dictionary<string, string> parameters = new Dictionary<string, string>();
            parameters[DotCMIS.SessionParameter.BindingType] = BindingType.AtomPub;
            parameters[DotCMIS.SessionParameter.AtomPubUrl] = "http://127.0.0.1:8080/alfresco/cmisatom";
            parameters[DotCMIS.SessionParameter.User] = "admin";
            parameters[DotCMIS.SessionParameter.Password] = "root";
            SessionFactory factory = SessionFactory.NewInstance();
            ISession session = factory.GetRepositories(parameters)[0].CreateSession();

            // get / define the document, which you would want to update with versioning
            IDocument doc = (IDocument)session.GetObjectByPath(path);
            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 incase of exception
            //    doc.CancelCheckOut();
            //    Response.Write("Exception checking out; must be checked out already: " + cbe.Message);
            //}
                IDocument pwc = (IDocument)session.GetObject(Iobj);
  
                    string formattedName = Path.GetFileName(fileUpload1.FileName);
                    // define dictionary for document
                    IDictionary<string, object> properties1 = new Dictionary<string, object>();
                    properties1.Add(PropertyIds.Name, "1" + formattedName);
                    properties1.Add(PropertyIds.ObjectTypeId, "cmis:document");
                 //   properties1.Add(PropertyIds.IsLatestVersion, "cmis:isLatestVersion");
                   properties1.Add(PropertyIds.ObjectId, objId);


                    String docText = "This is a sample document with an UPDATE";

                    byte[] content = Encoding.ASCII.GetBytes(docText);

                    // define new document stream object
                    ContentStream contentStream = new ContentStream
                    {
                        FileName = "1" + formattedName,
                        MimeType = "application/msword",
                        Length = content.Length,
                        Stream = new MemoryStream(content)
                    };
                    // create/update the document with new stream & version
                    //  newFolder.CreateDocument(properties1, contentStream, VersioningState.Major);
                 //newFolder.GetCheckedOutDocs().Contains(Iobj);
                // IDocument doc1 = newFolder.CreateDocument(properties1, contentStream, DotCMIS.Enums.VersioningState.CheckedOut);
                    // finally check in the document

                    IObjectId ob = pwc.CheckIn(false, properties1, contentStream, "Done");
                    doc = (Document)session.GetObject(ob);

dhamayandhi
Champ in-the-making
Champ in-the-making
Hi

I want to integrate alfresco for uploading new file with metadata
Edit file and metadata or only edit metadata or only edit file
delete file
query and get metadata details and document details and download document from alfresco
can you let me know is there any code is available using vb.net

You will find REST APIs in the out-of-the-box Alfresco for uploading a file,get metadata, edit metadata and so on. Please check out-of-the-box REST APIs in Alfresco. You can then make HTTP calls to them passing the required details from your application.

Hope this helps.

dhamayandhi
Champ in-the-making
Champ in-the-making
Hi Ramesh,

We are planning to use the Alfresco REST API form c#.NET code.
Can you give me one example code of upload document by calling Rest API from c#.
Based on that i can get an idea for implementing other functionalities.