copy document and change document type

Tomasz Czado asked on April 16, 2015 14:37

I want to copy document from one to another without loosing content but with changing document type. I was trying something like this, but nothing happened.

TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);
string className = "Test.Product";

TreeNodeDataSet singleNode = tree.SelectNodes(SiteContext.CurrentSiteName, "/Test/atbc", TreeProvider.ALL_CULTURES, true);
if (singleNode != null)
{
    foreach (DataRow node in singleNode.Tables[0].Rows)
    {
        TreeNode doc1 = TreeNode.New(className, node, tree);
    }
}

Correct Answer

Brenden Kehren answered on April 16, 2015 14:56

Have you looked at the API Examples in your local install? They can be found under /CMSApiExamples/Code/Documents/Basics/Default.aspx.cs. Below is the code from there:

/// <summary>
/// Creates the initial document structure used for the example. Called when the "Create page structure" button is pressed.
/// </summary>
private bool CreateDocumentStructure()
{
    // Add a new culture to the current site
    CultureInfo culture = CultureInfoProvider.GetCultureInfo("de-de");
    CultureSiteInfoProvider.AddCultureToSite(culture.CultureID, SiteContext.CurrentSiteID);

    // Create new instance of the Tree provider
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

    // Get parent node
    TreeNode parentNode = tree.SelectSingleNode(SiteContext.CurrentSiteName, "/", "en-us");

    if (parentNode != null)
    {
        // Create the API Example folder
        TreeNode newNode = TreeNode.New("CMS.Folder", tree);

        newNode.DocumentName = "API Example";
        newNode.DocumentCulture = "en-us";

        newNode.Insert(parentNode);

        parentNode = newNode;

        // Create the Source folder - the document to be moved will be stored here
        newNode = TreeNode.New("CMS.Folder", tree);

        newNode.DocumentName = "Source";
        newNode.DocumentCulture = "en-us";

        newNode.Insert(parentNode);

        // Create the Target folder - a document will be moved here
        newNode = TreeNode.New("CMS.Folder", tree);

        newNode.DocumentName = "Target";
        newNode.DocumentCulture = "en-us";

        newNode.Insert(parentNode);

        return true;
    }

    return false;
}
1 votesVote for this answer Unmark Correct answer

Recent Answers


Brenden Kehren answered on April 16, 2015 14:48

On top of what you already have you have to set the values of the fields in your foreach loop:

doc1.SetValue("ColumnName", columnValue);
doc1.Insert();
doc1.Publish();
0 votesVote for this answer Mark as a Correct answer

Tomasz Czado answered on April 16, 2015 14:50 (last edited on April 16, 2015 14:50)

I was trying to use Insert(), but then I got "Specified method is not supported." exception

0 votesVote for this answer Mark as a Correct answer

Tomasz Czado answered on April 16, 2015 15:01

I saw it, but I was wondering, if there is any possible to copy all properties between documents without doing it step by step.

As I understand now, we cannot.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on April 16, 2015 15:17

Since they are different objects, no, you have to map them.

2 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.