Following my code on Ektron – DMS Content List I created this Ektron Widget
<%@ Control Language= "C#" AutoEventWireup= "true" CodeFile= "DMSFileList.aspx.cs" Inherits= "widgets_DMSFileList" Debug= "true" %> <asp:MultiView ID= "ViewSet" runat= "server" ActiveViewIndex= "0" > <asp:View ID= "View" runat= "server" > <asp:Label runat= "server" ID= "lblDMSList" >DMS Files</asp:Label> <asp:ListView ID= "lvDMSList" runat= "server" ItemPlaceholderID= "aspItemPlaceholder" Visible= "false" > <EmptyDataTemplate> <asp:Literal ID= "litEmptyDataBasic" runat= "server" Text= "text" OnInit= "litEmptyData_Init" /> </EmptyDataTemplate> <LayoutTemplate> <div class = "form" style= "max-width: 650px;" > <asp:PlaceHolder ID= "aspItemPlaceholder" runat= "server" ></asp:PlaceHolder> </div> </LayoutTemplate> <ItemTemplate> <div style= "vertical-align: text-top;" > <a href= "<%# Eval(" QuickLink ")%>" ><%# sGetFileDetails(Eval( "id" ).ToString(), 2)%> <%# Eval( "Title" )%></a> </div> <div class = "clear" ></div> </ItemTemplate> <AlternatingItemTemplate> <div style= "vertical-align: text-top; background-color: lightgray;" > <a href= "<%# Eval(" QuickLink ")%>" ><%# sGetFileDetails(Eval( "id" ).ToString(), 2)%> <%# Eval( "Title" )%></a> </div> <div class = "clear" ></div> </AlternatingItemTemplate> </asp:ListView> <asp:ListView ID= "lvDMSListDetailed" runat= "server" ItemPlaceholderID= "aspItemPlaceholder" Visible= "false" > <EmptyDataTemplate> <asp:Literal ID= "litEmptyDataDetailed" runat= "server" Text= "text" OnInit= "litEmptyData_Init" /> </EmptyDataTemplate> <LayoutTemplate> <div class = "form" style= "max-width: 650px;" > <asp:PlaceHolder ID= "aspItemPlaceholder" runat= "server" ></asp:PlaceHolder> </div> </LayoutTemplate> <ItemTemplate> <div class = "form_2Col floatLeft" style= "vertical-align: text-top;" > <a href= "<%# Eval(" QuickLink ")%>" ><%# sGetFileDetails(Eval( "id" ).ToString(), 2)%> <%# Eval( "Title" )%></a> <br /> <%# Eval( "teaser" )%> </div> <div class = "form_2Col floatLeft" style= "vertical-align: text-top;" > <strong>Last Modified:</strong> <%# Eval( "DateModified" )%><br /> <strong>File Size:</strong> <%# sGetFileSize(Eval( "id" ).ToString()) %> </div> <div class = "clear" ></div> </ItemTemplate> <AlternatingItemTemplate> <div class = "form_2Col floatLeft" style= "vertical-align: text-top; background-color: lightgray;" > <a href= "<%# Eval(" QuickLink ")%>" ><%# sGetFileDetails(Eval( "id" ).ToString(), 2)%> <%# Eval( "Title" )%></a> <br /> <%# Eval( "teaser" )%> </div> <div class = "form_2Col floatLeft" style= "vertical-align: text-top; background-color: lightgray;" > <strong>Last Modified:</strong> <%# Eval( "DateModified" )%><br /> <strong>File Size:</strong> <%# sGetFileSize(Eval( "id" ).ToString()) %> </div> <div class = "clear" ></div> </AlternatingItemTemplate> </asp:ListView> </asp:View> <asp:View ID= "Unauthorized" runat= "server" > <div id= "UnauthorizedMessage" runat= "server" ></div> </asp:View> <asp:View ID= "Edit" runat= "server" > <div id= "<%=ClientID%>_edit" > <asp:Label ID= "EditDebug" runat= "server" Visible= "false" /> Title For List:<br /> <asp:TextBox ID= "txtTitle" runat= "server" Style= "width: 95%" /><br /> List Files From Folder: <br /> <asp:DropDownList ID= "ddlFolder" runat= "server" Style= "width: 95%" ToolTip= "Select the folder to get files from" > <asp:ListItem Text= "-- Select One --" Value= "" /> </asp:DropDownList><br /> Show Empty Widget:<br /> <asp:DropDownList ID= "ddlShowEmpty" runat= "server" Style= "width: 95%" > <asp:ListItem Text= "No" Value= "No" /> <asp:ListItem Text= "Yes" Value= "Yes" /> </asp:DropDownList><br /> Empty List Message:<br /> <asp:TextBox ID= "txtShowEmptyMessage" runat= "server" TextMode= "MultiLine" Style= "height: 70px; width: 95%" /><br /> Sort List By:<br /> <asp:DropDownList ID= "ddlSortList" runat= "server" Style= "width: 95%" > <asp:ListItem Text= "Title" Value= "Title" /> <asp:ListItem Text= "Last Modified" Value= "DateModified" /> </asp:DropDownList><br /> Sort Direction:<br /> <asp:DropDownList ID= "ddlSortDirection" runat= "server" Style= "width: 95%" > <asp:ListItem Text= "Ascending" Value= "Ascending" /> <asp:ListItem Text= "Descending" Value= "Descending" /> </asp:DropDownList><br /> Show File Details:<br /> <asp:DropDownList ID= "ddlDetails" runat= "server" Style= "width: 95%" > <asp:ListItem Text= "Yes" Value= "Yes" /> <asp:ListItem Text= "No" Value= "No" /> </asp:DropDownList><br /> <asp:Button ID= "CancelButton" runat= "server" Text= "Cancel" OnClick= "CancelButton_Click" /> <asp:Button ID= "SaveButton" runat= "server" Text= "Save" OnClick= "SaveButton_Click" /> </div> </asp:View> </asp:MultiView> |
using System; using System.Collections.Generic; using System.Configuration; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using Ektron.Cms; using Ektron.Cms.API; using Ektron.Cms.Common; using Ektron.Cms.Content; using Ektron.Cms.Framework.Content; using Ektron.Cms.Framework.Organization; using Ektron.Cms.Framework; using Ektron.Cms.Organization; using Ektron.Cms.PageBuilder; using Ektron.Cms.Widget; public partial class widgets_DMSFileList : System.Web.UI.UserControl, IWidget { #region properties private Ektron.Cms.Framework.Organization.FolderManager EkFolderManager = new Ektron.Cms.Framework.Organization.FolderManager(ApiAccessMode.Admin); private Ektron.Cms.API.User.User user = new Ektron.Cms.API.User.User(); public string sUsername = "" ; #region List Title Config private string _sTitle; [WidgetDataMember( "DMSFileList_Title" )] public string sTitle { get { return _sTitle; } set { _sTitle = value; } } #endregion List Title Config #region Folder List Config private string _sFolder; [WidgetDataMember( "DMSFileList_Folder" )] public string sFolder { get { return _sFolder; } set { _sFolder = value; } } #endregion Folder List Config #region Empty List Config private string _ShowEmpty; [WidgetDataMember( "DMSFileList_ShowEmpty" )] public string sShowEmpty { get { return _ShowEmpty; } set { _ShowEmpty = value; } } private string _ShowEmptyMessage; [WidgetDataMember( "DMSFileList_ShowEmptyMessage" )] public string sShowEmptyMessage { get { return _ShowEmptyMessage; } set { _ShowEmptyMessage = value; } } #endregion Empty List Config #region List Sort Config private string _sSortList; [WidgetDataMember( "DMSFileList_SortList" )] public string sSortList { get { return _sSortList; } set { _sSortList = value; } } #endregion List Sort Config #region List Sort Direction Config private string _sSortDirection; [WidgetDataMember( "DMSFileList_SortDirection" )] public string sSortDirection { get { return _sSortDirection; } set { _sSortDirection = value; } } #endregion List Sort Direction Config #region List File Details Config private string _sShowDetails; [WidgetDataMember( "DMSFileList_Details" )] public string sShowDetails { get { return _sShowDetails; } set { _sShowDetails = value; } } #endregion List File Details Config #endregion IWidgetHost _host; protected ContentAPI m_refContentApi = new ContentAPI(); protected EkMessageHelper m_refMsg; protected void Page_Init( object sender, EventArgs e) { //string sitepath = new CommonApi().SitePath; //JS.RegisterJSInclude(this, JS.ManagedScript.EktronJS); //JS.RegisterJSInclude(this, JS.ManagedScript.EktronModalJS); //Css.RegisterCss(this, Css.ManagedStyleSheet.EktronModalCss); _host = Ektron.Cms.Widget.WidgetHost.GetHost( this ); m_refMsg = m_refContentApi.EkMsgRef; _host.Title = "DMS File List" ; _host.Edit += new EditDelegate(EditEvent); _host.Maximize += new MaximizeDelegate( delegate () { Visible = true ; }); _host.Minimize += new MinimizeDelegate( delegate () { Visible = false ; }); _host.Create += new CreateDelegate( delegate () { EditEvent( "" ); }); CancelButton.Text = m_refMsg.GetMessage( "btn cancel" ); SaveButton.Text = m_refMsg.GetMessage( "btn save" ); PreRender += new EventHandler( delegate ( object PreRenderSender, EventArgs Evt) { SetOutput(); }); ViewSet.SetActiveView(View); long nFolderInt; try { nFolderInt = Convert.ToInt64(sFolder); } catch { nFolderInt = 0; } if (nFolderInt > 0) { lblDMSList.Text = "<h2>" + sTitle + "</h2>" ; LoadDMSList(nFolderInt); } else { // ViewSet.SetActiveView(Unauthorized); } } void EditEvent( string settings) { FolderList(0); //EditDebug.Text = "sFolder: " + sFolder + "<br/>"; //EditDebug.Visible = true; txtTitle.Text = sTitle; if (sFolder != "DMSFileList_Folder" ) { ddlFolder.SelectedValue = sFolder; } if (sShowEmpty != "DMSFileList_ShowEmpty" ) { ddlShowEmpty.SelectedValue = sShowEmpty; } txtShowEmptyMessage.Text = sShowEmptyMessage; if (sSortList != "DMSFileList_SortList" ) { ddlSortList.SelectedValue = sSortList; } if (sSortDirection != "DMSFileList_SortDirection" ) { ddlSortDirection.SelectedValue = sSortDirection; } if (sShowDetails != "DMSFileList_Details" ) { ddlDetails.SelectedValue = sShowDetails; } ViewSet.SetActiveView(Edit); } protected void SaveButton_Click( object sender, EventArgs e) { sTitle = txtTitle.Text; sFolder = ddlFolder.SelectedValue; sShowEmpty = ddlShowEmpty.SelectedValue; sShowEmptyMessage = txtShowEmptyMessage.Text; sShowDetails = ddlDetails.SelectedValue; sSortList = ddlSortList.SelectedValue; sSortDirection = ddlSortDirection.SelectedValue; _host.SaveWidgetDataMembers(); ViewSet.SetActiveView(View); } protected void SetOutput() { } protected void CancelButton_Click( object sender, EventArgs e) { ViewSet.SetActiveView(View); } protected void LoadDMSList( long folderID) { try { AssetManager assetManager = new AssetManager(); AssetCriteria criteria = new AssetCriteria(); criteria.AddFilter(AssetProperty.FolderId, CriteriaFilterOperator.EqualTo, folderID); switch (sSortList) { case "Title" : default : criteria.OrderByField = AssetProperty.Title; break ; case "DateModified" : criteria.OrderByField = AssetProperty.DateModified; break ; } //criteria.OrderByField = AssetProperty.Title; //criteria.OrderByField = AssetProperty.Title; switch (sSortDirection) { case "Ascending" : criteria.OrderByDirection = EkEnumeration.OrderByDirection.Ascending; break ; case "Descending" : criteria.OrderByDirection = EkEnumeration.OrderByDirection.Descending; break ; default : criteria.OrderByDirection = EkEnumeration.OrderByDirection.Ascending; break ; } // Only Show Published documents criteria.AddFilter(AssetProperty.Status, CriteriaFilterOperator.EqualTo, "A" ); List<ContentAssetData> contentAssetDataList = assetManager.GetList(criteria); // If documents exist display the header and list the files if (sShowDetails == "Yes" ) { lvDMSListDetailed.DataSource = contentAssetDataList; lvDMSListDetailed.DataBind(); if (sShowEmpty == "No" && lvDMSListDetailed.Items.Count.ToString() == "0" ) { lblDMSList.Visible = false ; } else { // If details are desired render the details list lvDMSList.Visible = false ; lvDMSListDetailed.Visible = true ; } } else { lvDMSList.DataSource = contentAssetDataList; lvDMSList.DataBind(); if (sShowEmpty == "No" && lvDMSList.Items.Count.ToString() == "0" ) { lblDMSList.Visible = false ; } else { // If details are not desired render the basic list lvDMSListDetailed.Visible = false ; lvDMSList.Visible = true ; } } ViewSet.SetActiveView(View); } catch (Exception ex) { UnauthorizedMessage.InnerHtml = ex.Message; ViewSet.SetActiveView(Unauthorized); } #region Available Options /* * id * Title * FolderId * ContType * Type * SubType * DateCreated * DateModified * QuickLink * Status * teaser */ #endregion Available Options } protected void litEmptyData_Init( object sender, EventArgs e) { (sender as Literal).Text = sShowEmptyMessage; } protected string sGetFileDetails( string sAssetID, int nOption) { //Get the Asset data for given Content ID var AssetCRUD = new AssetManager(Ektron.Cms.Framework.ApiAccessMode.Admin); var asset = AssetCRUD.GetItem(Convert.ToInt64(sAssetID), false ); switch (nOption) { default : case 1: return asset.AssetData.FileName; break ; case 2: return asset.AssetData.Icon; break ; } } public string sGetFileSize( string assetid) { string sFileSize = "" ; Ektron.Cms.ContentAPI content_api = new Ektron.Cms.ContentAPI(); long lCallerId = content_api.RequestInformationRef.CallerId; content_api.RequestInformationRef.CallerId = ( long )Ektron.Cms.Common.EkEnumeration.UserGroups.InternalAdmin; Ektron.Cms.ContentData content_data = null ; content_data = content_api.GetContentById(Convert.ToInt64(assetid), 0); content_data = content_api.ShowContentById(Convert.ToInt64(assetid), content_api.CmsPreview, System.Convert.ToBoolean(!content_api.CmsPreview)); string filepath; AssetData _assetData = new AssetData(); filepath = Page.Server.MapPath(( string )(content_api.EkContentRef.GetViewUrl(Convert.ToInt32(content_data.Type), content_data.AssetData.Id).Replace(Page.Request.Url.Scheme + "://" + Page.Request.Url.Authority, "" ).Replace( ":443" , "" ).Replace( ":80" , "" ))); try { FileInfo fi = new FileInfo(filepath); sFileSize = FormatBytes(fi.Length); } catch { } content_api.RequestInformationRef.CallerId = lCallerId; return sFileSize; } public string FormatBytes( long bytes) { const int scale = 1024; string [] orders = new string [] { "GB" , "MB" , "KB" , "Bytes" }; long max = ( long )Math.Pow(scale, orders.Length - 1); foreach ( string order in orders) { if (bytes > max) return string .Format( "{0:##.##} {1}" , decimal .Divide(bytes, max), order); max /= scale; } return "0 Bytes" ; } private void FolderList( long folderId) { try { //Get a list of child folders List< long > FolderIdList = GetChildFolderIDs(folderId, true ); //Get a list of folder objects Ektron.Cms.FolderCriteria fcriteria = new FolderCriteria(); fcriteria.AddFilter(FolderProperty.Id, CriteriaFilterOperator.In, FolderIdList); fcriteria.PagingInfo = new PagingInfo(2000, 1); fcriteria.OrderByField = FolderProperty.FolderPath; //fcriteria.OrderByField = FolderProperty.Id; fcriteria.OrderByDirection = EkEnumeration.OrderByDirection.Ascending; List<FolderData> folderList = EkFolderManager.GetList(fcriteria); //Loop over the folder list, and generate the menus. Build dictionary list of folder to menu ids foreach (FolderData folder in folderList) { ddlFolder.Items.Add( new ListItem(folder.NameWithPath, folder.Id.ToString())); } ddlFolder.DataBind(); } catch (Exception ex) { } } public static List< long > GetChildFolderIDs( long FolderID, bool Recursive) { Ektron.Cms.Framework.Organization.FolderManager EkFolderManager = new Ektron.Cms.Framework.Organization.FolderManager(ApiAccessMode.Admin); List< long > folderIDs = new List< long >(); Ektron.Cms.FolderCriteria criteria = new FolderCriteria(); criteria.AddFilter(Ektron.Cms.Common.FolderProperty.ParentId, Ektron.Cms.Common.CriteriaFilterOperator.EqualTo, FolderID); criteria.PagingInfo.RecordsPerPage = 500; List<Ektron.Cms.FolderData> childFolders = EkFolderManager.GetList(criteria); if (childFolders != null ) { foreach ( var childFolder in childFolders) { if (childFolder.Id > 0) { if (!folderIDs.Contains(childFolder.Id)) { folderIDs.Add(childFolder.Id); } if (Recursive && childFolder.HasChildren) { folderIDs = folderIDs.Union(GetChildFolderIDs(childFolder.Id, Recursive)).ToList(); } } } } return ( from ids in folderIDs orderby ids select ids).ToList(); } } |
Last Updated on October 26, 2015