Ektron Upload File Into Library

| | | |

I found and modified code to create an Ektron Library File Upload function.
This function allows some customization:

  • Defined Folder ID
    • In this code I manually set the folder ID but effectively
      you could have a drop down list of folders to select the folder
      (see DMS Widget)
  • Content Title
    • This is used in the Ektron Library as the title of the
      file
  • File Name
    • This is used in the Ektron Library as the filename and part
      of the filepath
  • File Upload Control
    • This is used in the event that you want to have more than
      one upload control
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="LibraryUpload.aspx.cs" Inherits="LibraryUpload" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Library Upload</title>

</head>
<body>
    <form id="form1" runat="server">

        <h1>New Division</h1>
        <asp:ValidationSummary runat="server" ID="vasFileUpload" ValidationGroup="FileUpload" ForeColor="#81060C" />

        <asp:Label ID="lblDivisionID" runat="server" Text="Division ID:" AssociatedControlID="txtDivisionID" /><br />
        <asp:TextBox ID="txtDivisionID" runat="server" />
        <asp:RequiredFieldValidator ID="rfvDivisionID" runat="server" ControlToValidate="txtDivisionID" Display="None"
            ErrorMessage="The Division ID field is required, please complete the information."
            ValidationGroup="FileUpload" /><br />
        <asp:Label ID="lblFile" runat="server" Text="File:" AssociatedControlID="fupFile" /><br />
        <asp:FileUpload ID="fupFile" runat="server" />
        <br />
        <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" ValidationGroup="FileUpload" CausesValidation="true" />

        <p><asp:Literal ID="ltlMessage" runat="server" /></p>

    </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;

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;

public partial class LibraryUpload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }


    protected void btnUpload_Click(object sender, EventArgs e)
    {
        //Library Upload Upload
        EktronLibraryUpload(167, txtDivisionID.Text, txtDivisionID.Text + ".pdf", fupFile);
    }

    protected void EktronLibraryUpload(long lFolderID, string sContentTitle, string sFileName, FileUpload fupFile)
    {

        // initialize framework.
        Ektron.Cms.API.Library eLibraryAPI = new Ektron.Cms.API.Library();
        LibraryManager eLibraryMgr = new LibraryManager();

        // determine if the item already exists.
       // Ektron.Cms.Content.LibraryCriteria eLibraryCriteria = new Ektron.Cms.Content.LibraryCriteria();

        #region Upload the file to Ektron
        Ektron.Cms.LibraryConfigData eLibraryConfigData = eLibraryAPI.GetLibrarySettings(lFolderID);

        // Set filename server path
        string sFileServerPath = Server.MapPath(eLibraryConfigData.FileDirectory);

        // Set filename to original file name (whatever the user has named the file) if a custom file name is not specified
        if (sFileName == "")
        {
            sFileName = Path.GetFileName(fupFile.FileName);
        }

        try
        {
            Ektron.Cms.LibraryData item = new Ektron.Cms.LibraryData()
            {
                Title = sContentTitle,
                ParentId = lFolderID,
                FileName = sFileServerPath + sFileName,
                File = fupFile.FileBytes
            };

            if (fupFile.PostedFile.ContentType == "application/pdf")
            {
                eLibraryMgr.Add(item);
                ltlMessage.Text = "Library item added with ID = " + item.Id.ToString();
            }
            else
            {
                ltlMessage.Text = "Not added. File must be a PDF.";
            }
        }
        catch (Exception ex)
        {
            ltlMessage.Text = "Not added, " + ex.Message;
        }
        #endregion Upload the file to Ektron
    }
}

References:

Originally Posted on February 18, 2015
Last Updated on October 26, 2015
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.