I wasn't able to get Primary Parent Folder of document

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

I wasn't able to get Primary Parent Folder of document

Hi All,

 

I am referring ScriptNode.java and using document.getParent() in my program to get primary parent folder but not able to get. Could some one suggest what am i doing wrong with my code?. I am attaching piece of my code below:-

var parent = document.getParent();

Also my document is under this folder hierarchy structure:- Sites/lambda/documentLibrary/GroupDocuments/2021/07/12 . so basically its 4th folder. i want to get Group documents from this structure which is primary parent as per my understanding. 

 

Thanks and Regards,

Piyush 

3 Replies
abhinavmishra14
Advanced

Re: I wasn't able to get Primary Parent Folder of document

The method call you are using would always return you its parent which is "12" if document is under folder named "12". I am not sure if there is a direct way to get the folder name on the top of the hierarchy after documentLibrary. I can't recall a way at this moment. Hope others may read your question and throw some light on it.

However, you can do couple of these tricks : 

1- Get the document display path and siteshortname of the document. Using display path use string manipulation to get substring after documentLibrary. Build the parent folder path by taking first item from the splitted substring. You would have to use search by path.

e.g. Sites/<siteShortName>/documentLibrary/GroupDocuments/2021/07/12 --> Sites/lambda/documentLibrary/GroupDocuments/2021/07/12

Refer this code snippet and see if it can help. Note that its not tested.

var document = search.findNode("workspace://SpacesStore/1a0b110f-1e09-4ca2-b367-fe25e4964a4e"); //assuming you know nodeRef
var docDisplayPath = document.displayPath;

var siteShortName = document.getSiteShortName();
var paths = docDisplayPath.split("/");
logger.log("docDisplayPath: "+ docDisplayPath);

var trimKey = "documentLibrary";
var substringAfterDocLib = docDisplayPath.substring(docDisplayPath.indexOf(trimKey)+trimKey.length);
var parentFolderXpath = "/app:company_home/st:sites/cm:"+siteShortName+"/cm:documentLibrary/cm:"+search.ISO9075Encode(substringAfterDocLib.split("/")[1]);
logger.log("Final search xpath: "+ parentFolderXpath);

var searchRs = search.selectNodes("workspace://SpacesStore", parentFolderXpath);
var docLibNode = searchRs[0]; //select first node which is always document library
var doclibFirstChild = docLibNode.getChildren()[0]; // take always first node
if (doclibFirstChild.isContainer) {
  logger.log("Name:" +doclibFirstChild.name+" | nodeRef: "+doclibFirstChild.nodeRef + " | displayPath: "+doclibFirstChild.displayPath);
}

2- Get the display path (document.displayPath) of document and use string manipulation. [Shorter trick but you only get string value]

3-  Iterate from document node to parent node up until you find the node that is child of documentLibrary node. [Expensive]

~Abhinav
(ACSCE, AWS SAA-C02, GAIQ)
piyush48
Active Member II

Re: I wasn't able to get Primary Parent Folder of document

Hi @abhinavmishra14 ,

I tried to use repo root objects from documentation and is using space object which is giving me path of folder on which rule is applied. for eg  for my path:- Sites/lambda/documentLibrary/GroupDocuments/2021/07/12. Rule is applied on primary parent folder GroupDocuments. So it is moving my document to this folder for else case according to my code. Please refer below piece of code:-

var createdDate = document.properties["cm:created"];
var code = document.properties["lambda:labelCode"];
var date = new Date(createdDate);
var year = ""+date.getFullYear();
var month = ""+(date.getMonth()+1);
var day = ""+date.getDate();
var hour = ""+date.getHours();
var min = ""+date.getMinutes();
var calender = [year,month,hour];
if(document.isSubType("cm:content")){
	var parent = document.parent;
	if (code === "101") {
		var parent = parent.childByNamePath("GroupAlpha");
		var child = createFolderHierarchy(parent);
		
	}
	else if (code === "102") {
		var parent = parent.childByNamePath("GroupBeta");
		var child = createFolderHierarchy(parent);
	}
	else if (code === "103") {
		var parent = parent.childByNamePath("GroupGamma");
		var child = createFolderHierarchy(parent);
	}
	else if (code === "104") {
		var parent = parent.childByNamePath("GroupEmployee");
		var child = createFolderHierarchy(parent);
	}
	else {
		if (document.parent !== parent) {
			var child = parent;
		}
	}
	document.move(child);
}
function createFolderHierarchy(parent) {
	for(var i =0; i<calender.length;i++) {
			var myfolder = parent.childByNamePath(calender[i]);
			if (myfolder === null) {
				myfolder = parent.createFolder(calender[i]);
			}
			 parent = myfolder;
		}
	return myfolder;
}
	

It works fine for all if conditions if document is created on path: Sites/lambda/documentLibrary/GroupDocuments and it moves documents to respective subfolder but for else statement where i create document on path:- Sites/lambda/documentLibrary/GroupDocuments/GroupEmployee/2021/07/12 . I have to run rule each and every time for document to get move to this path:- Sites/lambda/documentLibrary/GroupDocuments as per logic. Am i doing something wrong with code? as logic is correct but for else statement part i have to run rule again and again. Also my run rules is background is applied with rules applied for sub folder. Please do guide me out.

Thanks,

Piyush

piyush48
Active Member II

Re: I wasn't able to get Primary Parent Folder of document

Hi All,

Any help on above approach, it works but everytime rule has to be run. Any issue with above approach related to code or understanding?. 

Please do help out.

 

Thanks and Regards,

Piyush