Automatic Folder creation

cancel
Showing results for 
Search instead for 
Did you mean: 
CRISTIAN_MULLER
Member II

Automatic Folder creation

Hello guys, I have a folder rule that runs when a folder are created at a specific place, the problem is, i have to search if a folder exists , when exists i have to create some subfolders, the problem is, whem the first folder dont exists, a folder are created and subfolders too, but when the folders exists, another folder are created with the same name the original and the subfolders are created inside this new folder.

My question is, how can I search for a folder and create contetn inside of it without creating a new folder.

 

thats my script

 

var ATESTADOS = document.childByNamePath("ATESTADOS MÉDICOS");
if ( ATESTADOS == null && space.hasPermission("CreateChildren"))

{
// create ATESTADOS MÉDICOS
ATESTADOS = document.createFolder("ATESTADOS MÉDICOS");
var parentNode = ATESTADOS.parent;
parentNode.setInheritsPermissions(false);
parentNode.setPermission("Coordinator", "GROUP_RHF");

ATESTADOS.setInheritsPermissions(false);
ATESTADOS.setPermission("Coordinator", "GROUP_RHF");


var caminho = document.name;

var First_Name= caminho.match(/^(\S+)\s(.*)/).slice(1,2)
var Last_Name= caminho.match(/^(\S+)\s(.*)/).slice(2,3)


var query = search.query({query:"@cm\\:firstName:\"'"+First_Name+" '\" AND @CM\\:lastName:\"'"+ Last_Name+"'\""});

var nome= query[0].properties.userName;
parentNode.setPermission("Consumer", nome);
ATESTADOS.setPermission("Consumer", nome);


}

var BENEFÍCIOS = document.childByNamePath("BENEFÍCIOS");

if ( BENEFÍCIOS == null && space.hasPermission("CreateChildren"))
{
// create BENEFÍCIOS with some subfolders
BENEFÍCIOS = document.createFolder("BENEFÍCIOS");
BENEFÍCIOS.setInheritsPermissions(false);
BENEFÍCIOS.setPermission("Coordinator", "GROUP_RHF");
BENEFÍCIOS.setPermission("Consumer", nome);

var TICKET = document.childByNamePath("BENEFÍCIOS/TICKET");
TICKET = BENEFÍCIOS.createFolder("TICKET");
var VALE_TRANSPORTE = document.childByNamePath("BENEFÍCIOS/VALE TRANSPORTE");
VALE_TRANSPORTE = BENEFÍCIOS.createFolder("VALE TRANSPORTE");
var PLANO = document.childByNamePath("BENEFÍCIOS/PLANO DE SAÚDE");
PLANO = BENEFÍCIOS.createFolder("PLANO DE SAÚDE");
var PLANO_ODONTO = document.childByNamePath("BENEFÍCIOS/PLANO ODONTOLÓGICO");
PLANO_ODONTO = BENEFÍCIOS.createFolder("PLANO ODONTOLÓGICO");
}




var CERTIFICADOS = document.childByNamePath("CERTIFICADOS");
if ( CERTIFICADOS == null && space.hasPermission("CreateChildren"))
{
// create Documentazione with some subfolders
CERTIFICADOS = document.createFolder("CERTIFICADOS");

}

CERTIFICADOS.setInheritsPermissions(false);
CERTIFICADOS.setPermission("Coordinator", "GROUP_RHF");
CERTIFICADOS.setPermission("Consumer", nome);


var CONTRACHEQUE = document.childByNamePath("CONTRACHEQUE");
if ( CONTRACHEQUE== null && space.hasPermission("CreateChildren"))
{
// create Documentazione with some subfolders
CONTRACHEQUE = document.createFolder("CONTRACHEQUE");

}
CONTRACHEQUE.setInheritsPermissions(false);
CONTRACHEQUE.setPermission("Coordinator", "GROUP_RHF");
CONTRACHEQUE.setPermission("Consumer", nome);


var DOCUMENTOS_INTERNOS = document.childByNamePath("DOCUMENTOS INTERNOS");
if ( DOCUMENTOS_INTERNOS == null && space.hasPermission("CreateChildren"))


{
// create DOCUMENTOS INTERNOS with some subfolders
DOCUMENTOS_INTERNOS = document.createFolder("DOCUMENTOS INTERNOS");

var PENALIDADES = document.childByNamePath("DOCUMENTOS INTERNOS/PENALIDADES");
PENALIDADES = DOCUMENTOS_INTERNOS.createFolder("PENALIDADES");
var DIVERSOS = document.childByNamePath("DOCUMENTOS INTERNOS/DIVERSOS");
DIVERSOS = DOCUMENTOS_INTERNOS.createFolder("DIVERSOS");
var TESTES_E_AVALIACOES_DE_ADMISSAO = document.childByNamePath("DOCUMENTOS INTERNOS/TESTES E AVALIACÕES DE ADMISSÃO");
TESTES_E_AVALIACOES_DE_ADMISSAO = DOCUMENTOS_INTERNOS.createFolder("TESTES E AVALIACÕES DE ADMISSÃO");
}

else

{
var DOCUMENTOS_INTERNOS = document.childByNamePath("DOCUMENTOS INTERNOS");


//var PENALIDADES = document.childByNamePath("DOCUMENTOS INTERNOS/PENALIDADES");
PENALIDADES = DOCUMENTOS_INTERNOS.createFolder("PENALIDADES");
//var DIVERSOS = document.childByNamePath("DOCUMENTOS INTERNOS/DIVERSOS");
DIVERSOS = DOCUMENTOS_INTERNOS.createFolder("DIVERSOS");
//var TESTES_E_AVALIACOES_DE_ADMISSAO = document.childByNamePath("DOCUMENTOS INTERNOS/TESTES E AVALIACÕES DE ADMISSÃO");
TESTES_E_AVALIACOES_DE_ADMISSAO = DOCUMENTOS_INTERNOS.createFolder("TESTES E AVALIACÕES DE ADMISSÃO");
}

DOCUMENTOS_INTERNOS.setInheritsPermissions(false);
DOCUMENTOS_INTERNOS.setPermission("Coordinator", "GROUP_RHF");
DOCUMENTOS_INTERNOS.setPermission("Consumer", nome);


var DOCUMENTOS_PESSOAIS = document.childByNamePath("DOCUMENTOS PESSOAIS");
if ( DOCUMENTOS_PESSOAIS == null && space.hasPermission("CreateChildren"))
{
// create DOCUMENTOS PESSOAIS with some subfolders
DOCUMENTOS_PESSOAIS = document.createFolder("DOCUMENTOS PESSOAIS");


var DOCUMENTOS_DEPENDENTES = document.childByNamePath("DOCUMENTOS PESSOAIS/DOCUMENTOS DOS DEPENDENTES");
DOCUMENTOS_DEPENDENTES = DOCUMENTOS_PESSOAIS.createFolder("DOCUMENTOS DOS DEPENDENTES");
var DOCUMENTOS_DO_CONJUGE = document.childByNamePath("DOCUMENTOS PESSOAIS/DOCUMENTOS DO CÔNJUGE");
DOCUMENTOS_DO_CONJUGE = DOCUMENTOS_PESSOAIS.createFolder("DOCUMENTOS DO CÔNJUGE");
}

else {
var DOCUMENTOS_DEPENDENTES = document.childByNamePath("DOCUMENTOS PESSOAIS/DOCUMENTOS DOS DEPENDENTES");
DOCUMENTOS_DEPENDENTES = DOCUMENTOS_PESSOAIS.createFolder("DOCUMENTOS DOS DEPENDENTES");
var DOCUMENTOS_DO_CONJUGE = document.childByNamePath("DOCUMENTOS PESSOAIS/DOCUMENTOS DO CÔNJUGE");
DOCUMENTOS_DO_CONJUGE = DOCUMENTOS_PESSOAIS.createFolder("DOCUMENTOS DO CÔNJUGE");
}

DOCUMENTOS_PESSOAIS.setInheritsPermissions(false);
DOCUMENTOS_PESSOAIS.setPermission("Coordinator", "GROUP_RHF");
DOCUMENTOS_PESSOAIS.setPermission("Consumer", nome);


var ESPELHO = document.childByNamePath("ESPELHO DE PONTO");
if ( ESPELHO== null && space.hasPermission("CreateChildren"))
{
// create Documentazione with some subfolders
ESPELHO = document.createFolder("ESPELHO DE PONTO");

}

ESPELHO.setInheritsPermissions(false);
ESPELHO.setPermission("Coordinator", "GROUP_RHF");
ESPELHO.setPermission("Consumer", nome);

var FERIAS = document.childByNamePath("FÉRIAS");
if ( FERIAS== null && space.hasPermission("CreateChildren"))
{
// create Documentazione with some subfolders
FERIAS = document.createFolder("FÉRIAS");

}
FERIAS.setInheritsPermissions(false);
FERIAS.setPermission("Coordinator", "GROUP_RHF");
FERIAS.setPermission("Consumer", nome);

2 Replies
abhinavmishra14
Advanced

Re: Automatic Folder creation

If i understood the question, you want to upload content to a path. You can make use of rest apis, see a solution here: https://hub.alfresco.com/t5/alfresco-content-services-forum/i-want-to-add-a-document-in-a-subdirecto...

 

Or With help of root folder you can look for existance of subfolder and if doesn't exist you can create them. For example 

you have a root folder Asset in document library and you want to create content in Asset/docs/test, then using the webscript/script/ you can search for the subfolder using tmdq search api and then create the path and upload the content. 

Here is an example in java for creating folders based on path;

https://hub.alfresco.com/t5/alfresco-content-services-forum/how-to-create-directory-structure-using-...

 

~Abhinav
(ACSCE, AWS SAA, Azure Admin)
CRISTIAN_MULLER
Member II

Re: Automatic Folder creation

Thanks for the answer, i have this scrit that runs wherever any folder are created in this workspace, the script runs automatically trough folder rules and works fine, except when the destination folder already exists, I give a look at source code of the page itself and notice that even that apears to have 2 folders with the same name (impossible with alfresco rules), turns out that the source code for the folder tiltle has a '&nbsp' caracter in spaces, so when my script compares the two folders name "ATESTADOS MÉDICOS" with "ATESTADOS&nbspMÉDICOS", visually they are the same because html puts a space at '&nbsp' location, when my script compares they are not the same.

Now I Have two change all the  '&nbsp' im my folders name.

Someone has an idea how to acomplish that?.