Set Pages form field in MVC

James Mosquito asked on April 7, 2021 14:20

Hi,

I have added a Pages form field on one of my page types and would like to add pages to it from MVC.

Can anyone direct me to an example of how to do this please.

Thanks, James

Correct Answer

Dmitry Bastron answered on April 8, 2021 12:38

Hi James,

Try the following code and see if it works:

var pageType = DataClassInfoProvider.GetDataClassInfo("custom.PageTypeName");
var pagesField = new DataDefinition(pageType.ClassFormDefinition)
    .GetFields<FieldInfo>()
    .FirstOrDefault(x => x.Name == "YourPagesFieldName");
var relationshipName = RelationshipNameInfoProvider.GetAdHocRelationshipNameCodeName("custom.PageTypeName", pagesField);
var relationshipNameInfo = RelationshipNameInfo.Provider.Get(relationshipName);

RelationshipInfo.Provider.Add(currentPageNodeId, newRelatedPageNodeId, relationshipNameInfo.RelationshipNameId);
0 votesVote for this answer Unmark Correct answer

Recent Answers


James Mosquito answered on April 9, 2021 16:36 (last edited on April 9, 2021 16:36)

Thanks Dmitry,

I should have pointed out that I am using Kentico 12SP and the methods to add a page are slightly different but I was able to work it from your example and the relevant API documentation: http://devnet.kentico.com/docs/12_0/api/html/M_CMS_DocumentEngine_RelationshipInfoProvider_AddRelationship.htm

0 votesVote for this answer Mark as a Correct answer

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