cancel
Showing results for 
Search instead for 
Did you mean: 

Scripting : playing with sub space and import content

christophes
Champ in-the-making
Champ in-the-making
Dear,

I want to create a rule in a space A which move content, import in sub space B, into sub space defined by the date (as show in the schema below). The rule in A have to be inherited for others spaces created in A.


A
B
  Year
      Month
           Day
              <content import in B>
Actually I have a rule which create sub spaces and move imported content in. So, when I import a content in the space A, the rule moved the content in sub space defined by the date.

Example :
A
Year
     Month
          Day
             <content import in A>
The Javascript is following :
// Move document to YYYY/MM/DD structure
// First, find or create target folder

var current = document.properties["cm:created"];

var year = current.getFullYear();
var month = current.getMonth() + 1;
var day = current.getDate();

var yearSpace = space.childByNamePath(year);
if (yearSpace == null) {
   yearSpace = space.createFolder(year);
}
var monthSpace = yearSpace.childByNamePath(month);
if (monthSpace == null) {
    monthSpace = yearSpace.createFolder(month);
}
var daySpace = monthSpace.childByNamePath(day);
if (daySpace == null) {
   daySpace = monthSpace.createFolder(day);
}

// Then move document
document.move(daySpace);
The problem is how can I modify this code to create the sub spaces (Year, Month and Day) into the space of the import (B. It may have more than one space at this level) ?

Thanks in advance,

Christophe
2 REPLIES 2

kevinr
Star Contributor
Star Contributor
If i understand you correctly, the problem is that because you are using an inherited rule, therefore the 'space' object references folder 'A' not folder 'B' (the import folder). If so, then this should work:


// Move document to YYYY/MM/DD structure
// First, find or create target folder

var current = document.properties["cm:created"];

var year = current.getFullYear();
var month = current.getMonth() + 1;
var day = current.getDate();

var yearSpace = document.parent.childByNamePath(year);
if (yearSpace == null) {
   yearSpace = document.parent.createFolder(year);
}
var monthSpace = yearSpace.childByNamePath(month);
if (monthSpace == null) {
    monthSpace = yearSpace.createFolder(month);
}
var daySpace = monthSpace.childByNamePath(day);
if (daySpace == null) {
   daySpace = monthSpace.createFolder(day);
}

// Then move document
document.move(daySpace);

I have replaced 'space' with the 'document.parent' reference.

Hope this helps,

Kevin

christophes
Champ in-the-making
Champ in-the-making
It works.

Thanks a lot,

Christophe