I've tried this and it doesn't repeat since you put the fix id to the related_id's Item. It always do one level I think.
Here is a sample for recursive search for forward and backward.
////////////////////////////////////////////////////////////////////
/// - IN:
/// - RequestState
/// - [String] initIDs: initial IDs (csv)
/// - Properties
/// - [String] direction: forward/backward
/// - [String] itemtype: itemtype's name of RELATIONSHIP
/// - [String] is_all_level: return all levels or only the top/end
/// - 0(default): top/end only
/// - 1: all levels (all children or all parents)
/// - OUT:
/// - RequestState
/// - [String] returnIDs: target IDs (csv)
/// - newResult()
/// - 0 : normal
/// - E003: Bad Args
/// - E004: No target Items
///
////////////////////////////////////////////////////////////////////
Innovator inn = this.getInnovator();
List<string> targetList;
string direction, itemtype;
try
{
string temp = (string)RequestState["initIDs"];
targetList = temp.Split(',').ToList();
RequestState.Remove("initIDs");
}
catch (Exception e)
{
return inn.newResult("E004");
}
try
{
direction = this.getProperty("direction");
itemtype = this.getProperty("itemtype");
if (direction != "forward" && direction != "backward")
{
throw new Exception("Unvalid parameter.");
}
}
catch (Exception e)
{
return inn.newResult("E003");
}
var relatedList = new List<string>();
var libraryList = new List<string>();
try
{
while (targetList.Count != 0)
{
for (int i = targetList.Count - 1; i >= 0; i--)
{
if (libraryList.Contains(targetList[i]) == false)
{
Item sourceItem = inn.newItem(itemtype, "get");
sourceItem.setAttribute("select", direction == "forward" ? "related_id" : "source_id");
sourceItem.setProperty(direction == "forward" ? "source_id" : "related_id", targetList[i]);
Item rtItem = sourceItem.apply();
int rtCount = rtItem.getItemCount();
if (rtCount == 0)
{
relatedList.Add(targetList[i]);
}
else
{
for (int j = 0; j < rtCount; j++)
{
Item resultItem = rtItem.getItemByIndex(j);
targetList.Add(resultItem.getProperty(direction == "forward" ? "related_id" : "source_id"));
}
}
libraryList.Add(targetList[i]);
}
targetList.RemoveAt(i);
}
}
}
catch (Exception e)
{
// unknown Relationship itemtype
return inn.newResult("E003");
}
if (this.getProperty("is_all_level") == "1")
{
RequestState.Add("returnIDs", String.Join(",", libraryList));
}
else
{
RequestState.Add("returnIDs", String.Join(",", relatedList));
}
return inn.newResult("0");Copyright © 2025 Aras. All rights reserved.