cancel
Showing results for 
Search instead for 
Did you mean: 

Create Folder in the Repository using Alfresco ReSTful API

jeevitha_balu
Champ in-the-making
Champ in-the-making
Hello,

I specifically want to try creating a folder inside the alfresco Repository using Alfresco ReSTful api. Although, it is advisable to use CMIS, I would like to know if the same can be achieved using ReSTful api.

I referred the alfresco restful documentation (https://wiki.alfresco.com/wiki/Repository_RESTful_API_Reference#Create_folder_or_document_.28createD...) for creating a folder but the response was only the descendants of the parent folder.

Create folder syntax from documentation:
POST ../alfresco/service/api/node/{store_type}/{store_id}/{id}/descendants

Basically, this is what I tried:
../alfresco/service/api/node/workspace/SpacesStore/9dd479ca-d04e-4a6a-9a5a-55a51b484aa3/descendants?alf_ticket=TICKET_b34f0def6ed0c6208103bec0fc4ca86b7fa90813

The above URL displayed the descendants of the parent folder whose id is: 9dd479ca-d04e-4a6a-9a5a-55a51b484aa3

I have two questions:

1. Is it possible to create a folder inside the alfresco repository using Restful api? I do not intend to create folder in a alfresco site.
2. If yes, then could you please help me with a java or .Net snippet? 

Thanks in advance.
7 REPLIES 7

jpotts
World-Class Innovator
World-Class Innovator
The reason why CMIS is preferred is because it is so easy, especially if you are using Java or .NET. <a href="https://code.google.com/p/alfresco-api-java-examples/source/browse/alfresco-api-examples/src/main/ja...">Here</a> is an example that uses OpenCMIS (Java) to create a folder. And this happens using a RESTful API behind the scenes. One line of code. Four if you count the properties set up.

If you must use the raw binding, then take a look at page 24 of <a href="http://ecmarchitect.com/images/articles/cmis/cmis-article.pdf">this tutorial</a>. It shows a folder object being described in JSON and then being POSTed to the appropriate URL. (Note that the CMIS service URL has changed since that was written).

If I have failed to convince you to use CMIS, then what I suspect is happening is that you are issuing a GET instead of a POST. That would explain why you are getting a list of descendants back.

If you are for sure issuing a POST then please share exactly what you are posting (the body and any headers), not just the URL you are posting to.

Jeff


jeevitha_balu
Champ in-the-making
Champ in-the-making
Thanks for the reply, Jeff.

You are right. I'm issuing a 'GET' and it gives me the list of descendants back.
But in case of a 'POST', I get an error:
"The remote server returned an error: (405) Method Not Allowed.".

.Net Snippet used:

#region create a folder
        try
        {
           

            string folderURL = @"http://localhost:8080/alfresco/service/api/node/workspace/SpacesStore/9dd479ca-d04e-4a6a-9a5a-55a51b... alfresco ticket";
            WebRequest request = WebRequest.Create(folderURL);
            request.Method = "POST";
            request.ContentType = "application/json";
            WebResponse response = request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(dataStream);
            // The response from Alfresco server            
            status_code = ((HttpWebResponse)response).StatusCode;
            if (status_code == HttpStatusCode.OK)
            {
                Label1.Text = reader.ReadToEnd(); ;

            }
            else
            {
                Label1.Text = "Create New Folder failed with status: " + status_code.ToString();
            }

            reader.Close();
            response.Close();
        }
        catch (Exception ex)
        {
            Label1.Text = ex.Message;
        }
        #endregion

Note: The parent folder specified in the URL is a folder which resides inside the alfresco repository and it was created using CMIS. We wanted to check if we can create a sub-folder inside the parent folder using ReSTful api.

Hierarchy:  Repository > ParentFolder (created using CMIS) > SubFolder (To be created using ReSTful)

Will be great if you can shed us some light on what went wrong.

Thanks in advance,
Jeevitha

jpotts
World-Class Innovator
World-Class Innovator
Your post is missing a body. Take a look at the docs. The official docs on creating a folder live <a href="http://docs.alfresco.com/4.2/references/RESTful-NodeFolderPost.html">here</a>.

Here is some curl that will create a new folder named "foo" of type cm:folder in the folder specified by the node reference in the URL:


Metaversant:~ jpotts$ curl -X POST -uadmin:admin "http://localhost:8080/alfresco/s/api/node/folder/workspace/SpacesStore/d259c12a-a90b-465a-82dc-df71a..." -H "Content-Type: application/json" -d'{"name":"foo"}'
  {
     "nodeRef": "workspace://SpacesStore/faaf73a0-ff4a-4f75-a5a3-21c9e00d4f26"
  }


The JSON that contains "nodeRef" is the response.

You should be able to translate that into what you need for dotNET.

Jeff

jeevitha_balu
Champ in-the-making
Champ in-the-making
Thanks Jeff. I translated the same to .Net and with a JSON input for folder name and description, it worked fine.

Could send the complete code for my email :
rodrigo.apostolo@gmail.com
I need to create something like this in .Net .
Thank you

srini302
Champ in-the-making
Champ in-the-making
I want to create Folder under Company Home with CMIS Rest call and more over i want to upload document in that folder and update that document using Java can any one related to this plz help me with sample code

muralidharand
Star Contributor
Star Contributor