PageRenderTime 90ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/CMSAPIExamples/Code/Settings/Default.aspx.cs

https://bitbucket.org/kudutest2/kenticogit
C# | 428 lines | 218 code | 98 blank | 112 comment | 18 complexity | d513a2ad5a55a4e08507ce20671049db MD5 | raw file
  1. using System;
  2. using System.Data;
  3. using CMS.GlobalHelper;
  4. using CMS.UIControls;
  5. using CMS.CMSHelper;
  6. using CMS.SiteProvider;
  7. using CMS.SettingsProvider;
  8. [Title(Text = "Settings", ImageUrl = "Objects/CMS_SettingsKey/object.png")]
  9. public partial class CMSAPIExamples_Code_Settings_Default : CMSAPIExamplePage
  10. {
  11. #region "Initialization"
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. // Settings category
  15. this.apiCreateSettingsCategory.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(CreateSettingsCategory);
  16. this.apiGetAndUpdateSettingsCategory.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndUpdateSettingsCategory);
  17. this.apiGetAndBulkUpdateSettingsCategories.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndBulkUpdateSettingsCategories);
  18. this.apiDeleteSettingsCategory.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(DeleteSettingsCategory);
  19. // Settings group
  20. this.apiCreateSettingsGroup.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(CreateSettingsGroup);
  21. this.apiGetAndUpdateSettingsGroup.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndUpdateSettingsGroup);
  22. this.apiGetAndBulkUpdateSettingsGroups.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndBulkUpdateSettingsGroups);
  23. this.apiDeleteSettingsGroup.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(DeleteSettingsGroup);
  24. // Settings key
  25. this.apiCreateSettingsKey.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(CreateSettingsKey);
  26. this.apiGetAndUpdateSettingsKey.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndUpdateSettingsKey);
  27. this.apiGetAndBulkUpdateSettingsKeys.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetAndBulkUpdateSettingsKeys);
  28. this.apiDeleteSettingsKey.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(DeleteSettingsKey);
  29. // Web.config setting
  30. this.apiGetWebConfigSetting.RunExample += new CMSAPIExamples_Controls_APIExample.OnRunExample(GetWebConfigSetting);
  31. }
  32. #endregion
  33. #region "Mass actions"
  34. /// <summary>
  35. /// Runs all creating and managing examples.
  36. /// </summary>
  37. public override void RunAll()
  38. {
  39. base.RunAll();
  40. // Settings category
  41. this.apiCreateSettingsCategory.Run();
  42. this.apiGetAndUpdateSettingsCategory.Run();
  43. this.apiGetAndBulkUpdateSettingsCategories.Run();
  44. // Settings group
  45. this.apiCreateSettingsGroup.Run();
  46. this.apiGetAndUpdateSettingsGroup.Run();
  47. this.apiGetAndBulkUpdateSettingsGroups.Run();
  48. // Settings key
  49. this.apiCreateSettingsKey.Run();
  50. this.apiGetAndUpdateSettingsKey.Run();
  51. this.apiGetAndBulkUpdateSettingsKeys.Run();
  52. // Web.config setting
  53. this.apiGetWebConfigSetting.Run();
  54. }
  55. /// <summary>
  56. /// Runs all cleanup examples.
  57. /// </summary>
  58. public override void CleanUpAll()
  59. {
  60. base.CleanUpAll();
  61. // Settings key
  62. this.apiDeleteSettingsKey.Run();
  63. // Settings group
  64. this.apiDeleteSettingsGroup.Run();
  65. // Settings category
  66. this.apiDeleteSettingsCategory.Run();
  67. }
  68. #endregion
  69. #region "API examples - Settings category"
  70. /// <summary>
  71. /// Creates settings category. Called when the "Create category" button is pressed.
  72. /// </summary>
  73. private bool CreateSettingsCategory()
  74. {
  75. // Get parent category ID
  76. SettingsCategoryInfo parentCategory = SettingsCategoryInfoProvider.GetSettingsCategoryInfoByName("CMS.CustomSettings");
  77. if (parentCategory != null)
  78. {
  79. // Create new settings category object
  80. SettingsCategoryInfo newCategory = new SettingsCategoryInfo();
  81. // Set the properties
  82. newCategory.CategoryDisplayName = "My New Settings Category";
  83. newCategory.CategoryName = "MyNewSettingsCategory";
  84. newCategory.CategoryOrder = 0;
  85. newCategory.CategoryParentID = parentCategory.CategoryID;
  86. newCategory.CategoryIsGroup = false;
  87. newCategory.CategoryIsCustom = true;
  88. // Create settings category
  89. SettingsCategoryInfoProvider.SetSettingsCategoryInfo(newCategory);
  90. return true;
  91. }
  92. return false;
  93. }
  94. /// <summary>
  95. /// Gets and updates settings category. Called when the "Get and update category" button is pressed.
  96. /// Expects the CreateSettingsCategory method to be run first.
  97. /// </summary>
  98. private bool GetAndUpdateSettingsCategory()
  99. {
  100. // Get the settings category
  101. SettingsCategoryInfo updateCategory = SettingsCategoryInfoProvider.GetSettingsCategoryInfoByName("MyNewSettingsCategory");
  102. if (updateCategory != null)
  103. {
  104. // Update the property
  105. updateCategory.CategoryDisplayName = updateCategory.CategoryDisplayName.ToLower();
  106. // Update settings category
  107. SettingsCategoryInfoProvider.SetSettingsCategoryInfo(updateCategory);
  108. return true;
  109. }
  110. return false;
  111. }
  112. /// <summary>
  113. /// Gets and bulk updates settings categories. Called when the "Get and bulk update categories" button is pressed.
  114. /// Expects the CreateSettingsCategory method to be run first.
  115. /// </summary>
  116. private bool GetAndBulkUpdateSettingsCategories()
  117. {
  118. // Prepare the parameters
  119. string where = "CategoryName LIKE 'MyNew%' AND CategoryIsGroup = 0";
  120. // Get the settings categories
  121. DataSet categories = SettingsCategoryInfoProvider.GetSettingsCategories(where, null);
  122. if (!DataHelper.DataSourceIsEmpty(categories))
  123. {
  124. // Loop through the individual items
  125. foreach (DataRow categoryDr in categories.Tables[0].Rows)
  126. {
  127. // Create object from DataRow
  128. SettingsCategoryInfo modifyCategory = new SettingsCategoryInfo(categoryDr);
  129. // Update the property
  130. modifyCategory.CategoryDisplayName = modifyCategory.CategoryDisplayName.ToUpper();
  131. // Update the settings category
  132. SettingsCategoryInfoProvider.SetSettingsCategoryInfo(modifyCategory);
  133. }
  134. return true;
  135. }
  136. return false;
  137. }
  138. /// <summary>
  139. /// Deletes settings category. Called when the "Delete category" button is pressed.
  140. /// Expects the CreateSettingsCategory method to be run first.
  141. /// </summary>
  142. private bool DeleteSettingsCategory()
  143. {
  144. // Get the settings category
  145. SettingsCategoryInfo deleteCategory = SettingsCategoryInfoProvider.GetSettingsCategoryInfoByName("MyNewSettingsCategory");
  146. // Delete the settings category
  147. SettingsCategoryInfoProvider.DeleteSettingsCategoryInfo(deleteCategory);
  148. return (deleteCategory != null);
  149. }
  150. #endregion
  151. #region "API examples - Settings group"
  152. /// <summary>
  153. /// Creates settings group. Called when the "Create group" button is pressed.
  154. /// Expects the CreateSettingsCategory method to be run first.
  155. /// </summary>
  156. private bool CreateSettingsGroup()
  157. {
  158. // Get the settings category
  159. SettingsCategoryInfo settingsCategory = SettingsCategoryInfoProvider.GetSettingsCategoryInfoByName("MyNewSettingsCategory");
  160. if (settingsCategory != null)
  161. {
  162. // Create new settings group object
  163. SettingsCategoryInfo newGroup = new SettingsCategoryInfo();
  164. // Set the properties
  165. newGroup.CategoryDisplayName = "My New Settings Group";
  166. newGroup.CategoryName = "MyNewSettingsGroup";
  167. newGroup.CategoryOrder = 0;
  168. newGroup.CategoryParentID = settingsCategory.CategoryID;
  169. newGroup.CategoryIsGroup = true;
  170. newGroup.CategoryIsCustom = true;
  171. // Create the settings group
  172. SettingsCategoryInfoProvider.SetSettingsCategoryInfo(newGroup);
  173. return true;
  174. }
  175. return false;
  176. }
  177. /// <summary>
  178. /// Gets and updates settings group. Called when the "Get and update group" button is pressed.
  179. /// Expects the CreateSettingsGroup method to be run first.
  180. /// </summary>
  181. private bool GetAndUpdateSettingsGroup()
  182. {
  183. // Get the settings group
  184. SettingsCategoryInfo updateGroup = SettingsCategoryInfoProvider.GetSettingsCategoryInfoByName("MyNewSettingsGroup");
  185. if (updateGroup != null)
  186. {
  187. // Update the property
  188. updateGroup.CategoryDisplayName = updateGroup.CategoryDisplayName.ToLower();
  189. // Update the settings group
  190. SettingsCategoryInfoProvider.SetSettingsCategoryInfo(updateGroup);
  191. return true;
  192. }
  193. return false;
  194. }
  195. /// <summary>
  196. /// Gets and bulk updates settings groups. Called when the "Get and bulk update groups" button is pressed.
  197. /// Expects the CreateSettingsGroup method to be run first.
  198. /// </summary>
  199. private bool GetAndBulkUpdateSettingsGroups()
  200. {
  201. // Prepare the parameters
  202. string where = "CategoryName LIKE 'MyNew%' AND CategoryIsGroup = 1";
  203. string orderBy = "CategoryName";
  204. // Get the data
  205. DataSet groups = SettingsCategoryInfoProvider.GetSettingsCategories(where, orderBy);
  206. if (!DataHelper.DataSourceIsEmpty(groups))
  207. {
  208. // Loop through the individual items
  209. foreach (DataRow groupDr in groups.Tables[0].Rows)
  210. {
  211. // Create object from DataRow
  212. SettingsCategoryInfo modifyGroup = new SettingsCategoryInfo(groupDr);
  213. // Update the property
  214. modifyGroup.CategoryDisplayName = modifyGroup.CategoryDisplayName.ToUpper();
  215. // Update settings group
  216. SettingsCategoryInfoProvider.SetSettingsCategoryInfo(modifyGroup);
  217. }
  218. return true;
  219. }
  220. return false;
  221. }
  222. /// <summary>
  223. /// Deletes settings group. Called when the "Delete group" button is pressed.
  224. /// Expects the CreateSettingsGroup method to be run first.
  225. /// </summary>
  226. private bool DeleteSettingsGroup()
  227. {
  228. // Get the settings group
  229. SettingsCategoryInfo deleteGroup = SettingsCategoryInfoProvider.GetSettingsCategoryInfoByName("MyNewSettingsGroup");
  230. // Delete the settings group
  231. SettingsCategoryInfoProvider.DeleteSettingsCategoryInfo(deleteGroup);
  232. return (deleteGroup != null);
  233. }
  234. #endregion
  235. #region "API examples - Settings key"
  236. /// <summary>
  237. /// Creates settings key. Called when the "Create key" button is pressed.
  238. /// </summary>
  239. private bool CreateSettingsKey()
  240. {
  241. // Get the settings group
  242. SettingsCategoryInfo settingsGroup = SettingsCategoryInfoProvider.GetSettingsCategoryInfoByName("MyNewSettingsGroup");
  243. if (settingsGroup != null)
  244. {
  245. // Create new settings key object
  246. SettingsKeyInfo newKey = new SettingsKeyInfo();
  247. // Set the properties
  248. newKey.KeyDisplayName = "My new key";
  249. newKey.KeyName = "MyNewKey";
  250. newKey.KeyDescription = "My new key description";
  251. newKey.KeyType = "string";
  252. newKey.KeyValue = "My new value";
  253. newKey.KeyCategoryID = settingsGroup.CategoryID;
  254. newKey.KeyDefaultValue = null;
  255. // Set Site ID for site specific settings key (for global settings key is default value 0).
  256. newKey.SiteID = CMSContext.CurrentSiteID;
  257. // Create the settings key
  258. SettingsKeyProvider.SetValue(newKey);
  259. return true;
  260. }
  261. return false;
  262. }
  263. /// <summary>
  264. /// Gets and updates settings key. Called when the "Get and update key" button is pressed.
  265. /// Expects the CreateSettingsKey method to be run first.
  266. /// </summary>
  267. private bool GetAndUpdateSettingsKey()
  268. {
  269. // Get the settings key
  270. SettingsKeyInfo updateKey = SettingsKeyProvider.GetSettingsKeyInfo("MyNewKey", CMSContext.CurrentSiteID);
  271. if (updateKey != null)
  272. {
  273. // Update the property
  274. updateKey.KeyDisplayName = updateKey.KeyDisplayName.ToLower();
  275. // Update the settings key
  276. SettingsKeyProvider.SetValue(updateKey);
  277. return true;
  278. }
  279. return false;
  280. }
  281. /// <summary>
  282. /// Gets and bulk updates settings keys. Called when the "Get and bulk update keys" button is pressed.
  283. /// Expects the CreateSettingsKey method to be run first.
  284. /// </summary>
  285. private bool GetAndBulkUpdateSettingsKeys()
  286. {
  287. // Prepare the parameters
  288. string where = "KeyName LIKE N'MyNew%'";
  289. // Get the data
  290. DataSet keys = SettingsKeyProvider.GetSettingsKeys(where, null, 0, null);
  291. if (!DataHelper.DataSourceIsEmpty(keys))
  292. {
  293. // Loop through the individual items
  294. foreach (DataRow keyDr in keys.Tables[0].Rows)
  295. {
  296. // Create object from DataRow
  297. SettingsKeyInfo modifyKey = new SettingsKeyInfo(keyDr);
  298. // Update the property
  299. modifyKey.KeyDisplayName = modifyKey.KeyDisplayName.ToUpper();
  300. // Update the settings key
  301. SettingsKeyProvider.SetValue(modifyKey);
  302. }
  303. return true;
  304. }
  305. return false;
  306. }
  307. /// <summary>
  308. /// Deletes settings key. Called when the "Delete key" button is pressed.
  309. /// Expects the CreateSettingsKey method to be run first.
  310. /// </summary>
  311. private bool DeleteSettingsKey()
  312. {
  313. // Get the settings key
  314. SettingsKeyInfo deleteKey = SettingsKeyProvider.GetSettingsKeyInfo("MyNewKey", CMSContext.CurrentSiteID);
  315. // Delete the settings key
  316. SettingsKeyProvider.DeleteKey(deleteKey);
  317. return (deleteKey != null);
  318. }
  319. #endregion
  320. #region "API examples - Web.config setting"
  321. /// <summary>
  322. /// Gets web.config setting. Called when the button "Get web.config setting" is pressed.
  323. /// </summary>
  324. private bool GetWebConfigSetting()
  325. {
  326. string webConfigSetting = ValidationHelper.GetString(SettingsHelper.AppSettings["WS.webservice"], "");
  327. return (!String.IsNullOrEmpty(webConfigSetting));
  328. }
  329. #endregion
  330. }