PageRenderTime 28ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/TST.ContentByType/TST.WebParts/ContentByType/Editor/ContentTypeConfiguration.cs

#
C# | 282 lines | 250 code | 28 blank | 4 comment | 54 complexity | a0750e1e57c9a8af7a1ee1b61052c701 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Web.UI;
  5. using System.Web.UI.WebControls;
  6. using Microsoft.SharePoint;
  7. using TST.SharePoint.Shared;
  8. using System.Xml;
  9. using TST.WebParts.Shared.WebControls;
  10. namespace TST.WebParts
  11. {
  12. public class ContentTypeConfiguration : ConfigurationControl
  13. {
  14. protected Panel panelDatasource;
  15. protected Table tableDatasource;
  16. protected Label labelContentTypeGroup;
  17. protected SortDropDownList selectContentTypeGroup;
  18. protected Label labelContentType;
  19. protected SortDropDownList selectContentType;
  20. protected Label labelScope;
  21. protected RadioButtonList selectScope;
  22. protected Label labelincludeDescending;
  23. protected CheckBox cbkIncludeDescending;
  24. protected Label labelSelectListType;
  25. protected SortDropDownList selectListType;
  26. protected Label labelDisplayOption;
  27. protected Label labelJustSearchOnQuery;
  28. protected CheckBox cbkJustSearchOnQuery;
  29. protected CheckBox cbkFilterInverse;
  30. private Exception _exception;
  31. public event EventHandler SelectedContentTypeChanged;
  32. private string[] _supportedHiddenTypes = new string[]
  33. {
  34. "0x010108", // Wiki Page
  35. "0x0102", // Event"
  36. "0x010801", // Workflow Task
  37. "0x0109", // Workflow History
  38. "0x0110", // Post
  39. "0x0111" // Comment
  40. };
  41. protected void selectContentTypeGroup_SelectedIndexChanged(object sender, EventArgs e)
  42. {
  43. try
  44. {
  45. SelectedGroup = selectContentTypeGroup.SelectedValue;
  46. selectContentType.SelectedValue = "-1";
  47. WriteSelectedContentType();
  48. Datasource.ReadConfiguration(CurrentConfiguration);
  49. if (SelectedContentTypeChanged != null)
  50. {
  51. SelectedContentTypeChanged(this, new EventArgs());
  52. }
  53. }
  54. catch (Exception ex)
  55. {
  56. _exception = ex;
  57. }
  58. }
  59. private string SelectedGroup
  60. {
  61. get { return (string)(ViewState["selectedGroup"] ?? string.Empty); }
  62. set { ViewState["selectedGroup"] = value; }
  63. }
  64. protected void selectContentType_SelectedIndexChanged(object sender, EventArgs e)
  65. {
  66. try
  67. {
  68. if (Datasource != null && Datasource is DatasourceContentType)
  69. {
  70. string saveName = ((DatasourceContentType)Datasource).ContentTypeName;
  71. if (((DatasourceContentType)Datasource).ContentTypeName != selectContentType.SelectedValue)
  72. {
  73. WriteSelectedContentType();
  74. WriteConfiguration();
  75. Datasource.ReadConfiguration(CurrentConfiguration);
  76. if (SelectedContentTypeChanged != null)
  77. {
  78. SelectedContentTypeChanged(this, new EventArgs());
  79. }
  80. }
  81. }
  82. }
  83. catch (Exception ex)
  84. {
  85. _exception = ex;
  86. }
  87. }
  88. private void WriteSelectedContentType()
  89. {
  90. // Check if a property is available for the selected content type.
  91. string s = selectContentType.SelectedValue;
  92. if (!CurrentConfiguration.CustomProperties.Contains("contenttype"))
  93. CurrentConfiguration.CustomProperties.Add(new CustomProperty("contenttype", selectContentType.SelectedValue));
  94. else
  95. CurrentConfiguration.CustomProperties["contenttype"].Value = selectContentType.SelectedValue;
  96. }
  97. public void WriteConfiguration()
  98. {
  99. try
  100. {
  101. if (CurrentConfiguration == null)
  102. {
  103. return;
  104. }
  105. // Check if a property is available for include descending.
  106. if (!CurrentConfiguration.CustomProperties.Contains("includedescending"))
  107. CurrentConfiguration.CustomProperties.Add(new CustomProperty("includedescending", cbkIncludeDescending.Checked ? "true" : "false"));
  108. else
  109. CurrentConfiguration.CustomProperties["includedescending"].Value = cbkIncludeDescending.Checked ? "true" : "false";
  110. if (!string.IsNullOrEmpty(selectScope.SelectedValue))
  111. {
  112. // Get the scope and check if a property is available for the scope.
  113. if (!CurrentConfiguration.CustomProperties.Contains("scope"))
  114. CurrentConfiguration.CustomProperties.Add(new CustomProperty("scope", selectScope.SelectedValue));
  115. else
  116. CurrentConfiguration.CustomProperties["scope"].Value = selectScope.SelectedValue;
  117. }
  118. if (!CurrentConfiguration.CustomProperties.Contains("listservertemplate"))
  119. CurrentConfiguration.CustomProperties.Add(new CustomProperty("listservertemplate", selectListType.SelectedValue));
  120. else
  121. CurrentConfiguration.CustomProperties["listservertemplate"].Value = selectListType.SelectedValue;
  122. CurrentConfiguration.JustSearchOnQuery = cbkJustSearchOnQuery.Checked;
  123. CurrentConfiguration.FilterInverse = cbkFilterInverse.Checked;
  124. }
  125. catch (Exception ex)
  126. {
  127. _exception = ex;
  128. }
  129. }
  130. private bool HiddenTypeSupported(SPContentType ct)
  131. {
  132. for (int i = 0; i < _supportedHiddenTypes.Length; i++)
  133. if (ct.Id.ToString().ToLower().StartsWith(_supportedHiddenTypes[i].ToLower()))
  134. return true;
  135. return false;
  136. }
  137. protected override void OnPreRender(EventArgs e)
  138. {
  139. base.OnPreRender(e);
  140. if (Web == null)
  141. return;
  142. try
  143. {
  144. string selectedContentType = selectContentType.SelectedValue;
  145. if ((string.IsNullOrEmpty(selectedContentType) || selectedContentType == "-1") && CurrentConfiguration.CustomProperties.Contains("contenttype"))
  146. selectedContentType = CurrentConfiguration.CustomProperties["contenttype"].Value;
  147. if (string.IsNullOrEmpty(SelectedGroup) && !string.IsNullOrEmpty(selectedContentType) && selectedContentType != "-1")
  148. {
  149. foreach (SPContentType contentType in Web.AvailableContentTypes)
  150. {
  151. if (string.Compare(contentType.Name, selectedContentType, true) == 0)
  152. {
  153. SelectedGroup = contentType.Group;
  154. break;
  155. }
  156. }
  157. }
  158. selectContentType.Items.Clear();
  159. selectContentType.Items.Add(new ListItem("-- Select a content type --", "-1"));
  160. selectContentTypeGroup.Items.Clear();
  161. selectContentTypeGroup.Items.Add(new ListItem("-- Select a group --", "-1"));
  162. for (int i = 0; i < Web.AvailableContentTypes.Count; i++)
  163. {
  164. SPContentType ct = Web.AvailableContentTypes[i];
  165. if ((!ct.Hidden && ct.Group != "_Hidden") || HiddenTypeSupported(ct))
  166. {
  167. ListItem group = new ListItem(ct.Group);
  168. if (!selectContentTypeGroup.Items.Contains(group))
  169. selectContentTypeGroup.Items.Add(group);
  170. if (SelectedGroup == ct.Group)
  171. {
  172. ListItem newCT = new ListItem(ct.Name, ct.Name);
  173. selectContentType.Items.Add(newCT);
  174. newCT.Selected = (string.Compare(selectedContentType, newCT.Value, true) == 0);
  175. }
  176. }
  177. }
  178. if (!string.IsNullOrEmpty(SelectedGroup))
  179. selectContentTypeGroup.SelectedValue = SelectedGroup;
  180. // Populate dropdownlist with list types.
  181. string valueListType = selectListType.SelectedValue;
  182. selectListType.Items.Clear();
  183. selectListType.Items.Add(new ListItem("-- Select a list template --", "-1"));
  184. using (SPWeb root = Web.Site.RootWeb)
  185. {
  186. SPListTemplateCollection templates = root.ListTemplates;
  187. foreach (SPListTemplate template in templates)
  188. {
  189. string templateID = template.Name;
  190. System.Xml.XmlDocument schema = new XmlDocument();
  191. schema.LoadXml(template.SchemaXml);
  192. XmlAttribute typeAttribute = schema.DocumentElement.Attributes["Type"];
  193. if (typeAttribute != null)
  194. templateID = typeAttribute.InnerXml;
  195. selectListType.Items.Add(new ListItem(template.Name, templateID));
  196. }
  197. }
  198. selectListType.SelectedValue = valueListType;
  199. if (!Page.IsPostBack)
  200. {
  201. ReadConfiguration();
  202. }
  203. }
  204. catch (Exception ex)
  205. {
  206. _exception = ex;
  207. }
  208. }
  209. public void ReadConfiguration()
  210. {
  211. try
  212. {
  213. if (CurrentConfiguration == null)
  214. {
  215. return;
  216. }
  217. if (CurrentConfiguration.CustomProperties.Contains("contenttype"))
  218. {
  219. selectContentType.SelectedValue = CurrentConfiguration.CustomProperties["contenttype"].Value;
  220. }
  221. cbkIncludeDescending.Checked = (CurrentConfiguration.CustomProperties.Contains("includedescending") && CurrentConfiguration.CustomProperties["includedescending"].Value.ToLower() == "true");
  222. if (CurrentConfiguration.CustomProperties.Contains("scope"))
  223. selectScope.SelectedValue = CurrentConfiguration.CustomProperties["scope"].Value;
  224. if (CurrentConfiguration.CustomProperties.Contains("listservertemplate"))
  225. selectListType.SelectedValue = CurrentConfiguration.CustomProperties["listservertemplate"].Value;
  226. cbkJustSearchOnQuery.Checked = CurrentConfiguration.JustSearchOnQuery;
  227. cbkFilterInverse.Checked = CurrentConfiguration.FilterInverse;
  228. }
  229. catch (Exception ex)
  230. {
  231. _exception = ex;
  232. }
  233. }
  234. protected override void Render(HtmlTextWriter writer)
  235. {
  236. try
  237. {
  238. base.Render(writer);
  239. }
  240. catch (Exception ex)
  241. {
  242. _exception = ex;
  243. }
  244. if (_exception != null)
  245. {
  246. writer.WriteLine(string.Format("Error: {0} - {1}.", _exception.Message, _exception.StackTrace));
  247. if (_exception.InnerException != null)
  248. {
  249. writer.WriteLine(string.Format("Error: {0} - {1}.", _exception.InnerException.Message, _exception.InnerException.StackTrace));
  250. }
  251. }
  252. }
  253. }
  254. }