Set currentDocument property

Michele Frassetto asked on April 21, 2015 14:03

Hello, I've defined a new property, let's say "identifier", for the document type "Page (menu item)". I want to use this property in more than one web part inside the page. I use the following: {%CurrentDocument.GetValue("identifier")#%} to successfully read the property inside the web parts, but I'm not able to set the value in the c# codebehind source. I should set the value when the page loads, the web parts should read that value to correctly populate their content. I tried: protected override void OnInit(EventArgs e){ base.OnInit(e); CMSContext.CurrentDocument.SetValue("identifier", "something"); (...) but it does not work and always in the web parts I get the default value of the "identifier" property. what is the correct way to do? Best Regards

Recent Answers


Brenden Kehren answered on April 21, 2015 14:18

Best to get an instance of the current document and store it in it's own object, set the values and save the object. Using the CurrentDocument object is not a good idea. Have you looked at the API documentation for working with Documents/Pages?

Here is an example of the right way to get a document and update a property:

private bool GetAndUpdateDocuments()
{
    // Create an instance of the Tree provider first
    TreeProvider tree = new TreeProvider(MembershipContext.AuthenticatedUser);

    // Fill dataset with documents
    DataSet documents = tree.SelectNodes(SiteContext.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("CMS.MenuItem", documentRow, 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;
    }

    return false;
}
0 votesVote for this answer Mark as a Correct answer

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