API
Version 7.x > API > DocumentHelper.AddAttachment Not Saving Attachment? View modes: 
User avatar
Member
Member
matt-awg - 12/20/2013 9:50:26 AM
   
DocumentHelper.AddAttachment Not Saving Attachment?
Any idea why a call to AddAttachment would not return any error but also not seem to actually add the image I am trying to add to a "File" type column on a custom document type?

I am using this version of the method:
DocumentHelper.AddAttachment(TreeNode node, String guidColumnName, String file, TreeProvider tree)

where:
node = the TreeNode of the custom document type I am trying to add an image to.
guidColumnName = name of the column containing the Attachment GUID (Defined as File data type for this custom document type)
file = attachment file path (actual path to the image file on the web server that I am trying to attach (I verify its existance first by calling File.Exists)
tree = treeProvider to use

Here is my full code if that helps:
    private bool insertTeacherMainImage(string pUsername, string pImage, ref string statMsg)
{
CMS.DocumentEngine.TreeProvider dp = new CMS.DocumentEngine.TreeProvider(ui);

CMS.DocumentEngine.TreeNode teacherDoc = dp.SelectSingleNode(siteName, TEACHER_DIRECTORY_ALIASPATH + "/" + pUsername, cultureCode, true, TEACHER_CUSTOM_CLASS_NAME);

if (teacherDoc != null)
{
pImage = PATH_TO_TEACHER_IMAGES + pImage;

if (!File.Exists(pImage))
{
return false;
}

//insert link under teacher's document
try
{
CMS.DocumentEngine.DocumentHelper.AddAttachment(teacherDoc, "MainImage", pImage, dp);

return true;
}
catch (Exception ex)
{
return false;
}
}

// if we get here, the teacher document did not exist!
return false;
}

This code returns true so it is finding the TreeNode, finding the image on the server that I want to add, and the call to AddAttachment does not raise an error. Is there some other step, like a "save" or "update" method I need to call as well as the AddAttachment?
Thanks,
Matt

User avatar
Kentico Legend
Kentico Legend
Accepted solutionAccepted solution
Brenden Kehren - 12/20/2013 11:39:11 AM
   
RE:DocumentHelper.AddAttachment Not Saving Attachment?
Hey Matt!

In the API Examples there is a method that shows something similar to what you're attempting. it looks like you might be missing the node.Update() method call. Here is the code:
private bool InsertFieldAttachment()
{
// Create a new instance of the Tree provider
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

// Get the example document
TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/API-Example", "en-us");

if (node != null)
{
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 postedFile = Server.MapPath("Files/file.png");

// Insert the attachment and update the document with its GUID
newAttachment = DocumentHelper.AddAttachment(node, "MenuItemTeaserImage", postedFile, tree);
node.Update();

if (newAttachment != null)
{
return true;
}

apiInsertFieldAttachment.ErrorMessage = "Couldn't insert the attachment.";
}

return false;
}

User avatar
Member
Member
matt-awg - 12/20/2013 12:09:50 PM
   
RE:DocumentHelper.AddAttachment Not Saving Attachment?
Thanks FroggEye... that was it... just added
teacherDoc.Update();
after the AddAttachment call. I don't know why I did not just try that before posting, as you can see in the original post, I even said it may require an update. Oh well. Thanks again!!!

User avatar
Member
Member
rasiuddin-takweenmedia - 2/4/2014 1:13:23 AM
   
RE:DocumentHelper.AddAttachment Not Saving Attachment?
I am using Same code but i cant get attachment in node folder

private bool InsertUnsortedAttachment()
{
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);
CMS.DocumentEngine.TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/Images", "en-us");
string postedFile = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
if (node != null)
{

//return (DocumentHelper.AddUnsortedAttachment(node, Guid.NewGuid(), postedFile, tree, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE) != null);

AttachmentInfo newAttachment = null;

//newAttachment = DocumentHelper.AddAttachment(node, "MenuItemTeaserImage", postedFile, tree);
newAttachment = DocumentHelper.AddAttachment(node, "MenuItemTeaserImage", postedFile, tree);

node.Update();

if (newAttachment != null)
{
return true;
}
}

return false;
}

User avatar
Member
Member
kentico_davidb2 - 2/4/2014 5:30:51 AM
   
RE:DocumentHelper.AddAttachment Not Saving Attachment?
Are you using Kentico CMS version 7?

You are asking: I am using Same code but i cant get attachment in node folder

Do you mean Unsorted attachments that would be?
private bool InsertUnsortedAttachment()
{
// Create a new instance of the Tree provider
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

// Get the document
TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/API-Example", "en-us");

// 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 postedFile = Server.MapPath("Files/file.png");

if (node != null)
{
// Insert the attachment
return (DocumentHelper.AddUnsortedAttachment(node, Guid.NewGuid(), postedFile, tree, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE, ImageHelper.AUTOSIZE) != null);
}

return false;
}


For field attachments:
private bool InsertFieldAttachment()
{
// Create a new instance of the Tree provider
TreeProvider tree = new TreeProvider(CMSContext.CurrentUser);

// Get the example document
TreeNode node = tree.SelectSingleNode(CMSContext.CurrentSiteName, "/API-Example", "en-us");

if (node != null)
{
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 postedFile = Server.MapPath("Files/file.png");

// Insert the attachment and update the document with its GUID
newAttachment = DocumentHelper.AddAttachment(node, "MenuItemTeaserImage", postedFile, tree);
node.Update();

if (newAttachment != null)
{
return true;
}

apiInsertFieldAttachment.ErrorMessage = "Couldn't insert the attachment.";
}

return false;
}


Or do you want to create a new CMS.File document?

Should you have further issues, please try debugging your code. Is postedFile set?