cancel
Showing results for 
Search instead for 
Did you mean: 
gavincornwell
Star Collaborator
Star Collaborator

In the we looked at how to navigate the repository, this time we're going to create some files and folders.

As before all of the endpoints we'll cover in this blog post have been provided in a Postman collection and can be imported by clicking the "Run in Postman" button below.

button.svg

Let's start with creating a folder in our home folder. We use the same URL as we did for navigating the repository but this time we'll POST to it and use the -my- alias.

To create a folder named "My Folder" POST the body below using a Content-Type of application/json to http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children

{

  "name":"My Folder",

  "nodeType":"cm:folder"

}

This results in a response representing the newly created folder as shown below:

{

  "entry": {

    "aspectNames": [

      "cm:auditable"

    ],

    "createdAt": "2016-10-17T18:30:28.870+0000",

    "isFolder": true,

    "isFile": false,

    "createdByUser": {

      "id": "test",

      "displayName": "Test Test"

    },

    "modifiedAt": "2016-10-17T18:30:28.870+0000",

    "modifiedByUser": {

      "id": "test",

      "displayName": "Test Test"

    },

    "name": "My Folder",

    "id": "de8f6834-1d5a-4137-ab1f-67e6978f1aa8",

    "nodeType": "cm:folder",

    "parentId": "bd8f1283-3e84-4585-aafc-12da26db760f"

  }

}

You may have noticed that slightly more information about the node (aspectNames and properties) is returned by default but even here we are still using a "performance first" principle. The include parameter can also be used with create, see http://localhost:8080/api-explorer/#!/nodes/addNode for the list of extra information you can request.

Let's now create an empty file within our home folder. Once again we'll POST to http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children but this time we'll send the following body:

{

  "name": "my-file.txt",

  "nodeType": "cm:content"

}

This results in a response representing the newly created document as shown below:

{

  "entry": {

    "aspectNames": [

      "cm:auditable"

    ],

    "createdAt": "2016-10-17T18:58:38.717+0000",

    "isFolder": false,

    "isFile": true,

    "createdByUser": {

      "id": "test",

      "displayName": "Test Test"

    },

    "modifiedAt": "2016-10-17T18:58:38.717+0000",

    "modifiedByUser": {

      "id": "test",

      "displayName": "Test Test"

    },

    "name": "my-file.txt",

    "id": "e88e9b0f-7975-4398-beb4-db593b0b7aee",

    "nodeType": "cm:content",

    "content": {

      "mimeType": "text/plain",

      "mimeTypeName": "Plain Text",

      "sizeInBytes": 0,

      "encoding": "UTF-8"

    },

    "parentId": "bd8f1283-3e84-4585-aafc-12da26db760f"

  }

}

As well as accepting JSON the same endpoint also accepts multipart/form-data, allowing us to upload content from a standard HTML form or from the command line using curl.

Presuming there is a file named test.txt in the current directory try the following command:

curl -X POST -H "Authorization: Basic dGVzdDp0ZXN0" -H "Content-Type: multipart/form-data; boundary=----FormBoundary7MA4YWxkTrZu0gW" -F "filedata=@test.txt" "http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children"

Alternatively, the 3rd request in the Postman collection can be used after selecting a file in the "Body" tab as shown in the screenshot below.

Screen_Shot_2016-10-17_at_20_56_04.png

If you hadn't already noticed, one of the benefits of the API Explorer is that it allows you to "Try it out!" on all documented APIs. Unfortunately the OpenAPI specification does not currently allow multiple content types to be documented for the same endpoint. For POST /nodes/-my-/children we made the decision to focus the API Explorer on JSON to allow the creation of folders and files. We have however fully documented what is possible via multipart/form-data, we'll discuss a few of those options next.

To specify the name of the file that gets created you can use a form field called name as shown in the screenshot below. You can try this for yourself via the 4th request in the Postman collection.

Screen Shot 2016-10-18 at 20.04.27.png

When uploading content it's quite common for a file with the same name to exist, this will generate an error by default, to avoid the error the autoRename form field can be used as shown below. You can try this for yourself via the 5th request in the Postman collection. If a file name clash is detected a suffix will be added to the file name, for example my-file.txt will become my-file-1.txt.

Screen Shot 2016-10-18 at 20.25.36.png

The same thing can be achieved whilst creating folders and empty files via the same named query parameter, for example: http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/-my-/children?autoRena...

In some scenarios the path of the destination folder is known, so to avoid having to lookup the id of the folder a relativePath form field can be provided as shown in the screenshot below. You can try this for yourself via the 6th request in the Postman collection.

Screen Shot 2016-10-18 at 21.21.29.png

This will create a file named "my-file.txt" in the folder we created earlier i.e. in "/Company Home/User Homes/test/My Folder". The same thing can be achieved whilst creating folders and empty files by using the same named property in the body as follows:

{

   ...

   "relativePath": "/My Folder"

}

Another feature of the repository we can control when uploading content is the generation of a rendition. To have the thumbnail rendition used by Share generated, provide a renditions form field with a value of doclib as shown in the screenshot below. You can try this for yourself via the 7th request in the Postman collection.

Screen Shot 2016-10-18 at 21.43.36.png

Currently only one rendition can be requested, we plan to allow multiple in the future hence the plural form field name.

Finally, let's take a look at how we set properties. Any other form field will be presumed to represent a property to set. The screenshot below and the last request in the Postman collection shows how to set the cm:title, cm:description and exif:manufacturer properties. Properties have to follow the prefix:localname format and be a registered property via the content model otherwise they are silently ignored.

Screen Shot 2016-10-18 at 21.43.58.png

If we take a look at the response we'll see that the appropriate aspects have also been automatically applied.

{

  "entry": {

    ...

    "aspectNames": [

      "cm:versionable","cm:titled","cm:auditable","cm:author","exif:exif"

    ],

    "properties": {

      "cm:title": "The Title",

      "cm:versionType": "MAJOR",

      "cm:versionLabel": "1.0",

      "exif:manufacturer": "Canon",

      "cm:description":"The Description"

    }

  }

}

We've covered a lot of ground in this post but we still haven't looked at all the capabilities available; overwriting, versioning, custom types and associations will all be covered in future posts. we'll be looking at how to retrieve, update and delete files & folders.

34 Comments
gavincornwell
Star Collaborator
Star Collaborator

Hmm, interesting, I wasn't aware of that statement.

I wouldn't advocate testing the model change in production, try on a dev/test system first after making a backup 😉

In theory it should be possible to change the type hierarchy, however, cm:name is a mandatory property on cm:cmobject which your nodes are not likely to have which may cause some problems. If it does you may have to "migrate" all nodes by adding a cm:name property.

If you already have a production system using this type hierarchy maybe you could consider using the older v0 REST API to create your nodes? Then raise an enhancement request against the v1 REST API to support the creation of objects that extend sys:base.

ivanovpavel1983
Champ on-the-rise
Champ on-the-rise

Our classes that inherit from sys:base have property cm:name without this property it would be an error Smiley Happy while creating.

Now, I try to change our model in test/dev.

ivanovpavel1983
Champ on-the-rise
Champ on-the-rise

I've updated test. Everything works correctly. Tomorrow I will update model on prod.

Thanks!

shalini1403
Champ in-the-making
Champ in-the-making

Hi Gavin,

This blog was very helpful while i was creating a folder in alfresco from processmaker 3.2

I'm trying to upload a file using processmaker triggers by the following code:

$data = array(

"filedata" => "C:/Users/test/Desktop/ALFRESCO/test.doc"

);
$data_string = json_encode($data);


$ch = curl_init('http://localhost/alfresco/api/-default-/public/alfresco/versions/1/nodes/c5edfbbe-e9ee-4158-add1-37b...

curl_setopt($ch, CURLOPT_USERPWD, 'admin:test');

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:multipart/form-data'));

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
echo($result);

 I get the following error :

"No disk space available","statusCode":409,"briefSummary":"04090141 

Can u pls suggest what is wrong in the above code?

gavincornwell
Star Collaborator
Star Collaborator

You're POSTing using multipart/form-data (which is correct) but you're encoding as JSON first!?

Using plain curl you can use the -F option to pass in the file contents, you will need to do the equivalent in processmaker.

If this doesn't help try turn on debug using "log4j.logger.org.alfresco.rest".

shalini1403
Champ in-the-making
Champ in-the-making

Thanks Gavin!!

I have successfully uploaded multiple files using trigger in processmaker 3.2 

gavincornwell
Star Collaborator
Star Collaborator

That's great news, glad you got it working!

boyzoid
Champ in-the-making
Champ in-the-making

Gavin - this entire series of posts has been an incredible resource. It has answered most of the questions I have had regarding using the API. I do have a situation that I have been unable to figure out, though.

I need to add tags to files that are uploaded using the API.

Right now, I am waiting for a successful response from the upload and making another request to add the tags.

Is there a way to tag a file during the upload?

gavincornwell
Star Collaborator
Star Collaborator

Hi Scott,

Glad the blog posts were of some help to you.

No, unfortunately, there is no way to provide tags when uploading. However, that is a great idea so I'd encourage you to raise an enhancement request in JIRA.

chuchel
Champ in-the-making
Champ in-the-making

Hi Gavin,

I am using:

http://localhost:8080/alfresco/api/-default-/public/alfresco/versions/1/nodes/{parentID}/children

endpoint to create document in alfresco (parentID == parent folder and file content is delivered as multipart file). Everything works great, document is created and I can see it in share application. But seems like content of the document (docx in this case) is not being indexed (when I upload file manually with share i can search document by its content). Is this a known bug (feature) or am I missing something ?

Cheers,

Leszek.