using System; using System.ComponentModel; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using Microsoft.SharePoint; using Microsoft.SharePoint.WebControls; using SPEduTech.Core; using System.Collections.Generic; using System.Data; using SPEduTech.Core.DomainObjects; using System.Web.UI.HtmlControls; using Microsoft.SharePoint.Utilities; namespace SPEduTech.WebParts { [ToolboxItemAttribute(false)] public class DocumentViewer : CoreWebPart { protected override string WebPartSecret { get { return "0hIest#E"; } } #region WebPart Properties private string dmsSiteURL; private DMSContentTypes displayContentType; private int displayNumberItems = 5; private Dictionary columnDetails = new Dictionary() { { "Title", string.Format("{0}{1,10}{2:000}{3}", 1, "Eq", 0, string.Empty) } }; private KeyValuePair sortOrder = new KeyValuePair("Title", true); [WebBrowsable(true), Category("SPEduTech Settings"), WebDisplayName("DMS Site URL"), WebDescription("The site url that contains the DMS"), Personalizable(PersonalizationScope.Shared)] public string DMSSiteURL { get { return dmsSiteURL; } set { dmsSiteURL = value; } } [WebBrowsable(true), Category("SPEduTech Settings"), WebDisplayName("Display Content Type"), WebDescription("The type of document to display"), Personalizable(PersonalizationScope.Shared)] public DMSContentTypes DisplayContentType { get { return displayContentType; } set { displayContentType = value; try { List InvalidColumns = new List() { "ContentType", "SelectFilename", "FileLeafRef", "_dlc_DocId", "_dlc_DocIdUrl", "_dlc_DocIdPersistId", "AverageRating", "RatingCount", }; Dictionary temp = new Dictionary(); int i = 1; manageSPWeb = new ManageSPWeb(DMSSiteURL); foreach (SPField field in manageSPWeb.GetList("Documents").ContentTypes[displayContentType.ToString()].Fields) { if (field.InternalName.Equals("Title")) { temp.Add(field.InternalName, string.Format("{0}{1,10}{2:000}{3}", 1, "Eq", 0, string.Empty)); } else if (!InvalidColumns.Contains(field.InternalName)) { temp.Add(field.InternalName, string.Format("{0}{1,10}{2:000}{3}", 0, "Eq", i++, string.Empty)); } } ColumnDetails = temp; } catch { } } } [WebBrowsable(true), Category("SPEduTech Settings"), WebDisplayName("Display Number of Items"), WebDescription("Default number of items to display"), Personalizable(PersonalizationScope.Shared)] public int DisplayNumberItems { get { return displayNumberItems; } set { displayNumberItems = value; } } [WebBrowsable(false), Category("SPEduTech Settings"), WebDisplayName("Column Details"), WebDescription("Display And Filter Value for Columns"), Personalizable(PersonalizationScope.Shared)] public Dictionary ColumnDetails { get { return columnDetails; } set { columnDetails = value; } } [WebBrowsable(false), Category("SPEduTech Settings"), WebDisplayName("Column Details"), WebDescription("Display And Filter Value for Columns"), Personalizable(PersonalizationScope.Shared)] public KeyValuePair SortOrder { get { return sortOrder; } set { sortOrder = value; } } #endregion Dictionary displayColumns = new Dictionary(); Dictionary filterType = new Dictionary(); Dictionary filterColumns = new Dictionary(); Dictionary displayOrder = new Dictionary(); DropDownList ddlSortColumn; CheckBox cbxSortAscending; string dmsLinkFilter = string.Empty; protected override void CreateDesignTimeControls() { Table tblFilter = new Table(); TableRow tblFilterRow = new TableHeaderRow(); tblFilterRow.TableSection = TableRowSection.TableHeader; TableCell tblFilterCell = new TableHeaderCell(); tblFilterCell.Text = "Column Name"; tblFilterRow.Cells.Add(tblFilterCell); tblFilterCell = new TableHeaderCell(); tblFilterCell.Text = "Display"; tblFilterRow.Cells.Add(tblFilterCell); tblFilterCell = new TableHeaderCell(); tblFilterCell.Text = "Filter Type"; tblFilterRow.Cells.Add(tblFilterCell); tblFilterCell = new TableHeaderCell(); tblFilterCell.Text = "Filter Value"; tblFilterCell.ToolTip = ""; tblFilterRow.Cells.Add(tblFilterCell); tblFilterCell = new TableHeaderCell(); tblFilterCell.Text = "Display Order"; tblFilterRow.Cells.Add(tblFilterCell); tblFilter.Rows.Add(tblFilterRow); foreach (KeyValuePair column in columnDetails) { tblFilterRow = new TableRow(); tblFilterCell = new TableCell(); tblFilterCell.Text = column.Key; tblFilterRow.Cells.Add(tblFilterCell); tblFilterCell = new TableCell(); CheckBox cbxDisplay = new CheckBox(); cbxDisplay.ID = "cbxDisplay" + column.Key; cbxDisplay.AutoPostBack = false; if (column.Key.Equals("Title")) { cbxDisplay.Checked = true; cbxDisplay.Enabled = false; } else { cbxDisplay.Checked = column.Value.Substring(0, 1).Equals("1"); } displayColumns.Add(column.Key, cbxDisplay); tblFilterCell.Controls.Add(cbxDisplay); tblFilterRow.Cells.Add(tblFilterCell); tblFilterCell = new TableCell(); DropDownList ddlFilterType = new DropDownList(); ddlFilterType.ID = "ddlFilterType" + column.Key; ddlFilterType.AutoPostBack = false; /*Items Adding*/ ddlFilterType.Items.Add(new ListItem("Equals", "Eq")); ddlFilterType.Items.Add(new ListItem("Not Equal", "Neq")); ddlFilterType.Items.Add(new ListItem("Greater Than", "Gt")); ddlFilterType.Items.Add(new ListItem("Less Than", "Geq")); ddlFilterType.Items.Add(new ListItem("Greater Than Or Equal To", "Lt")); ddlFilterType.Items.Add(new ListItem("Less Than Or Equal To", "Leq")); ddlFilterType.Items.Add(new ListItem("Is Null", "IsNull")); ddlFilterType.Items.Add(new ListItem("Is Not Null", "IsNotNull")); ddlFilterType.Items.Add(new ListItem("Begins With", "BeginsWith")); ddlFilterType.Items.Add(new ListItem("Contains", "Contains")); /*Items Added*/ ddlFilterType.SelectedValue = column.Value.Substring(1, 10).Trim(); filterType.Add(column.Key, ddlFilterType); tblFilterCell.Controls.Add(ddlFilterType); tblFilterRow.Cells.Add(tblFilterCell); tblFilterCell = new TableCell(); TextBox txtFilterValue = new TextBox(); txtFilterValue.ID = "txtFiltreValue" + column.Key; txtFilterValue.AutoCompleteType = AutoCompleteType.None; txtFilterValue.AutoPostBack = false; if (column.Value.Length > 14) { txtFilterValue.Text = column.Value.Substring(14); } else { txtFilterValue.Text = string.Empty; } filterColumns.Add(column.Key, txtFilterValue); tblFilterCell.Controls.Add(txtFilterValue); tblFilterRow.Cells.Add(tblFilterCell); tblFilterCell = new TableCell(); TextBox txtDisplayOder = new TextBox(); txtDisplayOder.ID = "txtDisplayOder" + column.Key; txtDisplayOder.AutoCompleteType = AutoCompleteType.None; txtDisplayOder.AutoPostBack = false; txtDisplayOder.Text = column.Value.Substring(11, 3); displayOrder.Add(column.Key, txtDisplayOder); tblFilterCell.Controls.Add(txtDisplayOder); tblFilterRow.Cells.Add(tblFilterCell); tblFilter.Rows.Add(tblFilterRow); } pnlWebPartContainer.Controls.Add(tblFilter); Table tblSort = new Table(); TableRow tblSortRow = new TableHeaderRow(); TableCell tblSortCell = new TableHeaderCell(); tblSortCell.Text = "Sort Column"; tblSortRow.Cells.Add(tblSortCell); tblSortCell = new TableCell(); ddlSortColumn = new DropDownList(); foreach (string column in columnDetails.Keys) { ddlSortColumn.Items.Add(column); } tblSortCell.Controls.Add(ddlSortColumn); tblSortRow.Cells.Add(tblSortCell); tblSort.Rows.Add(tblSortRow); tblSortRow = new TableRow(); tblSortCell = new TableHeaderCell(); tblSortCell.Text = "Sort Ascending"; tblSortRow.Cells.Add(tblSortCell); tblSortCell = new TableCell(); cbxSortAscending = new CheckBox(); cbxSortAscending.Checked = true; tblSortCell.Controls.Add(cbxSortAscending); tblSortRow.Cells.Add(tblSortCell); tblSort.Rows.Add(tblSortRow); pnlWebPartContainer.Controls.Add(tblSort); Button btnUpdate = new Button(); btnUpdate.ID = "btnUpdateFilter"; btnUpdate.Text = "Update"; btnUpdate.Click += new EventHandler(btnUpdate_Click); pnlWebPartContainer.Controls.Add(btnUpdate); } void btnUpdate_Click(object sender, EventArgs e) { try { Dictionary temp = new Dictionary(); foreach (string column in ColumnDetails.Keys) { temp.Add(column, string.Format("{0}{1,10}{2:000}{3}", displayColumns[column].Checked ? 1 : 0, filterType[column].SelectedValue, int.Parse(displayOrder[column].Text), filterColumns[column].Text)); } SetPersonalizationDirty(); ColumnDetails = temp; SortOrder = new KeyValuePair(ddlSortColumn.SelectedValue, cbxSortAscending.Checked); } catch { } } protected override void CreateWebPartControls() { manageSPWeb = new ManageSPWeb(DMSSiteURL); DataTable documents = GetDocuments(); if (documents != null) { CreatedOutputControls(documents); } else { pnlWebPartContainer.Controls.Add(new LiteralControl("No Documents stored under this catagory")); } } private DataTable GetDocuments() { int i = 1; SPList spList = manageSPWeb.GetList("DMS"); SPQuery spQuery = new SPQuery(); spQuery.Query = "" + DisplayContentType + ""; dmsLinkFilter += string.Format("{0}?FilterField{1}=ContentType&FilterValue{1}={2}", spList.DefaultViewUrl, i++, DisplayContentType); foreach (KeyValuePair column in columnDetails) { if (column.Value.Length > 14) { spQuery.Query = string.Format("{0}<{1}>{4}", spQuery.Query, column.Value.Substring(1, 10).Trim(), column.Key, spList.Fields[column.Key].FieldValueType, column.Value.Substring(14)); dmsLinkFilter += string.Format("&FilterField{0}={1}&FilterValue{0}={2}", i++, column.Key, column.Value.Substring(14)); } } spQuery.Query = string.Format("{0}", spQuery.Query, SortOrder.Key, SortOrder.Value); return manageSPWeb.GetListItems(spList, spQuery); } private void CreatedOutputControls(DataTable documents) { List displayColumns = new List(); foreach (KeyValuePair column in columnDetails) { if (column.Value.Substring(0, 1).Equals("1")) { displayColumns.Add(column.Value.Substring(11, 3) + column.Key); } } displayColumns.Sort(); Table tblOutput = new Table(); TableRow tblRow = new TableHeaderRow(); tblRow.TableSection = TableRowSection.TableHeader; TableCell tblCell = new TableHeaderCell(); tblCell.Text = " "; tblRow.Cells.Add(tblCell); foreach (string column in displayColumns) { tblCell = new TableHeaderCell(); tblCell.Text = column.Substring(3); tblRow.Cells.Add(tblCell); } tblOutput.Rows.Add(tblRow); int count = 0; foreach (DataRow document in documents.Rows) { if (count++ >= DisplayNumberItems) { break; } tblRow = new TableRow(); tblCell = new TableCell(); tblCell.ToolTip = (string)document["DocIcon"]; HtmlImage imgType = new HtmlImage(); imgType.Src = "/_layouts/images/" + SPUtility.MapToIcon(SPContext.Current.Web, (string)document["DocIcon"], string.Empty, IconSize.Size16); imgType.Alt = (string)document["DocIcon"]; tblCell.Controls.Add(imgType); tblRow.Cells.Add(tblCell); foreach (string column in displayColumns) { string columnTitle = column.Substring(3); tblCell = new TableCell(); if (columnTitle.Equals("Title")) { HtmlAnchor lnkDocument = new HtmlAnchor(); //lnkDocument.HRef = (string)document["FileRef"]; lnkDocument.HRef = new SPFieldUrlValue((string)document["_dlc_DocIdUrl"]).Url; if (document[columnTitle] == DBNull.Value || string.IsNullOrEmpty((string)document[columnTitle])) { string filename = document["FileLeafRef"].ToString(); lnkDocument.InnerText = filename.Substring(0, filename.IndexOf(".")); } else { lnkDocument.InnerText = document[columnTitle].ToString(); } lnkDocument.Target = "_blank"; tblCell.Controls.Add(lnkDocument); } else { tblCell.Text = document[columnTitle].ToString(); } tblRow.Cells.Add(tblCell); } tblOutput.Rows.Add(tblRow); } pnlWebPartContainer.Controls.Add(tblOutput); if (count++ > DisplayNumberItems) { HtmlAnchor lnkMore = new HtmlAnchor(); lnkMore.Attributes.Add("class", "MoreDMSDocumentsLink"); lnkMore.HRef = dmsLinkFilter; lnkMore.InnerText = "More >"; lnkMore.Target = "_parent"; pnlWebPartContainer.Controls.Add(lnkMore); } } } }