cancel
Showing results for 
Search instead for 
Did you mean: 

Webscript to create folder / space

mwaldrop
Champ in-the-making
Champ in-the-making
I'm pretty new to web scripts, but I've played with some of the samples and am starting to get the hang of it.  What I really need is a web script that will create a folder within a specified space.  Does anybody have or know of an example web script that will accomplish this ?
10 REPLIES 10

mikeh
Star Contributor
Star Contributor
Have a look at docActions.get.js in the Office add-in web scripts.

Towards the bottom third of the javascript is the code to create a new space, optionally from a specified template.

Thanks,
Mike

mwaldrop
Champ in-the-making
Champ in-the-making
OK, this worked great.  One last question.  This script uses:

var nodeParent = search.findNode("workspace://SpacesStore/" + nodeId);

to determine where to create the folder.  This assumes I'm passing in the nodeId to the script.  I can make this work by looking up the nodeId and passing it in, but the application that I'm calling this web script from will not know that id.  So I need to figure out a way to either:
a) Determine the nodeId of the target space within the web script
b) Come up with another type of search that uses a more logical path string

I tried referencing the objects roothome.findChildByPath(""); but roothome doesn't appear to be available within the script [alfresco community v2.1.0].  Browsing through forums makes me think this may be a bug.

It seems like the findNode might also work with a different type of string path, but I can't seem to figure out what this path would need to look like.

Thanks for any help.

Any thoughts ?

mikeh
Star Contributor
Star Contributor
It's not clear from your post what exactly you *do* have, but I'm assuming it's a path looking something like:
/Company Home/User Homes/mwaldrop/My Spaces

Then you can use:
var nodeParent = companyhome.childByNamePath("/Company Home/… etc.");

Thanks,
Mike

mwaldrop
Champ in-the-making
Champ in-the-making
Then you can use:
var nodeParent = companyhome.childByNamePath("/Company Home/… etc.");

Yeah, I tried that.  What get is a result is an error message stating that companyhome is undefined.
org.mozilla.javascript.EcmaError - ReferenceError: "companyhome" is not defined. (AlfrescoScript#27)

I'm ready to believe that I'm missing something stupid.  Here's my entire script:

// Client has requested server-side action

/* Inputs */


/* Outputs */
var resultString = "Action failed",
   resultCode = false;



         resultString = "Could not create space";
         var nodeId = args.n,
            spaceName = args.sn,
            spaceTitle = (args.st == "undefined") ? "" : args.st,
            spaceDescription = (args.sd == "undefined") ? "" : args.sd,
            templateId = args.t;
         var nodeNew;
        
         if ((spaceName == null) || (spaceName == ""))
         {
            resultString = "Space must have a Name";
         }
         else
         {
            
            var nodeParent = companyhome.childByNamePath("/Company Home/User Homes/teamworks");
            // var nodeParent = search.findNode("workspace://SpacesStore/" + nodeId);
            // Copy from template?
            if ((templateId != null) && (templateId != ""))
            {
               nodeTemplate = search.findNode("workspace://SpacesStore/" + templateId);
               nodeNew = nodeTemplate.copy(nodeParent, true);
               nodeNew.name = spaceName;
            }
            else
            {
               nodeNew = nodeParent.createFolder(spaceName);
            }
            // Always add title & description, default icon
            nodeNew.properties["cm:title"] = spaceTitle;
            nodeNew.properties["cm:description"] = spaceDescription;
            nodeNew.properties["app:icon"] = "space-icon-default";
            nodeNew.save();
            // Add uifacets aspect for the web client
            nodeNew.addAspect("app:uifacets");
            if (nodeNew != null)
            {
               resultString = "New space created";
               resultCode = true;
            }
         }
     
  
model.resultString = resultString;
model.resultCode = resultCode;

Thanks for the help !

davidc
Star Contributor
Star Contributor
companyhome is not defined if

1) the executing user does not have read permission to company home

or

2) you're not authenticated when executing the script

mwaldrop
Champ in-the-making
Champ in-the-making
It would seem that my Company Home space is readable by everyone.  When I look at the details it has group EVERYONE as a "Consumer".  When I execute the web script via URL, I authenticate as a user.  I've even tried to authenticate as admin (via HTTP authentication).

Do I need to create the authentication ticket ?  I'm not sure how the context of the ticket works via the web API.

davidc
Star Contributor
Star Contributor
Can you post the full implementation of your web script (.desc.xml, .js and .ftl files).

Also, which version are you testing against?  You mention community 2.1.

How are you calling your web script?

mwaldrop
Champ in-the-making
Champ in-the-making
Thanks for the help.  I've noticed other scripts on this install seem to work with roothome, so it must be something in my implementation of this script.

createSpace.get.desc.xml
<webscript>
  <shortname>Create Space</shortname>
  <description>Create a space to store info from Teamworks</description>
  <url>/lombardi/createSpace?n={nodeId}&amp;sn={spaceName}&amp;st={spaceTitle?}&amp;sd={spaceDescription?}&amp;t={templateId?}</url>
  <authentication>user</authentication>
  <transaction>none</transaction>
</webscript>

createSpace.get.html.ftl
{
   "statusString":"${resultString}",
   "statusCode":${resultCode?string}
}

createSpace.get.js

// Client has requested server-side action

/* Inputs */


/* Outputs */
var resultString = "Action failed",
   resultCode = false;



         resultString = "Could not create space";
         var nodeId = args.n,
            spaceName = args.sn,
            spaceTitle = (args.st == "undefined") ? "" : args.st,
            spaceDescription = (args.sd == "undefined") ? "" : args.sd,
            templateId = args.t;
         var nodeNew;
        
         if ((spaceName == null) || (spaceName == ""))
         {
            resultString = "Space must have a Name";
         }
         else
         {
            
            var nodeParent = roothome.childByNamePath("/Company Home/User Homes/teamworks" + nodeId);
            //var nodeParent = search.findNode("workspace://SpacesStore/" + nodeId);
            // Copy from template?
            if ((templateId != null) && (templateId != ""))
            {
               nodeTemplate = search.findNode("workspace://SpacesStore/" + templateId);
               nodeNew = nodeTemplate.copy(nodeParent, true);
               nodeNew.name = spaceName;
            }
            else
            {
               nodeNew = nodeParent.createFolder(spaceName);
            }
            // Always add title & description, default icon
            nodeNew.properties["cm:title"] = spaceTitle;
            nodeNew.properties["cm:description"] = spaceDescription;
            nodeNew.properties["app:icon"] = "space-icon-default";
            nodeNew.save();
            // Add uifacets aspect for the web client
            nodeNew.addAspect("app:uifacets");
            if (nodeNew != null)
            {
               resultString = "New space created";
               resultCode = true;
            }
         }
     
  
model.resultString = resultString;
model.resultCode = resultCode;

davidc
Star Contributor
Star Contributor
remove the <transaction> element from your web script description file.  The value 'none' is forcing the web script f/w not to create a transaction,  therefore none of the repository objects can be read.