cancel
Showing results for 
Search instead for 
Did you mean: 

Use of CMIS web scripts

lakshya
Champ in-the-making
Champ in-the-making
I got the link for using CMIS web script.
I want to create document / folder by hitting the URL provided by
http://wiki.alfresco.com/wiki/CMIS_Web_Scripts_Reference#Create_folder_or_document_.28createDocument...

The URL provided for it is
http://localhost:8080/alfresco/service/api/path/%7Bstore_type%7D/%7Bstore_id%7D/%7Bid%7D/descendants

How can i pass parameters like "new folder name", "location", etc in this url.

Because on hit of this url I just get the list of children  of Company Home.

Can anyone answer it quickly.. . .
23 REPLIES 23

mrogers
Star Contributor
Star Contributor
In general you will need to find and use a POST script to create content.    And you will specify your content within the content of your posted data.

lakshya
Champ in-the-making
Champ in-the-making
Can you please clarify it little more.
Because we are hitting the url given as POST only.

POST /alfresco/service/api/node/{store_type}/{store_id}/{id}/descendants
POST /alfresco/service/api/path/{store_type}/{store_id}/{id}/descendants

mrogers
Star Contributor
Star Contributor
In a RESTful API resources are identified by URIs and HTTP commands operate upon those resources.   The http commands are GET, PUT, POST, and DELETE messages which (very roughly) correspond to read, update, create and delete.

Each web script will negotiate the format of data it will accept and the format of data it will return.   In this particular case your decision is easy since this particular resource will only accept ATOM and will only return ATOM.

So to create a new folder you will need to POST appropriate atom content to the correct URI.

One useful trick that may help you is to look at the java script implementation, in this case descendants.post.atom.js.   You may also find unit tests calling this script.

lakshya
Champ in-the-making
Champ in-the-making
Can you please give an example for  like delete content

because when i hit URL provided by
DELETE /alfresco/service/api/node/{store_type}/{store_id}/{id}?includeChildren={includeChildren?}
DELETE /alfresco/service/api/path/{store_type}/{store_id}/{id}?includeChildren={includeChildren?}

given on http://wiki.alfresco.com/wiki/CMIS_Web_Scripts_Reference#Delete_item_.28deleteObject.29

it browses as
       http://localhost:8080/alfresco/service/api/node/workspace/SpacesStore/{id}?includeChildren={includeC...?}

it gives the list of children.


I have
item.delete.desc.xml
<webscript>
  <shortname>Delete item (deleteObject)</shortname>
  <description>
<![CDATA[
Deletes specified object<br>
<br>
Inputs:<br>
<br>
ID objectId<br>
<br>
Notes:<br>
<br>
If the object is a Folder with at least one child, throw ConstraintViolationException.<br>
If the object is the Root Folder, throw OperationNotSupportedException.<br>
When a filed object is deleted, it is removed from all folders it is filed in.<br>
This service deletes a specific version of a document object.  To delete all versions, use deleteAllVersions()<br>
Deletion of a private working copy (checked out version) is the same as to cancel checkout.<br>
]]>
  </description>
  <url>/api/node/{store_type}/{store_id}/{id}?includeChildren={includeChildren?}</url>
  <url>/api/path/{store_type}/{store_id}/{id}?includeChildren={includeChildren?}</url>
  <authentication>user</authentication>
  <format default="html"/>
  <family>CMIS</family>
</webscript>



and item.delete.js

script:
{
    // locate node
    var pathSegments = url.match.split("/");
    var reference = [ url.templateArgs.store_type, url.templateArgs.store_id ].concat(url.templateArgs.id.split("/"));
    var node = cmis.findNode(pathSegments[2], reference);
    if (node === null)
    {
        status.code = 404;
        status.message = "Repository " + pathSegments[2] + " " + reference.join("/") + " not found";
        status.redirect = true;
        break script;
    }

    if (!node.hasPermission("Delete"))
    {
        status.code = 403;
        status.message = "Permission to delete is denied";
        status.redirect = true;
        break script;
    }

    // if deleting a folder, only delete when the folder is empty or
    // a force delete has been specified
    // NOTE: force delete is not part of CMIS specification
    if (node.isFolder)
    {
       if (node.children.length > 0 && !args.includeChildren)
       {
          status.code = 403;
          status.message = "Cannot delete folder " + pathSegments[2] + " " + reference.join("/") + " as it's not empty";
          status.redirect = true;
          break script;
       }
    }

    // Note: checked-out documents are automatically unlocked if a private working copy is deleted
    if (!node.remove())
    {
        status.code = 500;
        status.message = "Failed to delete node " + pathSegments[2] + " " + reference.join("/");
        status.redirect = true;
        break script;
    }
   
    status.code = 204;  // Success, but no response content
    status.redirect = true;
}


Can u please let me know that how http DELETE comand is getting fired by URL
  http://localhost:8080/alfresco/service/api/node/workspace/SpacesStore/{id}?includeChildren={includeC...?}


Thanks in advance…

mrogers
Star Contributor
Star Contributor
If you are seeing a list of children then you are issuing a GET rather than a DELETE.

For example if you simply typing a URL into firefox.   Then you are doing GET.

What you need to use is some sort of HTTP client.  

Here's a popular one for Java Script.
[[http://en.wikipedia.org/wiki/XMLHttpRequest]]

And if you are using Java then take a look at classes like HttpURLConnection.

Also here's a link to the Alfresco wiki that may be relevant.
[[http://wiki.alfresco.com/wiki/Web_Scripts#Tunneling_HTTP_Methods]]

lakshya
Champ in-the-making
Champ in-the-making
Thanks a lot for your reply.

Now I am using HttpClient and hiiting the URL for GET and DELETE. I am getting the proper result.
But in case of POST i am getting error.

The method specified in the Request-Line is not allowed for the resource identified by the Request-URI.

I am trying to create folder / document by hitting the URL
"http://localhost:8080/alfresco/service/api/path/workspace/SpacesStore/Company Home/descendants/"

Do I need to pass method name like createFolder or createDocument in it. If yes, how?

How will I pass folder / document name that is to be created?

Can you explain it?

Thanks.

lakshya
Champ in-the-making
Champ in-the-making
Can anyone answer my question?

I am using PostMethod of apache common to create doc / folder in alfresco.

The code snippet is :
               String url = "http://localhost:8080/alfresco/service/api/path/workspace/SpacesStore/Company%20Home/descendants/";
      String creds = "admin" + ":" + "admin";
      HttpClient httpClient = new HttpClient();
      PostMethod method = new PostMethod(url);
      method.setRequestHeader("Authorization", "Basic "
            + new String(Base64.encodeBase64(creds.getBytes())));
      String filePath = "C:/temp/test.txt";
      String contentType = "text/plain";
      File upload = new File(filePath);
      method.setRequestHeader("name", upload.getName());
      method.setRequestHeader("Content-type", contentType);
      method.setRequestBody(new FileInputStream(upload));
      httpClient.executeMethod(method);
      System.out.println(method.getResponseBodyAsString());

But I am getting error :
Script url /api/path/workspace/SpacesStore/Company Home/descendants/ does not support the method POST

And when I Post URL = "http://localhost:8080/alfresco/service/api/path/workspace/SpacesStore/Company%20Home/descendants"; (without ending by /)

Can anyone tell me how to use REStClient for PostMethod in Alfresco.

Regards.

lakshya
Champ in-the-making
Champ in-the-making
Can anyone answer this question please . . . . .

I am stuck   :roll:

mrogers
Star Contributor
Star Contributor
I don't have the answer to hand but I do note that you are posting a content type of text/plain.

This script will require some sort of atom format rather than plain text.