Portal Engine Questions on portal engine and web parts.
Version 5.x > Portal Engine > Having trouble updating attachments via API (using C#) View modes: 
User avatar
Member
Member
sfbrown-hcso.tampa.fl - 2/1/2011 4:02:16 PM
   
Having trouble updating attachments via API (using C#)
I am currently in the process of updating a large number of documents. The requirement is that if they have a word document attached, I need to replace that attachment with a PDF version.

I've allready handled the conversion without an issue. What I am having trouble with is that when I replace the attachment with the PDF, it does not show up on the document (the upload box is empty, no document to display, returns error of GUID not found).

I've traced the SQL calls to a failure to find the new attachment GUID on CMS_AttachmentHistory. I think I'm missing something in my code that's preventing the version table from being updated properly. The file is loaded up to the attachments table so I'm not sure what is missing. I tried adding the PublishAttachments call but that didn't seem to do much.

This is a one-off program so excuse the slopiness. :) Any help is greatly appreciated.


private static void ConvertToPdfPass2()
{
//Control file, what files go with what documents
TextReader reader = new StreamReader(destPath + @"\fileinfo.txt");

Console.WriteLine("Beginning upload of pdfs...");
CMS.TreeEngine.TreeProvider tree = new TreeProvider(CMS.CMSHelper.CMSContext.CurrentUser);

string currentFileLine;

currentFileLine = reader.ReadLine();

//Keep reading until all files processed
while (currentFileLine != null)
{
string nodeId;
string oldGuid;
string documentName;
string oldFileName;

nodeId = currentFileLine.Split(',')[0];
oldGuid = currentFileLine.Split(',')[1];
documentName = currentFileLine.Split(',')[2];
oldFileName = currentFileLine.Split(',')[3];

//change the filename to the pdf version
string newFileName = oldFileName.Substring(0, oldFileName.Length - 4) + ".pdf" ;
string newFullName = destPath + @"\" + newFileName;

int nId = int.Parse(nodeId);
Guid oguid = new Guid(oldGuid);
Guid pdfGuid = Guid.NewGuid();


TreeNode node = tree.SelectSingleNode(nId, "en-us", "hcso.document");
WorkflowManager wm = new WorkflowManager(tree);
VersionManager vm = new VersionManager(tree);
vm.CheckOut(node);

// Create the attachment manager
AttachmentManager am = new AttachmentManager(tree.Connection);
//am.DeleteAtachments(node.DocumentID);

// Create the attachment
AttachmentInfo ai = new AttachmentInfo(newFullName, node.DocumentID, pdfGuid, tree.Connection);
ai.AttachmentMimeType = "application/pdf";
am.SetAttachmentInfo(ai);
vm.PublishAttachments(node.DocumentCheckedOutVersionHistoryID, "Intranet", node);

AttachmentInfo info = am.GetAttachmentInfo(pdfGuid, "Intranet");

node.SetValue("FileAttachment", pdfGuid);
node.Update();
vm.CheckIn(node, null, null);

//Just verify if not published. If not published, publish it.

WorkflowInfo wi = wm.GetNodeWorkflow(node);
WorkflowStepInfo si = wm.GetStepInfo(node);

while (si.StepName.ToLower() != "published")
{
Debug.WriteLine("Trying to publish...");
si = wm.MoveToNextStep(node, "");
if (si == null)
break;
}
currentFileLine = reader.ReadLine();
}
}







User avatar
Member
Member
sfbrown-hcso.tampa.fl - 2/2/2011 10:01:49 AM
   
RE:Having trouble updating attachments via API (using C#)
Never mind...

I was able to clean this up a lot by using.

DocumentHelper.DeleteAttachment(node, oguid, tree);
DocumentHelper.AddAttachment(node, "FileAttachment", newFullName, tree);

Still new to the Kentico API and I was making things more difficult than they needed to be.