Welcome to EMC Consulting Blogs Sign in | Join | Help

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

Comments

 

SharePoint 2007 Link Love: 03-25-2008 part deux at Virtual Generations said:

March 25, 2008 13:42
 

How to change built-in "NAME" field display and order ? | keyongtech said:

January 18, 2009 17:15
 

kmicic said:

When i should put this script?

When i put it in in <asp:Content ContentPlaceHolderId="PlaceHolderAdditionalPageHead" runat="server">

on my new customized upload.aspx?

When i do this a have error like this:

c:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS\upload.aspx(164): error CS0246: The type or namespace name 'FileInfo' could not be found (are you missing a using directive or an assembly reference?) at System.Web.Compilation.AssemblyBuilder.Compile()

at System.Web.Compilation.BuildProvidersCompiler.PerformBuild()

at System.Web.Compilation.BuildManager.CompileWebFile(VirtualPath virtualPath)

at System.Web.Compilation.BuildManager.GetVPathBuildResultInternal(VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)

at System.Web.Compilation.BuildManager.GetVPathBuildResultWithNoAssert(HttpContext context, VirtualPath virtualPath, Boolean noBuild, Boolean allowCrossApp, Boolean allowBuildInPrecompile)

at System.Web.Compilation.BuildManager.GetVirtualPathObjectFactory(VirtualPath virtualPath, HttpContext context, Boolean allowCrossApp, Boolean noAssert)

at System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(VirtualPath virtualPath, Type requiredBaseType, HttpContext context, Boolean allowCrossApp, Boolean noAssert)

at System.Web.UI.PageHandlerFactory.GetHandlerHelper(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)

at System.Web.UI.PageHandlerFactory.System.Web.IHttpHandlerFactory2.GetHandler(HttpContext context, String requestType, VirtualPath virtualPath, String physicalPath)

at System.Web.HttpApplication.MapHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)

at System.Web.HttpApplication.MapHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()

at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

November 5, 2009 12:35
 

kmicic said:

I add import to System.Web.UI but this was wrong, and later imported System.IO

but SP now say in error stack that they don't have onLoad function.

I paste standard onLoad function, but they have 'base' object inside. And now i have error with say its no reference to object.

November 5, 2009 12:47
Anonymous comments are disabled

This Blog

Syndication

News

Powered by Community Server (Personal Edition), by Telligent Systems