Need to Upload a PDF file Using Kentico 10 API methods - DocumentHelper.AddAttachment

Sriram Poluri asked on January 12, 2017 15:45

Hello,

I am getting "The given path's format is not supported." error while uploading a pdf file using API methods. looks like I am missing something. need help to identify the issue.

Code to Upload a pdf document

// Creates a new page of the "CMS.File" page type TreeNode newPage = TreeNode.New(SystemDocumentTypes.File, tree);

                // Sets the properties of the new page
                newPage.DocumentName = System.IO.Path.GetFileName(filename);
                newPage.DocumentCulture = "en-us";

                // Inserts the new page as a child of the parent page
                newPage.Insert(parentPage);

                // Add the attachment to the CMS_Attachment table
                **var file = DocumentHelper.AddAttachment(newPage, "AttachmentGUID", filename);**
                //Associate the attachment with the document in the CONTENT_File table
                newPage.SetValue("FileAttachment", file.AttachmentGUID);

                //Save the changes to the database
                newPage.Update();

Thanks, Siva

Correct Answer

Trevor Fayas answered on January 12, 2017 17:46

Okay taking another look, try calling this instead

var file = DocumentHelper.AddAttachment(newPage, "FileAttachment", filename);
//Associate the attachment with the document in the CONTENT_File table
newPage.SetValue("FileAttachment", file.AttachmentGUID);

I believe the API call you want to set the "guidColumnName" (2nd parameter) to the newPage's field you want to set it to (FileAttachment).

Try that and see if that works!

1 votesVote for this answer Unmark Correct answer

Recent Answers


Trevor Fayas answered on January 12, 2017 15:54

What value does "filename" contain and afteryou create the var file does it have an attachmentGUID and show up in the database?

0 votesVote for this answer Mark as a Correct answer

Peter M answered on January 12, 2017 16:09 (last edited on January 12, 2017 16:35)

Check if the extension is permitted in Settings>system>files>Security> Upload extensions. Make sure that the physical path to the file is correct. This is an asp.net error. There are few answers on stackoverflow.

0 votesVote for this answer Mark as a Correct answer

Sriram Poluri answered on January 12, 2017 16:34

Hi Trevor Fayas,

Thanks for you reply. "filename" contains physical path of the pdf file. while executing DocumentHelper.AddAttachment(newPage, "AttachmentGUID", filename) I am getting mentioned error.

0 votesVote for this answer Mark as a Correct answer

Sriram Poluri answered on January 12, 2017 16:38 (last edited on January 12, 2017 16:46)

Hi Peter,

Thanks for you reply.verified Upload extensions, it contains pdf extension

0 votesVote for this answer Mark as a Correct answer

Trevor Fayas answered on January 12, 2017 16:46

Sriram, can you provide the ACTUAL value, so i can see the format of your file path? Depending on what you send it, that may be the issue.

0 votesVote for this answer Mark as a Correct answer

Sriram Poluri answered on January 12, 2017 16:59

File Path : C:\SourceControlRoot\WebContent\Test\Agenda011713.pdf

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on January 12, 2017 17:56

I had a requirement in the past to take media library files and convert them to page types with attachments. Below is a snippet of the code for v8.2 that you're looking for to create the attachment and update the document.

//add the attachment to the document
AttachmentInfo newAttachment = null;

// Path to the file to be inserted. This example uses an explicitly defined file path. However, you can use an object of the HttpPostedFile type (uploaded via an upload control).
string filePath = MediaLibraryPath + @"\" + mediaFile.FileName + mediaFile.FileExtension;
// MediaLibraryPath is something like this: C:\inetpub\wwwroot\sitename\media\PDF-files\Meeting Minutes

// Insert the attachment and update the document with its GUID
newAttachment = DocumentHelper.AddUnsortedAttachment(newNode, Guid.NewGuid(), filePath, tree, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE);

newNode.SetValue("FileAttachment", newAttachment.AttachmentGUID);
DocumentHelper.UpdateDocument(newNode);
newNode.Publish();
0 votesVote for this answer Mark as a Correct answer

Sriram Poluri answered on January 12, 2017 18:37

After changing the grid column name I am able to upload a document. Thanks Trevor Fayas

var file = DocumentHelper.AddAttachment(newPage, "FileAttachment", filename);

0 votesVote for this answer Mark as a Correct answer

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