blogs.conchango.com

welcome to the conchango blogging site
Welcome to blogs.conchango.com Sign in | Join | Help
in Search

Merrick Chaffer's Blog

Customising file upload validation in MOSS 2007 (Sharepoint)

Just spent the day trying to do this, having not really written sharepoint code before. Seeing as it took most of the day to figure out, and perfect, I thought I would post the solution in case I ever needed to repeat this again...

clip_image002[5]

In the customised UploadAtlasDocument.aspx in the 12 hive C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\ I added the following code...

<script runat="server">

 

    /// <summary>

    /// Validates the posted filename doesn't already exist in the sharepoint list CH Documents

    /// </summary>

    /// <param name="source"></param>

    /// <param name="args"></param>

    void custVldFileExists_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)

    {

        //note you don't get intellisense for this.OverwriteSingle

        //as it is nested in a template control

        if (!string.IsNullOrEmpty(args.Value)

            && this.OverwriteSingle.Visible == true

            && this.OverwriteSingle.Checked == false)

        {

            FileInfo fileInfo = new FileInfo(this.InputFile.PostedFile.FileName);

 

            string newFileName = fileInfo.Name;

 

            using (SPSite site = new SPSite(SPContext.Current.Web.Url))

            {

                using (SPWeb webSite = site.OpenWeb())

                {

                    SPList chDocumentsList = webSite.Lists["CH Documents"];

                   

                    //do we have any files existing in the list already

                    if (chDocumentsList != null && chDocumentsList.RootFolder.Files.Count > 0)

                    {

                        //we have files in the list already

                        //so iterate round each one and see if the one we're about to upload

                        //already exists in the list.

                        foreach (SPFile spFile in chDocumentsList.RootFolder.Files)

                        {

                            if (string.Compare(spFile.Name, newFileName, StringComparison.InvariantCultureIgnoreCase) == 0)

                            {

                                //the file name we're uploading

                                //matches an existing file,

                                //and the user hasn't checked the OverwriteMultiple check box

                                //so invalidate this validator

                                //in order to get message back to the user.

                                args.IsValid = false;

                                break;

                            }

                        }

                    }

                   

                    webSite.Close();

                }

                site.Close();

            }

        }

    }

</script>

...

<TR><TD>

            <wssawc:InputFormRequiredFieldValidator ID="InputFormRequiredFieldValidator1" ControlToValidate="InputFile"

               Display="Dynamic" ErrorMessage="Please click the browse button to upload a file." Runat="server"

               BreakBefore="false" BreakAfter="false" EnableClientScript="false" />

            <asp:CustomValidator ID="custVldFileExists" runat="server"

                    ErrorMessage="File already exists. Please either rename the file or select overwrite to replace the existing file."

                    ControlToValidate="InputFile" OnServerValidate="custVldFileExists_ServerValidate"

                    Display="Static" EnableClientScript="false" ></asp:CustomValidator>

            <asp:CustomValidator ID="CustomValidator1" ControlToValidate="InputFile"

               Display = "Dynamic"

               ErrorMessage = "<%$Resources:wss,upload_document_file_invalid%>"

               OnServerValidate="ValidateFile"

               runat="server" EnableClientScript="false" />

                              </TD></TR>

Note the EnableClientScript="false" was required so that the error would get cleared from our custom validator if the user deleted the text out of the input file control, and then hit OK again.

Published 05 March 2008 16:12 by merrick.chaffer
Anonymous comments are disabled
Powered by Community Server (Personal Edition), by Telligent Systems