Create adhoc page relationship through kentico api

Sultan Ahmed asked on June 26, 2020 14:32

We can add page relationship in two different ways using named relationships and pages data type which is kind of advanced content modelling in Kentico..

if we go through named relationships then we can give a meaningful relationship between to content node by providing description. So, we get a relationship name called “is related to”(example). When we use this in practice, then we get [page A] {is related to} [Page B].

if we go through pages data type then we assign some content on the form tab, records are created in the “CMS_Relationship” table as ad-hoc via the “RelationshipIsAdHoc” column and there is no relationship name for this as such. It is marked as Ad-hoc. and Relationship name is also added page type name underscore some randon guid example abc.product_3d628a37-7637-4a21-b0b4-e1dd1a00a3bc

My question is when we try to use page data type and we need to add relationship through api code, then how can we add because in kentico api to add page relationship through api code RelationShipNameID is mandatory field. We don't have this RelationShipNameID as we are not going through named relationship.

Correct Answer

Sultan Ahmed answered on June 26, 2020 14:34

Found out the way

Need to retrieve ad-hoc relatioshipnameinfo object, e.g. like this:

string codeName = GetAdHocRelationshipNameCodeName("fillclassnamehere", field);
var relationshipNameInfo = GetRelationshipNameInfo(codeName);

and then use is with API:

RelationshipInfoProvider.AddRelationship(leftSiteId, rightSiteId, relationshipNameInfo.RelationshipNameId)

0 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.