/CMSWebParts/Sharepoint/SharePointDataGrid.ascx.cs

https://bitbucket.org/kudutest2/kenticogit · C# · 846 lines · 582 code · 119 blank · 145 comment · 20 complexity · 82d857034004fc19eabe1c8a4b733c32 MD5 · raw file

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Xml;
  9. using System.Xml.Xsl;
  10. using CMS.CMSHelper;
  11. using CMS.GlobalHelper;
  12. using CMS.PortalControls;
  13. using CMS.SettingsProvider;
  14. using CMS.Controls;
  15. using CMS.SiteProvider;
  16. public partial class CMSWebParts_SharePoint_SharePointDataGrid : CMSAbstractWebPart
  17. {
  18. #region "SharePoint data source properties"
  19. /// <summary>
  20. /// Gets or sets value of username.
  21. /// </summary>
  22. public string Username
  23. {
  24. get
  25. {
  26. return ValidationHelper.GetString(this.GetValue("Username"), SPDataSource.Username);
  27. }
  28. set
  29. {
  30. this.SetValue("Username", value);
  31. SPDataSource.Username = value;
  32. }
  33. }
  34. /// <summary>
  35. /// Gets or sets value of password.
  36. /// </summary>
  37. public string Password
  38. {
  39. get
  40. {
  41. return ValidationHelper.GetString(this.GetValue("Password"), SPDataSource.Password);
  42. }
  43. set
  44. {
  45. this.SetValue("Password", value);
  46. SPDataSource.Password = value;
  47. }
  48. }
  49. /// <summary>
  50. /// Gets or sets value of SharePoint list name.
  51. /// </summary>
  52. public string ListName
  53. {
  54. get
  55. {
  56. return ValidationHelper.GetString(this.GetValue("ListName"), SPDataSource.ListName);
  57. }
  58. set
  59. {
  60. this.SetValue("ListName", value);
  61. SPDataSource.ListName = value;
  62. }
  63. }
  64. /// <summary>
  65. /// Gets or sets URL of SharePoint service (Eg. Lists.asmx, Imaging.asmx).
  66. /// </summary>
  67. public string SPServiceURL
  68. {
  69. get
  70. {
  71. return ValidationHelper.GetString(this.GetValue("SPServiceURL"), SPDataSource.SPServiceURL);
  72. }
  73. set
  74. {
  75. this.SetValue("SPServiceURL", value);
  76. SPDataSource.SPServiceURL = value;
  77. }
  78. }
  79. /// <summary>
  80. /// Enables or disables showing CAML on output.
  81. /// </summary>
  82. public bool ShowReturnedCAML
  83. {
  84. get
  85. {
  86. return ValidationHelper.GetBoolean(this.GetValue("ShowReturnedCAML"), SPDataSource.ShowReturnedCAML);
  87. }
  88. set
  89. {
  90. this.SetValue("ShowReturnedCAML", value);
  91. SPDataSource.ShowReturnedCAML = value;
  92. }
  93. }
  94. /// <summary>
  95. /// Gets or set the row limit.
  96. /// </summary>
  97. public int RowLimit
  98. {
  99. get
  100. {
  101. return ValidationHelper.GetInteger(this.GetValue("RowLimit"), SPDataSource.RowLimit);
  102. }
  103. set
  104. {
  105. this.SetValue("RowLimit", value);
  106. SPDataSource.RowLimit = value;
  107. }
  108. }
  109. /// <summary>
  110. /// Gets or sets query to specify which document should be retrieved (like where condition).
  111. /// </summary>
  112. public string Query
  113. {
  114. get
  115. {
  116. return ValidationHelper.GetString(this.GetValue("Query"), SPDataSource.Query);
  117. }
  118. set
  119. {
  120. this.SetValue("Query", value);
  121. SPDataSource.Query = value;
  122. }
  123. }
  124. /// <summary>
  125. /// Gets or sets document fields which should be retrieved.
  126. /// </summary>
  127. public string ViewFields
  128. {
  129. get
  130. {
  131. return ValidationHelper.GetString(this.GetValue("ViewFields"), SPDataSource.ViewFields);
  132. }
  133. set
  134. {
  135. this.SetValue("ViewFields", value);
  136. SPDataSource.ViewFields = value;
  137. }
  138. }
  139. /// <summary>
  140. /// Enables or disables using of classic dataset as data source for ASCX transformation.
  141. /// </summary>
  142. public bool UseClassicDataset
  143. {
  144. get
  145. {
  146. return ValidationHelper.GetBoolean(this.GetValue("UseClassicDataset"), SPDataSource.UseClassicDataset);
  147. }
  148. set
  149. {
  150. this.SetValue("UseClassicDataset", value);
  151. SPDataSource.UseClassicDataset = value;
  152. }
  153. }
  154. /// <summary>
  155. /// Gets or sets fields which should be included in dataset
  156. /// Note: Only if UseClassicDataset is enabled
  157. /// </summary>
  158. public string Fields
  159. {
  160. get
  161. {
  162. return ValidationHelper.GetString(this.GetValue("Fields"), SPDataSource.Fields);
  163. }
  164. set
  165. {
  166. this.SetValue("Fields", value);
  167. SPDataSource.Fields = value;
  168. }
  169. }
  170. /// <summary>
  171. /// Gets or sets the mode which specifies what this webpart exactly do.
  172. /// </summary>
  173. public string Mode
  174. {
  175. get
  176. {
  177. return ValidationHelper.GetString(this.GetValue("Mode"), SPDataSource.Mode);
  178. }
  179. set
  180. {
  181. this.SetValue("Mode", value);
  182. SPDataSource.Mode = value;
  183. }
  184. }
  185. /// <summary>
  186. /// Gets or sets the name of field for selecting item.
  187. /// </summary>
  188. public string ItemIDField
  189. {
  190. get
  191. {
  192. return ValidationHelper.GetString(this.GetValue("ItemIDField"), SPDataSource.ItemIDField);
  193. }
  194. set
  195. {
  196. this.SetValue("ItemIDField", value);
  197. SPDataSource.ItemIDField = value;
  198. }
  199. }
  200. /// <summary>
  201. /// Gets or sets the type of field for selecting item.
  202. /// </summary>
  203. public string ItemIDFieldType
  204. {
  205. get
  206. {
  207. return ValidationHelper.GetString(this.GetValue("ItemIDFieldType"), SPDataSource.ItemIDFieldType);
  208. }
  209. set
  210. {
  211. this.SetValue("ItemIDFieldType", value);
  212. SPDataSource.ItemIDFieldType = value;
  213. }
  214. }
  215. #endregion
  216. #region "Basic data grid properties"
  217. /// <summary>
  218. /// Gets or sets HideControlForZeroRows property.
  219. /// </summary>
  220. public bool HideControlForZeroRows
  221. {
  222. get
  223. {
  224. return ValidationHelper.GetBoolean(GetValue("HideControlForZeroRows"), BasicDataGrid.HideControlForZeroRows);
  225. }
  226. set
  227. {
  228. SetValue("HideControlForZeroRows", value);
  229. BasicDataGrid.HideControlForZeroRows = value;
  230. }
  231. }
  232. /// <summary>
  233. /// Gets or sets ZeroRowsText property.
  234. /// </summary>
  235. public string ZeroRowsText
  236. {
  237. get
  238. {
  239. return ValidationHelper.GetString(GetValue("ZeroRowsText"), BasicDataGrid.ZeroRowsText);
  240. }
  241. set
  242. {
  243. SetValue("ZeroRowsText", value);
  244. BasicDataGrid.ZeroRowsText = value;
  245. }
  246. }
  247. /// <summary>
  248. /// Gets or sets the value that inidcates whether header will be displayed.
  249. /// </summary>
  250. public bool ShowHeader
  251. {
  252. get
  253. {
  254. return ValidationHelper.GetBoolean(GetValue("ShowHeader"), BasicDataGrid.ShowHeader);
  255. }
  256. set
  257. {
  258. SetValue("ShowHeader", value);
  259. BasicDataGrid.ShowHeader = value;
  260. }
  261. }
  262. /// <summary>
  263. /// Gets or sets the value that indicates whether footer will be displayed.
  264. /// </summary>
  265. public bool ShowFooter
  266. {
  267. get
  268. {
  269. return ValidationHelper.GetBoolean(GetValue("ShowFooter"), BasicDataGrid.ShowFooter);
  270. }
  271. set
  272. {
  273. SetValue("ShowFooter", value);
  274. BasicDataGrid.ShowFooter = value;
  275. }
  276. }
  277. /// <summary>
  278. /// Gets or sets the tool tip text.
  279. /// </summary>
  280. public string ToolTip
  281. {
  282. get
  283. {
  284. return DataHelper.GetNotEmpty(GetValue("ToolTip"), BasicDataGrid.ToolTip);
  285. }
  286. set
  287. {
  288. SetValue("ToolTip", value);
  289. BasicDataGrid.ToolTip = value;
  290. }
  291. }
  292. /// <summary>
  293. /// Gets or sets the value that indicates whether first page is sets when sort of some column is changed.
  294. /// </summary>
  295. public bool SetFirstPageAfterSortChange
  296. {
  297. get
  298. {
  299. return ValidationHelper.GetBoolean(GetValue("SetFirstPageAfterSortChange"), BasicDataGrid.SetFirstPageAfterSortChange);
  300. }
  301. set
  302. {
  303. SetValue("SetFirstPageAfterSortChange", value);
  304. BasicDataGrid.SetFirstPageAfterSortChange = value;
  305. }
  306. }
  307. /// <summary>
  308. /// Gets or sets names of columns which will be used in header.
  309. /// </summary>
  310. public string HeaderColumnsNames
  311. {
  312. get
  313. {
  314. return DataHelper.GetNotEmpty(GetValue("HeaderColumnsNames"), "");
  315. }
  316. set
  317. {
  318. SetValue("HeaderColumnsNames", value);
  319. }
  320. }
  321. /// <summary>
  322. /// Gets or sets the SkinID which should be used.
  323. /// </summary>
  324. public override string SkinID
  325. {
  326. get
  327. {
  328. return base.SkinID;
  329. }
  330. set
  331. {
  332. base.SkinID = value;
  333. if ((BasicDataGrid != null) && (PageCycle < PageCycleEnum.Initialized))
  334. {
  335. BasicDataGrid.SkinID = SkinID;
  336. }
  337. }
  338. }
  339. #endregion
  340. #region "Basic data grid paging properties"
  341. /// <summary>
  342. /// Gets or sets the value that indicates whether paging will be allowed.
  343. /// </summary>
  344. public bool AllowPaging
  345. {
  346. get
  347. {
  348. return ValidationHelper.GetBoolean(GetValue("AllowPaging"), BasicDataGrid.AllowPaging);
  349. }
  350. set
  351. {
  352. SetValue("AllowPaging", value);
  353. BasicDataGrid.AllowPaging = value;
  354. }
  355. }
  356. /// <summary>
  357. /// Gets or sets the value that indicates whether custom paging is enabled.
  358. /// </summary>
  359. public bool AllowCustomPaging
  360. {
  361. get
  362. {
  363. return ValidationHelper.GetBoolean(GetValue("AllowCustomPaging"), BasicDataGrid.AllowCustomPaging);
  364. }
  365. set
  366. {
  367. SetValue("AllowCustomPaging", value);
  368. BasicDataGrid.AllowCustomPaging = value;
  369. }
  370. }
  371. /// <summary>
  372. /// Gets or sets the size of the page if the paging is allowed.
  373. /// </summary>
  374. public int PageSize
  375. {
  376. get
  377. {
  378. return ValidationHelper.GetInteger(GetValue("PageSize"), BasicDataGrid.PageSize);
  379. }
  380. set
  381. {
  382. SetValue("PageSize", value);
  383. BasicDataGrid.PageSize = value;
  384. }
  385. }
  386. /// <summary>
  387. /// Gets or sets the paging mode.
  388. /// </summary>
  389. public PagerMode PagingMode
  390. {
  391. get
  392. {
  393. return BasicDataGrid.GetPagerMode(ValidationHelper.GetString(GetValue("PagingMode"), ""));
  394. }
  395. set
  396. {
  397. SetValue("PagingMode", value);
  398. }
  399. }
  400. #endregion
  401. #region "Basic data grid sorting properties"
  402. /// <summary>
  403. /// Gets or sets the value that indicates whether sorting is ascending at default.
  404. /// </summary>
  405. public bool SortAscending
  406. {
  407. get
  408. {
  409. return (ValidationHelper.GetBoolean(GetValue("SortAscending"), BasicDataGrid.SortAscending));
  410. }
  411. set
  412. {
  413. SetValue("SortAscending", value);
  414. BasicDataGrid.SortAscending = value;
  415. }
  416. }
  417. /// <summary>
  418. /// Gets or sets the value that indicates whether sorting is allowed.
  419. /// </summary>
  420. public bool AllowSorting
  421. {
  422. get
  423. {
  424. return ValidationHelper.GetBoolean(GetValue("AllowSorting"), BasicDataGrid.AllowSorting);
  425. }
  426. set
  427. {
  428. SetValue("AllowSorting", value);
  429. BasicDataGrid.AllowSorting = value;
  430. }
  431. }
  432. /// <summary>
  433. /// Gets or sets the value that inidcates whether sorting process is proceeded in the code.
  434. /// </summary>
  435. public bool ProcessSorting
  436. {
  437. get
  438. {
  439. return ValidationHelper.GetBoolean(GetValue("ProcessSorting"), BasicDataGrid.ProcessSorting);
  440. }
  441. set
  442. {
  443. SetValue("ProcessSorting", value);
  444. BasicDataGrid.ProcessSorting = value;
  445. }
  446. }
  447. /// <summary>
  448. /// Gets or sets the default sort field.
  449. /// </summary>
  450. public string SortField
  451. {
  452. get
  453. {
  454. return DataHelper.GetNotEmpty(ValidationHelper.GetString(GetValue("SortField"), BasicDataGrid.SortField), BasicDataGrid.SortField);
  455. }
  456. set
  457. {
  458. SetValue("SortField", value);
  459. BasicDataGrid.SortField = value;
  460. }
  461. }
  462. #endregion
  463. #region "Overridden properties"
  464. /// <summary>
  465. /// Gets or sets the cache item name.
  466. /// </summary>
  467. public override string CacheItemName
  468. {
  469. get
  470. {
  471. return base.CacheItemName;
  472. }
  473. set
  474. {
  475. base.CacheItemName = value;
  476. SPDataSource.CacheItemName = value;
  477. }
  478. }
  479. /// <summary>
  480. /// Cache dependencies, each cache dependency on a new line.
  481. /// </summary>
  482. public override string CacheDependencies
  483. {
  484. get
  485. {
  486. return ValidationHelper.GetString(base.CacheDependencies, SPDataSource.CacheDependencies);
  487. }
  488. set
  489. {
  490. base.CacheDependencies = value;
  491. SPDataSource.CacheDependencies = value;
  492. }
  493. }
  494. /// <summary>
  495. /// Gets or sets the cache minutes.
  496. /// </summary>
  497. public override int CacheMinutes
  498. {
  499. get
  500. {
  501. return base.CacheMinutes;
  502. }
  503. set
  504. {
  505. base.CacheMinutes = value;
  506. SPDataSource.CacheMinutes = value;
  507. }
  508. }
  509. /// <summary>
  510. /// Indicates if the control should perform the operations.
  511. /// </summary>
  512. public override bool StopProcessing
  513. {
  514. get
  515. {
  516. return base.StopProcessing;
  517. }
  518. set
  519. {
  520. base.StopProcessing = value;
  521. this.SPDataSource.StopProcessing = value;
  522. }
  523. }
  524. #endregion
  525. #region "Methods"
  526. /// <summary>
  527. /// Content loaded event handler.
  528. /// </summary>
  529. public override void OnContentLoaded()
  530. {
  531. base.OnContentLoaded();
  532. SetupControl();
  533. }
  534. /// <summary>
  535. /// Initializes the control properties.
  536. /// </summary>
  537. protected void SetupControl()
  538. {
  539. if (this.StopProcessing)
  540. {
  541. // Do nothing
  542. }
  543. else
  544. {
  545. // Classic repeater+dataset
  546. if (this.UseClassicDataset)
  547. {
  548. // Set SharePoint datasource properties
  549. SPDataSource.Username = this.Username;
  550. SPDataSource.Password = EncryptionHelper.DecryptData(this.Password);
  551. SPDataSource.ListName = this.ListName;
  552. SPDataSource.SPServiceURL = this.SPServiceURL;
  553. SPDataSource.ShowReturnedCAML = this.ShowReturnedCAML;
  554. SPDataSource.RowLimit = this.RowLimit;
  555. SPDataSource.Query = this.Query;
  556. SPDataSource.ViewFields = this.ViewFields;
  557. SPDataSource.UseClassicDataset = this.UseClassicDataset;
  558. SPDataSource.Fields = this.Fields;
  559. SPDataSource.Mode = this.Mode;
  560. SPDataSource.ItemIDField = this.ItemIDField;
  561. SPDataSource.ItemIDFieldType = this.ItemIDFieldType;
  562. SPDataSource.CacheItemName = this.CacheItemName;
  563. SPDataSource.CacheDependencies = this.CacheDependencies;
  564. SPDataSource.CacheMinutes = this.CacheMinutes;
  565. // Set basic datagrid properties
  566. BasicDataGrid.DataBindByDefault = false;
  567. BasicDataGrid.EnableViewState = true;
  568. BasicDataGrid.HideControlForZeroRows = HideControlForZeroRows;
  569. BasicDataGrid.ZeroRowsText = ZeroRowsText;
  570. if (!StandAlone && (PageCycle < PageCycleEnum.Initialized) && (ValidationHelper.GetString(Page.StyleSheetTheme, "") == ""))
  571. {
  572. BasicDataGrid.SkinID = SkinID;
  573. }
  574. BasicDataGrid.SortAscending = SortAscending;
  575. BasicDataGrid.AllowSorting = AllowSorting;
  576. BasicDataGrid.ProcessSorting = ProcessSorting;
  577. BasicDataGrid.SortField = SortField;
  578. BasicDataGrid.AllowPaging = AllowPaging;
  579. BasicDataGrid.AllowCustomPaging = AllowCustomPaging;
  580. BasicDataGrid.PageSize = PageSize;
  581. BasicDataGrid.PagerStyle.Mode = PagingMode;
  582. BasicDataGrid.ShowHeader = ShowHeader;
  583. BasicDataGrid.ShowFooter = ShowFooter;
  584. BasicDataGrid.ToolTip = ToolTip;
  585. BasicDataGrid.SetFirstPageAfterSortChange = SetFirstPageAfterSortChange;
  586. // Load and bind data
  587. LoadData();
  588. }
  589. else
  590. {
  591. throw new Exception("XSLT transformation not supported in this webpart. Use SharePoint repeater.");
  592. }
  593. }
  594. }
  595. /// <summary>
  596. /// Reload data.
  597. /// </summary>
  598. public override void ReloadData()
  599. {
  600. base.ReloadData();
  601. SetupControl();
  602. BindData();
  603. }
  604. /// <summary>
  605. /// OnPreRender override.
  606. /// </summary>
  607. protected override void OnPreRender(EventArgs e)
  608. {
  609. if (!string.IsNullOrEmpty(SPDataSource.ErrorMessage))
  610. {
  611. BasicDataGrid.Visible = false;
  612. ShowError(SPDataSource.ErrorMessage);
  613. }
  614. else
  615. {
  616. //DataSet
  617. if (this.UseClassicDataset && this.HideControlForZeroRows && !SPDataSource.HasData)
  618. {
  619. this.Visible = false;
  620. }
  621. }
  622. base.OnPreRender(e);
  623. }
  624. /// <summary>
  625. /// OnLoad override.
  626. /// </summary>
  627. protected override void OnLoad(EventArgs e)
  628. {
  629. base.OnLoad(e);
  630. BindData();
  631. }
  632. /// <summary>
  633. /// Loads SharePoint data.
  634. /// </summary>
  635. protected void LoadData()
  636. {
  637. // Connect data grid with data source
  638. BasicDataGrid.DataSource = SPDataSource.DataSource;
  639. }
  640. /// <summary>
  641. /// Binds data to BasicDataGrid.
  642. /// </summary>
  643. protected void BindData()
  644. {
  645. // Create columns
  646. InitColumns(this.Fields, this.HeaderColumnsNames);
  647. if (!DataHelper.DataSourceIsEmpty(BasicDataGrid.DataSource))
  648. {
  649. try
  650. {
  651. BasicDataGrid.DataBind();
  652. }
  653. catch (Exception ex)
  654. {
  655. ShowError(string.Format(GetString("sharepoint.DataBindError"), ex.Message));
  656. }
  657. }
  658. }
  659. /// <summary>
  660. /// Bounds grid columns.
  661. /// </summary>
  662. /// <param name="columns">Input string with column name separated by semicolon</param>
  663. /// <param name="headerNames">Header columns names</param>
  664. protected void InitColumns(string columns, string headerNames)
  665. {
  666. // There are already some bounded columns
  667. if (BasicDataGrid.Columns.Count > 0)
  668. {
  669. return;
  670. }
  671. if (String.IsNullOrEmpty(columns))
  672. {
  673. BasicDataGrid.AutoGenerateColumns = true;
  674. return;
  675. }
  676. // Custom columns will be used
  677. BasicDataGrid.AutoGenerateColumns = false;
  678. string[] cols = columns.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  679. string[] headerCols = headerNames.Split(new char[] { ';' });
  680. int i = 0;
  681. foreach (string col in cols)
  682. {
  683. string currentCol = col.Trim();
  684. if (!string.IsNullOrEmpty(currentCol))
  685. {
  686. BoundColumn column = new BoundColumn();
  687. column.DataField = currentCol;
  688. // Set header
  689. if ((headerCols.Length > i) && !String.IsNullOrEmpty(headerCols[i]))
  690. {
  691. column.HeaderText = ResHelper.LocalizeString(headerCols[i]);
  692. }
  693. else
  694. {
  695. // Use dataset column name
  696. column.HeaderText = col;
  697. }
  698. if (BasicDataGrid.AllowSorting)
  699. {
  700. column.SortExpression = currentCol;
  701. }
  702. BasicDataGrid.Columns.Add(column);
  703. i++;
  704. }
  705. }
  706. }
  707. /// <summary>
  708. /// Applies given stylesheet skin.
  709. /// </summary>
  710. public override void ApplyStyleSheetSkin(Page page)
  711. {
  712. BasicDataGrid.SkinID = SkinID;
  713. base.ApplyStyleSheetSkin(page);
  714. }
  715. /// <summary>
  716. /// Shows error with message.
  717. /// </summary>
  718. /// <param name="message">Message</param>
  719. private void ShowError(string message)
  720. {
  721. Label lblError = new Label();
  722. lblError.Text = message;
  723. lblError.CssClass = "ErrorLabel";
  724. Controls.Add(lblError);
  725. }
  726. #endregion
  727. }