How to import Bad words from an external file

   —   
This article describes how to import Bad words from an external file to Kentico CMS application.
The Bad words module can be used as a filter for unwanted input from the website users. This can be anything from rude language to spam or links to illegal content, basically anything that is detectable by presence of some keyword in inputted text.
 
If you want to import Bad words from some other Kentico CMS instance, it is not a problem and you can use built-in Export / Import functionality. In particular, you can export Bad words objects using the Site Manager / Sites / Export objects wizard, and import it using the Import site or objects wizard in the target instance.
 
However, when having the list of Bad words in some external file, you have to import it via API and some basic C# (or VB) code. Sample code in this article describes one possible way of doing that.
 
Please note – sample code in this article supposes that you have a simple list of particular Bad words, like:
 
Sample 1
Sample 2
Sample 3
 
Etc.
 
If your source data has a more complex structure, you may need to parse particular lines, split the values and adjust the code so it works for your particular scenario.
 
Sample code:

protected void ImportBadWords_Click(object sender, EventArgs e)
 {
  //Path to the file containing the list of Bad words
  string path = @"C:\BadWordList.txt";
 
  //Particular Bad word string variable, used later in loop
  string sBadWord = "";
       
  try
   {
    //Determines whether the specified file exists
    if (File.Exists(path))
       {
        //New StreamReader class
        StreamReader sr = new StreamReader(path);
 
        if (sr != null)
         {
          //For every single row in the list, do
          while (!sr.EndOfStream)
           {
            //Read current line
            sBadWord = sr.ReadLine();
 
            //Create the Bad word
            CreateBadWord(sBadWord, BadWordActionEnum.Replace, true, true);
           }
         }
 
             sr.Close();
        }
      }
 
  catch (Exception ex)
   {
    Response.Write(String.Format("Error. Please check the code. Error details: {0}",ex.Message));
   }
}
 
/// <summary>
/// Create BadWordInfo object, according to defined parameters
/// </summary>
/// <param name="sExpression">The string that should not appear in input text</param>
/// <param name="bwEnum">What action should be taken: Remove / Replace / Report abuse / Moderation / Deny  </param>
/// <param name="bGlobal">If checked, the word is global</param>
/// <param name="bRegular">If checked, the string entered into the previous field will searched as a regular expression</param>
/// <returns></returns>
private bool CreateBadWord(string sExpression, BadWordActionEnum bwEnum, bool bGlobal, bool bRegular)
 {
  // Create new bad word object
  BadWordInfo newWord = new BadWordInfo();
 
  // Set the properties
  newWord.WordExpression = sExpression;
  newWord.WordAction = bwEnum;
  newWord.WordIsGlobal = bGlobal;
  newWord.WordIsRegularExpression = bRegular;
 
  // Save the bad word
  BadWordInfoProvider.SetBadWordInfo(newWord);
 
  return true;
 }

 
-rm-


See also: Bad words
Export and import

Applies to: Kentico CMS 6.x
Share this article on   LinkedIn