Data FAQ

How can I programatically attach a Page Data Folder to a page?

I have developed a feature that takes input from website visitors and store it in C1. The data needs a reference to the page it's submitted from so I have created a Page Data Folder type in which to store the data. 

One of the features of this type is that C1 users can attach it to a page as a Page Data Folder (a right click action on the page) enabling them to manage the data relating to a page exactly below this page. I would like to 'steal' this feature but avoid asking my users to do the 'right click' action - I would like the Page Data Folder for my type to show up automatically below a page the first time data is added to the page.

How can I programatically detect if my type have already been attached to a specific page, and how can I programatically attach it if needed?

Answer:

// Sample type used here - change this line:
Type commentItemType = Composite.Types.TypeManager.TryGetType("DynamicType:Composite.Community.PageComments.Item");
if  (commentItemType != null)
{
    Guid commentItemTypeId = commentItemType.GetImmutableTypeId();
IPage currentPage = PageManager.GetPageById(pageId);
    //Check for existing assosiation between page and type
    if  (DataAggregationVisabilityFacade.GetInstanceDefinedAssociationDescriptionIds(currentPage, commentItemType).Any() == false)
    {
        //Create assosiation
        DataAggregationVisabilityFacade.AddAggregationDescription(currentPage, commentItemType, DataAssociationVisabilityRule.OneToOne());
    }
}