cancel
Showing results for 
Search instead for 
Did you mean: 

cmis deleteTree in web script

silenr0c
Champ in-the-making
Champ in-the-making
Hi Folks,

I try to delete folder using cmis.deleteTree in webscript, following is js code, but I get error below.
Not sure because deleteTree is non existant function or there are some wrong parameter in it.
I'm not so sure UnfileObject.DELETE is valid.

p.s. I notice there are some api, but it stated as deprecated in lifecycle so I didn't bother to try it.
<blockquote>
Authentication:   none
Transaction:   none
Format Style:   any
Default Format:   text
Lifecycle:   deprecated
Id:   org/alfresco/cmis/test.post
Descriptor:   classpath:alfresco/templates/webscripts/org/alfresco/cmis/test.post.desc.xml

Delete Folder Tree (deleteTree)
DELETE /alfresco/service/cmis/i/{id}/tree?continueOnFailure={continueOnFailure?}&unfileObjects={unfileObjects}
DELETE /alfresco/service/cmis/s/{store}/i/{id}/tree?continueOnFailure={continueOnFailure?}&unfileObjects={unfileObjects}
DELETE /alfresco/service/cmis/p{path}/tree?continueOnFailure={continueOnFailure?}&unfileObjects={unfileObjects}
DELETE /alfresco/service/cmis/s/{store}/p{path}/tree?continueOnFailure={continueOnFailure?}&unfileObjects={unfileObjects}
DELETE /alfresco/service/api/node/{store_type}/{store_id}/{id}/tree?continueOnFailure={continueOnFailure?}&unfileObjects={unfileObjects}
DELETE /alfresco/service/api/path/{store_type}/{store_id}/{nodepath}/tree?continueOnFailure={continueOnFailure?}&unfileObjects={unfileObjects}

Deletes the specified folder and all of its child and descendant objects.

</blockquote>

Thanks for your help & kind attention
- silenr0c -

Additional Info:
- Alfresco Edition : Community v4.2.0 (4576) schema 6,022


// get the sample user connection
var connectionId = "cmis-sample-connection"
var cmisConnection = cmis.getConnection(connectionId)
if (cmisConnection == null) {
   // if no connection exists, talk to the local server
   cmisConnection = cmis.getConnection()
}

var cmisSession = cmisConnection.getSession()

// get arguments
var path = args["path"]

if (path != null )
{
  // get path folder
  var parentFolder = cmisSession.getObjectByPath(path)

  //delete folder
  parentFolder.deleteFolder(true, UnfileObject.DELETE, true);
}


<blockquote>The Web Script /alfresco/service/cmissamples/deletefolder has responded with a status of 500 - Internal Error.

500 Description:    An error inside the HTTP server which prevented it from fulfilling the request.

Message:   07020044 Wrapped Exception (with status template): 070216449 Failed to execute script '/cmis/cmisdeletefolder.get.js (in repository store workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts)': 070216448 TypeError: Cannot find function deleteFolder. (workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts/cmis/cmisdeletefolder.get.js#27)

Exception:   org.mozilla.javascript.EcmaError - TypeError: Cannot find function deleteFolder. (workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts/cmis/cmisdeletefolder.get.js#27)

org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3350)
org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3340)
org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3356)
org.mozilla.javascript.ScriptRuntime.typeError1(ScriptRuntime.java:3368)
org.mozilla.javascript.ScriptRuntime.notFunctionError(ScriptRuntime.java:3428)
org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(ScriptRuntime.java:2052)
org.mozilla.javascript.gen.c15._c0(workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts/cmis/cmisdeletefolder.get.js:27)
org.mozilla.javascript.gen.c15.call(workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts/cmis/cmisdeletefolder.get.js)
org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:393)
org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2834)
org.mozilla.javascript.gen.c15.call(workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts/cmis/cmisdeletefolder.get.js)
org.mozilla.javascript.gen.c15.exec(workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts/cmis/cmisdeletefolder.get.js)
org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:492)
org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:198)
org.alfresco.repo.processor.ScriptServiceImpl.execute(ScriptServiceImpl.java:212)
org.alfresco.repo.processor.ScriptServiceImpl.executeScript(ScriptServiceImpl.java:174)
org.alfresco.repo.web.scripts.RepositoryScriptProcessor.executeScript(RepositoryScriptProcessor.java:102)
org.springframework.extensions.webscripts.AbstractWebScript.executeScript(AbstractWebScript.java:1305)
org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:86)
org.alfresco.repo.web.scripts.RepositoryContainer$3.execute(RepositoryContainer.java:433)
org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:433)
org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:495)
org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:533)
org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:349)
org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:377)
org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:209)
org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:118)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:61)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:1813)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
java.lang.Thread.run(Thread.java:722)

Exception:   org.alfresco.error.AlfrescoRuntimeException - 070216448 TypeError: Cannot find function deleteFolder. (workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts/cmis/cmisdeletefolder.get.js#27)

org.alfresco.repo.jscript.RhinoScriptProcessor.executeScriptImpl(RhinoScriptProcessor.java:516)

Exception:   org.alfresco.scripts.ScriptException - 070216449 Failed to execute script '/cmis/cmisdeletefolder.get.js (in repository store workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts)': 070216448 TypeError: Cannot find function deleteFolder. (workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts/cmis/cmisdeletefolder.get.js#27)

org.alfresco.repo.jscript.RhinoScriptProcessor.execute(RhinoScriptProcessor.java:202)

Exception:   org.springframework.extensions.webscripts.WebScriptException - 07020044 Wrapped Exception (with status template): 070216449 Failed to execute script '/cmis/cmisdeletefolder.get.js (in repository store workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts)': 070216448 TypeError: Cannot find function deleteFolder. (workspace://SpacesStore/Company Home/Data Dictionary/Web Scripts/cmis/cmisdeletefolder.get.js#27)

org.springframework.extensions.webscripts.AbstractWebScript.createStatusException(AbstractWebScript.java:1067)

Server:   Community v4.2.0 (4576) schema 6,022
</blockquote>
2 REPLIES 2

kaynezhang
World-Class Innovator
World-Class Innovator
alfresco have already provide cmis web sript to delete the specified folder and all of its child and descendant objects.

you can user either
alfresco\WEB-INF\classes\alfresco\templates\webscripts\org\alfresco\cmis\tree.delete.desc.xml(this one is deprecated )
or
alfresco\WEB-INF\classes\alfresco\templates\webscripts\org\alfresco\cmis\descendants.delete.desc.xml

Below are their definitions
<webscript kind="org.alfresco.cmiskind">
  <shortname>Delete Folder Tree (deleteTree)</shortname>
  <description>
  <![CDATA[
  Deletes the specified folder and all of its child and descendant objects.
  ]]>
  </description>
 
  <!– by object id –>
  <url>/cmis/i/{id}/tree?continueOnFailure={continueOnFailure?}&amp;unfileObjects={unfileObjects}</url>
  <url>/cmis/s/{store}/i/{id}/tree?continueOnFailure={continueOnFailure?}&amp;unfileObjects={unfileObjects}</url>
  <!– by path –>
  <url>/cmis/p{path}/tree?continueOnFailure={continueOnFailure?}&amp;unfileObjects={unfileObjects}</url>
  <url>/cmis/s/{store}/p{path}/tree?continueOnFailure={continueOnFailure?}&amp;unfileObjects={unfileObjects}</url>
  <!– alfresco style –>
  <url>/api/node/{store_type}/{store_id}/{id}/tree?continueOnFailure={continueOnFailure?}&amp;unfileObjects={unfileObjects}</url>
  <url>/api/path/{store_type}/{store_id}/{nodepath}/tree?continueOnFailure={continueOnFailure?}&amp;unfileObjects={unfileObjects}</url>
  <args>
    <arg>
        <shortname>store</shortname>
        <description>the store name</description>
    </arg>
    <arg>
        <shortname>id</shortname>
        <description>the node id of the folder</description>
    </arg>
    <arg>
        <shortname>path</shortname>
        <description>the path of the object (relative to CMIS root, typically "Company Home")</description>
    </arg>
    <arg>
        <shortname>nodepath</shortname>
        <description>the path of the object (relative to root of Alfresco store)</description>
    </arg>
    <arg>
        <shortname>continueOnFailure</shortname>
        <description>if true, continue attempting to perform this operation, even if deletion of child or descendant fails</description>
        <default>false</default>
    </arg>
    <arg>
        <shortname>unfileObjects</shortname>
        <description>one of unfile (unfile all fileable objects), deletesinglefiled, delete (delete all fileable objects)</description>
        <default>delete</default>
    </arg>
  </args>

  <format default="atomfeed"/>
 
  <authentication>user</authentication>
  <family>CMIS</family>
  <lifecycle>deprecated</lifecycle>
</webscript>




or
alfresco\WEB-INF\classes\alfresco\templates\webscripts\org\alfresco\cmis\descendants.delete.desc.xml
<webscript kind="org.alfresco.cmiskind">
  <shortname>Delete Folder Tree (deleteTree)</shortname>
  <description>
  <![CDATA[
  Deletes the specified folder and all of its child and descendant objects.
  ]]>
  </description>
  <!– by object id –>
  <url>/cmis/i/{id}/descendants?continueOnFailure={continueOnFailure?}&amp;unfileObjects={unfileObjects}</url>
  <url>/cmis/s/{store}/i/{id}/descendants?continueOnFailure={continueOnFailure?}&amp;unfileObjects={unfileObjects}</url>
  <!– by path –>
  <url>/cmis/p{path}/descendants?continueOnFailure={continueOnFailure?}&amp;unfileObjects={unfileObjects}</url>
  <url>/cmis/s/{store}/p{path}/descendants?continueOnFailure={continueOnFailure?}&amp;unfileObjects={unfileObjects}</url>
  <!– alfresco style –>
  <url>/api/node/{store_type}/{store_id}/{id}/descendants?continueOnFailure={continueOnFailure?}&amp;unfileObjects={unfileObjects}</url>
  <url>/api/path/{store_type}/{store_id}/{nodepath}/descendants?continueOnFailure={continueOnFailure?}&amp;unfileObjects={unfileObjects}</url>
 
  <args>
    <arg>
        <shortname>store</shortname>
        <description>the store name</description>
    </arg>
    <arg>
        <shortname>id</shortname>
        <description>the node id of the folder</description>
    </arg>
    <arg>
        <shortname>path</shortname>
        <description>the path of the object (relative to CMIS root, typically "Company Home")</description>
    </arg>
    <arg>
        <shortname>nodepath</shortname>
        <description>the path of the object (relative to root of Alfresco store)</description>
    </arg>
    <arg>
        <shortname>continueOnFailure</shortname>
        <description>if true, continue attempting to perform this operation, even if deletion of child or descendant fails</description>
        <default>false</default>
    </arg>
    <arg>
        <shortname>unfileObjects</shortname>
        <description>one of unfile (unfile all fileable objects), deletesinglefiled, delete (delete all fileable objects)</description>
        <default>delete</default>
    </arg>
  </args>
 
  <format default="atomfeed"/>

  <authentication>user</authentication>
  <family>CMIS</family>
  <lifecycle>deprecated</lifecycle>
</webscript>

silenr0c
Champ in-the-making
Champ in-the-making
Hi Kaynezhang,

Thanks for your reply, I manage to try out with asp.net
Following is someone else's code I found in the internet, I just make little changes.


//Delete Folder
string endpointAddress = @"http://localhost:8080/alfresco/service/cmis/p/User Homes/Folder to be deleted/tree";
GetWebScriptResponse("DELETE", string.Empty, endpointAddress, "admin", "admin");

public static string GetWebScriptResponse(string method, string postData, string endpointAddress, string username, string password)
{
    string _auth = string.Format("{0}:{1}", username, password);
    string _enc = Convert.ToBase64String(Encoding.ASCII.GetBytes(_auth));
    string _cred = string.Format("{0} {1}", "Basic", _enc);

    /* WebProxy proxyObject = new WebProxy("http://youralfrescowebsite:8080/',true);*/
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(endpointAddress);
    /* req.Proxy = proxyObject;*/

    req.Timeout = 20 * 60 * 1000;
    req.ReadWriteTimeout = 20 * 60 * 1000;
    req.Method = method;
    if (postData.Length > 0)
    {
        req.ContentType = "application/json;charset=UTF-8";
        req.ContentLength = postData.Length;
    }
    req.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired;
    NetworkCredential nc = new NetworkCredential(username, password);
    req.Credentials = nc;
    req.Headers[HttpRequestHeader.Authorization] = _cred;
    string response = null;

    Stream dataStream = null;
    try
    {
        if (POST.Equals(method) || PUT.Equals(method))
        {
            using (StreamWriter sw = new StreamWriter(req.GetRequestStream(),
            System.Text.Encoding.ASCII))
            {
                sw.Write(postData);
                sw.Flush();
            }
        }
        using (StreamReader sr = new StreamReader(req.GetResponse().GetResponseStream()))
        {
            response = sr.ReadToEnd();
        }
        req.GetResponse().Close();

    }
    catch (Exception e)
    {               
        response = "Exception: " + e.Message + "; " + e.ToString();
    }

    return response;
}