Working with Families |
Documents in ReadySuite can be organized into families — a parent document with one or more child attachments. This topic covers querying family relationships, building new families, and restructuring existing ones.
A family consists of a parentDocumentItem and its attachments (children). Every document exposes properties and methods for navigating these relationships:
| Method / Property | Description |
|---|---|
| HasFamily() | Returns true if the document belongs to a family |
| IsParent() | Returns true if the document is a parent (has or can have attachments) |
| IsRootParent() | Returns true if the document is the top-level parent |
| IsAttach() | Returns true if the document is an attachment (child) |
| HasAttach() | Returns true if the document has at least one attachment |
| Parent | Gets or sets the parent document reference |
| GetAttachments() | Returns the list of child documents |
while (script.ReadDocument()) { var document = script.Document; if (document.IsParent() && document.HasAttach()) { var attachments = document.GetAttachments(); script.AddMessage(string.Format("{0} has {1} attachments", document.DocId, attachments.Count)); } }
To process a parent and all its children together:
while (script.ReadDocument()) { var document = script.Document; if (!document.IsParent()) continue; // Process the parent ProcessDocument(document); if (document.HasAttach()) { foreach (var child in document.GetAttachments()) { ProcessDocument(child); } } }
To attach a new document as a child of an existing parent:
var parent = script.Document; var child = script.GetNewDocument("ATTACH-0001"); child.SetNative(child.GetNewNative(filePath)); script.CreateDocument(child); parent.AddAttachment(child); script.UpdateDocument(parent);
AddAttachment() sets the child's Parent property automatically.
ClearAttachments() removes all child relationships from a parent:
document.ClearAttachments(); script.UpdateDocument(document);
The forceUpdateParent parameter (default true) controls whether the parent reference is updated immediately.
You can move a document to a different family by setting its Parent property:
var child = script.Document;
child.Parent = newParent;
script.UpdateDocument(child);This example finds standalone documents (no family) and groups them under a new parent based on a shared key field:
public override void Run(IScriptContext script) { base.Run(script); // First pass: group standalone documents by key var groups = new Dictionary<string, List<DocumentItem>>(); var documents = script.GetDocuments(); foreach (var doc in documents) { if (doc.HasFamily()) continue; var key = doc.GetFieldData(options.GroupField); if (string.IsNullOrEmpty(key)) continue; if (!groups.ContainsKey(key)) groups[key] = new List<DocumentItem>(); groups[key].Add(doc); } // Second pass: create families foreach (var group in groups) { if (group.Value.Count < 2) continue; var parent = group.Value[0]; for (int i = 1; i < group.Value.Count; i++) { parent.AddAttachment(group.Value[i]); } script.UpdateDocument(parent); } }