API Questions on Kentico API.
Version 6.x > API > How to change the document type of an existing document? View modes: 
User avatar
Member
Member
Bruno Nepomuceno - 3/12/2013 4:05:55 PM
   
How to change the document type of an existing document?
Hi everyone,

I know other people have already asked this before but as I am extremely new to Kentico, I would need something more specific like a code snippet. The question is... how do I change the document type of an existing document?

I found this http://devnet.kentico.com/Forums/f36/fp16/t11910/Change-Document-Type.aspx and http://devnet.kentico.com/Forums/f4/t3795/change-document-type-of-an-existing-page.aspx. The basic idea would be copy the document, change its type and delete the original one.

I have found this reference that could help me:

Managing content (select document(s), copy document(s), delete document(s),...):
http://devnet.kentico.com/Docs/DevGuide22/index.html?creating_documents.htm

I am also aware of CMS.WorkflowEngine.DocumentHelper class.

So basically, I have everything I need except the line of code that would actually change the document type of a document.

Could anyone help me?

User avatar
Kentico Support
Kentico Support
kentico_radekm - 3/13/2013 2:47:43 AM
   
RE:How to change the document type of an existing document?
Helllo.

Let's start with what version of KCMS do you actually use? This forum thread is opened in Kentico 6 category and your links refer to version 2.2. Can you clarify this, please?

Best Regards,
Radek Macalik

User avatar
Member
Member
Bruno Nepomuceno - 3/13/2013 4:11:18 AM
   
RE:How to change the document type of an existing document?
Hi,

I'm sorry if I let you confused; I am using Kentico 6.

User avatar
Kentico Support
Kentico Support
kentico_radekm - 3/13/2013 4:21:09 AM
   
RE:How to change the document type of an existing document?
Hello.

Thank you for clarification. As both forum threads you link mention, this is not supported out-of-the-box and you will need to write a custom code for it. Please note, we did not test it, do not have sample code for this very task and this operation is completely up to you. Therefore, do not forget to back-up your project and DB before performing this customization.

You will need to follow these steps:

Optionally you could use our API and take every original document, create a new document of appropriate document type and then delete the original document.

How to get an original document and how to create new document (TreeNode object) can be found in the following code samples: http://devnet.kentico.com/docs/6_0/devguide/documents_api_examples_managing_documents.htm

How to delete original document is described at http://devnet.kentico.com/docs/6_0/devguide/documents_api_examples_deleting_documents.htm

Best Regards,
Radek Macalik

User avatar
Member
Member
Bruno Nepomuceno - 3/13/2013 12:14:55 PM
   
RE:How to change the document type of an existing document?
Hi Radek,

I really appreciate your help but this doesn't work. I have tried it before and I tried it again and it still failing. I will explain what I think the reason is.

First, I am following what is in here (http://devnet.kentico.com/docs/6_0/devguide/documents_api_examples_managing_documents.htm).

This is the code:

// Fill dataset with documents
DataSet documents = tree.SelectNodes(CMSContext.CurrentSiteName, "/API-Example/%", "en-us", false, "CMS.MenuItem");

if (!DataHelper.DataSourceIsEmpty(documents))
{
// Loop through all documents
foreach (DataRow documentRow in documents.Tables[0].Rows)
{
// Create a new Tree node from the data row
TreeNode editDocument = TreeNode.New(documentRow, "CMS.MenuItem", tree);

string newName = editDocument.DocumentName.ToLower();

// Change coupled data
editDocument.SetValue("MenuItemName", newName);
// Change document data
editDocument.DocumentName = newName;

// Save to database
editDocument.Update();

return true;
}
}


The problem is this is not what I need. It is basically copying a document to another one with the same type. What I don't know is how to change the type. I tried:


TreeNode editDocument = TreeNode.New(documentRow, "CMS.MyNewTypeHere", tree);


but then it destroyed some of the references to the original file, didn't create the new one and now I ended up with something like this:

http://farm9.staticflickr.com/8252/8555330814_31dede5324_b.jpg

BTW, I can't even remove this document anymore. "Right click -> Delete" doesn't work.

Thanks!

User avatar
Member
Member
Bruno Nepomuceno - 3/13/2013 2:00:30 PM
   
RE:How to change the document type of an existing document?
BTW, I forgot to ask, did I do something wrong? Would you have a suggestion to make it work?

Thanks!

User avatar
Kentico Support
Kentico Support
kentico_radekm - 3/18/2013 2:52:26 AM
   
RE:How to change the document type of an existing document?
Hello.

Regrettably, we do not have any sample code for this specific task, as in general, this operation is not possible, because of document type data are stored in multiple tables and views and it is not possible to do a simple change / switch.

The only way is getting the data of the current document, delete it and then, create a new document of the other document type with the data of the removed document. Or, you could use Import toolkit to do it.

So, you need to create brand new documents based on new document type as per http://devnet.kentico.com/docs/devguide/documents_api_examples_creating_documents.htm. Then, you need to get original document via http://devnet.kentico.com/docs/devguide/documents_api_examples_managing_documents.htm and set document data of a new document like (newNode.SetValue(<columnName>,<value>), where <value> is respective data value from your original document (which you got in step 2).
Finally, you can delete original document.

Best Regards,
Radek Macalik