I've been looking at the following article to create pages using the core service, I've found the page does create but only the last component presentation is added to the page? The code below shows how I'm using an existing page as an example to populate the new page, I'm building up the component presentation collection (which I thought was correct) but can't seem to work out why every cp isn't added?
public static PageData CreatePage(CoreServiceClient client, string sgId, string pageId)
{
var pageData = (PageData)client.Read(pageId, new ReadOptions());
var newPageData = new PageData();
newPageData.Id = "tcm:0-0-0";
newPageData.Title = pageData.Title;
newPageData.FileName = pageData.FileName;
// Loop over cps
foreach (var cp in pageData.ComponentPresentations)
{
// Build up collection of cps
newPageData.ComponentPresentations = new[]
{
new ComponentPresentationData
{
Component = new LinkToComponentData{ IdRef = cp.Component,
ComponentTemplate = new LinkToComponentTemplateData{ IdRef = cp.ComponentTemplate
}
};
}
newPageData.PageTemplate = new LinkToPageTemplateData
{
IdRef = pageData.PageTemplate.IdRef
};
newPageData.LocationInfo = new LocationInfo
{
OrganizationalItem = new LinkToOrganizationalItemData { IdRef = sgId }
};
// Setup metadata
if (!String.IsNullOrEmpty(pageData.Metadata))
{
newPageData.MetadataSchema = new LinkToSchemaData
{
IdRef = pageData.MetadataSchema.IdRef
};
newPageData.Metadata = pageData.Metadata;
}
try
{
newPageData = (PageData)client.Create(newPageData, new ReadOptions());
_log.InfoFormat("New page created {0}", newPageData.Id);
}
catch (Exception ex)
{
_log.ErrorFormat("Unable to create page {0}", ex);
}
return newPageData;
}