Kentico CMS 7.0 Developer's Guide

Managing document aliases

Managing document aliases

Previous topic Next topic Mail us feedback on this topic!  

Managing document aliases

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

Arrow


API examples for newer versions


Please visit the latest API Examples documentation to view API examples for newer versions of Kentico.



The following example creates a document alias.

 

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;
}

 

The following example gets and updates a document alias.

 

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;
}

 

The following example gets and bulk updates document aliases.

 

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;
}

 

The following example deletes a document alias.

 

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;
}