Dear George,
Yes, you are right, there was a mistake in my code, apologies for that. Here is the correct one:
public IEnumerable<TreeNode> GetRelatedDocuments(TreeNode mainNode, string relatedProperty)
{
IEnumerable<TreeNode> result = null;
var dataCacheKey = $"custom|{mainNode.NodeID}|{relatedProperty}";
using (var cs = new CachedSection<IEnumerable<TreeNode>>(ref result, 10, true, dataCacheKey))
{
if (cs.LoadData)
{
var formField = FormHelper.GetFormInfo(mainNode.NodeClassName, false, true, false)
.GetFormField(relatedProperty);
var adHocRelationshipNameCodeName = RelationshipNameInfoProvider
.GetAdHocRelationshipNameCodeName(mainNode.NodeClassName, formField);
var relationshipNameInfo = RelationshipNameInfoProvider
.GetRelationshipNameInfo(adHocRelationshipNameCodeName);
var query = RelationshipInfoProvider.ApplyRelationshipOrderData(
DocumentHelper.GetDocuments().Culture(new string[] { mainNode.DocumentCulture })
.WithCoupledColumns(true).Published(!mainNode.IsLastVersion)
.PublishedVersion(!mainNode.IsLastVersion).InRelationWith(mainNode.NodeGUID,
adHocRelationshipNameCodeName, RelationshipSideEnum.Left), mainNode.NodeID,
relationshipNameInfo.RelationshipNameId);
result = query.ToList();
cs.Data = result;
// related pages by ID
var dependencies = result.Select(x => $"nodeid|{x.NodeID}").ToList();
// main page dependencies by ID
dependencies.Add($"nodeid|{mainNode.NodeID}|relationships");
cs.CacheDependency = CacheHelper.GetCacheDependency(dependencies);
}
}
return result;
}
Regarding the second point, even if it's not an explicit relationship, CMS_Relationship is used behind the scenes. My expectation was it should work as well. Probably regarding this you may contact support as there may be a bug or something. Anyway, as a workaround, you can use "main" Node ID. I know it's not ideal, and will invalidate the cache when other properties of that page change, but should work:
dependencies.Add($"nodeid|{mainNode.NodeID}");
// instead of
dependencies.Add($"nodeid|{mainNode.NodeID}|relationships");