Search code examples
kentico

Can't import a file into a page using the import toolkit


I'm working through an import using the Kentico Import Toolkit for Kentico 12.

My intention is to import content from a CSV file into pages in Kentico. It's fundamentally working, however, I can't get it to take a PDF on the file system and bring that in as a binary object into a Kentico page property.

In my CSV file I have a column called "FileName", and in Kentico I have a page type called "Asset" which has a property called "File" which is of type File.

In my column mapping I have the "File" property mapped as follows:

#<file>c:\temp\filesfortesting\{%FileName%}

The import runs and the pages are created however no files are actually imported and mapped to the File property on the page.

Any suggestions as to how I can fix this? Do I have the mapping right?


Solution

  • I guess you want to import is as a page attachment. In this case, you need to import the pages first and then, in a second run import the page attachments.It might be easier to create a simple tool and use API to do the migration.

    At first I will clarify the types of attachments in Kentico:

    • Unsorted attachments: these are the attachments added through e.g. WYSIWYG editor and are available on page's Properties -> Attachments tab

    • Grouped attachments: these are the files attached to pages using the Attachments form control. You can change the order of the attachments and you manage them on the Form tab

    • CMS.File and direct file field attachments: these are the files uploaded to a page on the Form tab using the Direct uploader form control.

    So, in the import toolkit, you will select the page attachments to be imported and then configure the source and import the attachments into the CMS_Attachment table.

    The import toolkit supports importing of the unsorted attachments only. So, if you want to move the attachments you may need to do it in multiple runs + execute some API code. When importing the attachments, you need to set the WHERE condition to match your pages, in my case I was using /news/% path: Where condition:
    AttachmentDocumentID IN (SELECT DocumentID FROM CMS_Document WHERE DocumentNamePath LIKE '/news/%')

    Then, select some dummy page where to import all the attachments. remember? KIT imports attachments as unsorted. In this case I used the parent /news page with DocumentID = 1063 so all attachments are assigned to this page.

    Then, execute code similar to this to move the attachments to respective pages:

    // Creates a new instance of the Tree provider
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
    
    // Gets  the parent page
    TreeNode page = tree.SelectNodes()
                .Path("/news")
                .OnCurrentSite()
                .Culture("en-us")
                .TopN(1)
                .FirstOrDefault();
    
            if (page != null)
            {
                foreach (DocumentAttachment attachment in page.AllAttachments)
                {
                    // Perform any action with the attachment object (DocumentAttachment), NewsTeaser is the target page’s direct file upload field where I want to get the attachment
                    TreeNode targetPage = tree.SelectNodes()
                        .Type("CMS.News")
                        .Where("NewsTeaser = '" + attachment.AttachmentGUID + "'")
                        .TopN(1)
                        .Culture("en-us")
                        .OnCurrentSite()
                        .FirstOrDefault();
    
                    if (targetPage != null)
                    {
                        // Edits the attachment's metadata (name, title and description) and set the right AttachmentDocumentID and AttachmentIsUnsorted to false
                        attachment.AttachmentDocumentID = targetPage.DocumentID;
                        attachment.AttachmentIsUnsorted = false;
                        // Ensures that the attachment can be updated without supplying its binary data
                       attachment.AllowPartialUpdate = true;
                        // Saves the modified attachment into the database
                        attachment.Update();
                    }
                }
            }