using System; using System.Data; using CMS.CMSHelper; using CMS.GlobalHelper; using CMS.DocumentEngine; using CMS.UIControls; [Title(Text = "Document aliases", ImageUrl = "Objects/CMS_Document/object.png")] public partial class CMSAPIExamples_Code_Documents_DocumentAliases_Default : CMSAPIExamplePage { #region "Initialization" protected void Page_Load(object sender, EventArgs e) { // Document alias apiCreateDocumentAlias.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(CreateDocumentAlias); apiGetAndUpdateDocumentAlias.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndUpdateDocumentAlias); apiGetAndBulkUpdateDocumentAliases.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndBulkUpdateDocumentAliases); apiDeleteDocumentAlias.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(DeleteDocumentAlias); } #endregion #region "Mass actions" /// /// Runs all creating and managing examples. /// public override void RunAll() { base.RunAll(); // Document alias apiCreateDocumentAlias.Run(); apiGetAndUpdateDocumentAlias.Run(); apiGetAndBulkUpdateDocumentAliases.Run(); } /// /// Runs all cleanup examples. /// public override void CleanUpAll() { base.CleanUpAll(); // Document alias apiDeleteDocumentAlias.Run(); } #endregion #region "API examples - Document alias" /// /// Creates document alias. Called when the "Create alias" button is pressed. /// private bool CreateDocumentAlias() { // Get "Home" document TreeNode document = TreeHelper.GetDocument(CMSContext.CurrentSiteName, "/Home", CultureHelper.GetPreferredCulture(), true, "CMS.MenuItem", false); if (document != null) { // Create new document alias object DocumentAliasInfo newAlias = new DocumentAliasInfo(); // Set the properties newAlias.AliasURLPath = "/MyNewAlias"; newAlias.AliasNodeID = document.NodeID; newAlias.AliasSiteID = CMSContext.CurrentSiteID; // Save the document alias DocumentAliasInfoProvider.SetDocumentAliasInfo(newAlias, CMSContext.CurrentSiteName); return true; } return false; } /// /// Gets and updates document alias. Called when the "Get and update alias" button is pressed. /// Expects the CreateDocumentAlias method to be run first. /// private bool GetAndUpdateDocumentAlias() { // Prepare the parameters string orderBy = ""; string where = "AliasURLPath = N'/MyNewAlias'"; // Get the data DataSet aliases = DocumentAliasInfoProvider.GetDocumentAliases(where, orderBy); if (!DataHelper.DataSourceIsEmpty(aliases)) { DocumentAliasInfo updateAlias = new DocumentAliasInfo(aliases.Tables[0].Rows[0]); // Update the properties updateAlias.AliasURLPath = updateAlias.AliasURLPath.ToLower(); // Save the changes DocumentAliasInfoProvider.SetDocumentAliasInfo(updateAlias, CMSContext.CurrentSiteName); return true; } return false; } /// /// Gets and bulk updates document aliases. Called when the "Get and bulk update aliases" button is pressed. /// Expects the CreateDocumentAlias method to be run first. /// private bool GetAndBulkUpdateDocumentAliases() { // Prepare the parameters string orderBy = ""; string where = "AliasURLPath = N'/MyNewAlias'"; // Get the data DataSet aliases = DocumentAliasInfoProvider.GetDocumentAliases(where, orderBy); if (!DataHelper.DataSourceIsEmpty(aliases)) { // Loop through the individual items foreach (DataRow aliasDr in aliases.Tables[0].Rows) { // Create object from DataRow DocumentAliasInfo modifyAlias = new DocumentAliasInfo(aliasDr); // Update the properties modifyAlias.AliasURLPath = modifyAlias.AliasURLPath.ToUpper(); // Save the changes DocumentAliasInfoProvider.SetDocumentAliasInfo(modifyAlias, CMSContext.CurrentSiteName); } return true; } return false; } /// /// Deletes document alias. Called when the "Delete alias" button is pressed. /// Expects the CreateDocumentAlias method to be run first. /// private bool DeleteDocumentAlias() { // Prepare the parameters string orderBy = ""; string where = "AliasURLPath = N'/MyNewAlias'"; // Get the data DataSet aliases = DocumentAliasInfoProvider.GetDocumentAliases(where, orderBy); if (!DataHelper.DataSourceIsEmpty(aliases)) { DocumentAliasInfo deleteAlias = new DocumentAliasInfo(aliases.Tables[0].Rows[0]); // Delete the document alias DocumentAliasInfoProvider.DeleteDocumentAliasInfo(deleteAlias); return true; } return false; } #endregion }