6

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;
    }
9

You are overwriting your array of ComponentPresentationData objects in each iteration of your foreach:

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
        } 
    };
}

Initialize the array before the loop, then loop over pageData.ComponentPresentations and add each one to the new array, like so:

// Get the number of Component Presentations
int numCPs = pageData.ComponentPresentations.Length;

// Initialize the new array to that number
newPageData.ComponentPresentations = new ComponentPresentationData[numCPs];

// Loop over each CP and copy it to the new array
for (int i = 0; i < numCPs; i++)
{
    var cp = pageData.ComponentPresentations[i];
    newPageData.ComponentPresentations[i] = new ComponentPresentationData
    {
        Component = new LinkToComponentData
            { IdRef = cp.Component },
        ComponentTemplate = new LinkToComponentTemplateData
            { IdRef = cp.ComponentTemplate }
    };
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.