Portal Engine Questions on portal engine and web parts.
Version 4.x > Portal Engine > Attachment file View modes: 
User avatar
Member
Member
ramon.almarza-emtmadrid - 2/2/2010 3:27:07 AM
   
Attachment file
Good morning,

We’re developing a web site for the municipal transport company in Madrid and we have a problem.

We´re triying to attach files to a node. The code that we are using is here:

private static void CrearFichero(string nombreEnlace, string enlace, string nombreFichero, CMS.TreeEngine.TreeNode nodoPadre, TreeProvider tree,String certificado)
{
CMS.TreeEngine.TreeNode node=new CMS.TreeEngine.TreeNode("CMS.File", tree);
try
{
// Create new document
//string fileName = Path.GetFileNameWithoutExtension(FileUpload.FileName);
node.DocumentCulture = CMSContext.PreferredCultureCode;
node.DocumentName = nombreFichero;

// Load default values
CMS.FormEngine.FormHelper.LoadDefaultValues(node);

node.SetValue("FileDescription", "");
node.SetValue("FileName", nombreFichero);
node.SetValue("textoEnlace", nombreEnlace);
node.SetValue("UrlCertificadoFandango", certificado);

// Set default template ID
//if (templateId > 0)
//{
// node.DocumentPageTemplateID = templateId;
//}
//else
//{
// Get the class
CMS.SettingsProvider.DataClassInfo ci = CMS.SettingsProvider.DataClassInfoProvider.GetDataClass("CMS.File");
if (ci == null)
{
throw new Exception("[Content/Edit.aspx]: Class 'CMS.File' not found.");
}
node.DocumentPageTemplateID = ci.ClassDefaultPageTemplateID;
//}

// Insert the document
DocumentHelper.InsertDocument(node, nodoPadre.NodeID, tree);

//Fichero
AttachmentManager am = new AttachmentManager(tree.Connection);

// Create the attachment
Guid attachmentGuid = Guid.NewGuid();

AttachmentInfo ai = new AttachmentInfo("c:\\temp\\" + nombreFichero, node.DocumentID, attachmentGuid, tree.Connection);

am.SetAttachmentInfo(ai);

// Set the attachment reference
node.SetValue("FileAttachment", attachmentGuid);
node.Update();
DocumentHelper.UpdateDocument(node, tree);

// Add the file
//DocumentHelper.AddAttachment(node, "FileAttachment", FileUpload.PostedFile, tree);

// Create default SKU if configured
if (ModuleEntry.CheckModuleLicense(ModuleEntry.ECOMMERCE, UrlHelper.GetCurrentDomain(), FeatureEnum.Ecommerce, VersionActionEnum.Insert))
{
node.CreateDefaultSKU();
}


WorkflowManager wm = new WorkflowManager(tree);
// Approve until the step is publish
WorkflowStepInfo currentStep = wm.GetStepInfo(node);
while ((currentStep != null) && (currentStep.StepName.ToLower() != "published"))
{
currentStep = wm.MoveToNextStep(node, "");
}


}
catch (Exception ex)
{
// Delete the document if something failed
if ((node != null) && (node.DocumentID > 0))
{
DocumentHelper.DeleteDocument(node, tree, false, true, true);
}
//lblError.Text = ResHelper.GetString("NewFile.Failed") + ": " + ex.Message;
}
}


But this isn´t working well, because the file not attach to the node. The element CMS.File is created, but the attach file is not included in the property FileAttachment. Is this code correct?? What’s the problem??

Thanks in advance.

User avatar
Kentico Support
Kentico Support
kentico_jurajo - 2/3/2010 2:28:07 AM
   
RE:Attachment file
Hi,

Since you are using workflow, you need to work with methods from DocumentHelper class - I can see that you tried and it is commented out. Right now you are working with the published version of the document and not with actual version. I think this method should do the trick:

CMS.WorkflowEngine.DocumentHelper.AddAttachment(CMS.TreeEngine.TreeNode, string GUIDcolumnName, string FilePath, CMS.TreeEngine.TreeProvider)


You can also take a look into this file - \CMSModules\Content\CMSDesk\New\NewFile.aspx.cs. It is a bit complicated example of creating new file, but I hope it will help.

Best Regards,
Juraj Ondrus