API
Version 7.x > API > attachment to a new culture version of this document View modes: 
User avatar
Member
Member
peterm-technologyevaluation - 4/1/2013 4:20:02 PM
   
attachment to a new culture version of this document


There is something what I am missing about creating file attachment for a new culture. Basically I am creating a document with file attachment in several languages.

It clearly works if I do it manually in CMS Desk: Create a document in English attach English file. Create culture version of it in Spanish attach Spanish file.

Programmatically if I try to repeat the same sequence if I create Spanish attachment after English I overwrite English attachment and I end up with Spanish attachment for English and for Spanish version.

I have this InsertAsNewCultureVersion ,but there is no "InsertCultureVersionAttachment".

For example if I have English Spanish and Chinese : First I create English then Spanish then Chinese. I create 3 cultures with 3 “cultural” attachments , but because Chinese culture is the last in my loop it overwrites the attachments in English and Spanish.

Here some basic code sample:

// Create a new document

string defaultDocumentPath = "/Content-Library/";
string defaultSite = CMSContext.CurrentSiteName;
string defaultCulture = "en-us";



UserInfo ui = UserInfoProvider.GetUserInfo("administrator");
TreeProvider tree = new TreeProvider(ui);
// Get parent node
TreeNode parentNode = tree.SelectSingleNode(defaultSite, defaultDocumentPath, defaultCulture);


TreeNode node = TreeNode.New("Custom.MyDocument", tree);

// Set the document properties
node.DocumentName = "My English Document";
node.DocumentCulture = defaultCulture;

// attache file to english 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 postedFile = Server.MapPath("/samples/English.xls");
newAttachment = DocumentHelper.AddAttachment(node, "Sample", postedFile, tree);

node.Update();

...

// create spanish version
node.DocumentName = "Mi documento españolas";
node.InsertAsNewCultureVersion("es-ES");

newAttachment = null;
postedFile = Server.MapPath("/samples/spanish.xls");
newAttachment = DocumentHelper.AddAttachment(node, "Sample", postedFile, tree);
node.Update();

User avatar
Member
Member
kentico_sandroj - 4/2/2013 1:21:54 PM
   
RE:attachment to a new culture version of this document
Hi Peter,

As per our conversation, the attachment issue has been resolved. For future reference or for anyone that might be interested, the following code was used:

node.InsertAsNewCultureVersion(CultureCode); // Inserts new culture version (keeps attachment GUID value in the field for the new culture version)

node.SetValue(“Sample”, null); // Clears the attachment GUID from the new culture version – we want to attach brand new attachment with different GUID

DocumentHelper.AddAttachment(node, "Sample", postedFile, tree); // Adds attachment with new GUID and stores GUID to the document field (Without clearing the GUID it uses the one from document field and gets the existing attachment and overrides it with the new binary data)

node.Update(); // Update document to store new field value

Best Regards,
Sandro Jankovic