1

Users uploads excel files to Share-point site every day.

Problem: Excel file names are not consistent so i came up with some file naming convention.

Is there any way we can reject a file that is ready to be uploaded if proper naming convention is not followed.

For instance: Good file naming convention Filename_YYYY_MM.xls

Wrong/Bad file naming convention: This could be anything other than what’s above

Let me know if it’s possible to reject a file or user wont be able to upload because proper file naming convention is not followed.

If it's possible, Please let me know how to make it work. I'm a newbie to SP.

Thanks!!

5
  • Do you have Visual Studio? Jul 24, 2014 at 16:13
  • Yes i do have Visual Studio Jul 24, 2014 at 17:32
  • ok, and you would like to notify the user somehow if their name isn't conventional? Or can you just ignore it and close the upload dialog? Jul 24, 2014 at 17:48
  • Yes. It would be good. If i can notify user about the name is not correct. Jul 24, 2014 at 18:31
  • Any resolution on this Colbs Jul 24, 2014 at 21:25

1 Answer 1

1

I accomplished this with a simple Sandbox solution in SharePoint 2010. Create an Event Receiver that will attach to a document library.

Below is the code that I used:

public class DocumentLibraryEventReceiver : SPItemEventReceiver
{

    const string _threeNumPrefixRegex = "^[0-9]{3}.*";
    const string _fileErrorMessage = "Non-conforming File Name:  File names must have a three-number prefix and be no more than 25 characters long.";
    const string _folderErrorMessage = "Non-conforming Folder Name: Folder names can not be longer than 25 characters.";

    private void Validate(SPItemEventProperties properties)
    {
        properties.Status = SPEventReceiverStatus.Continue;

        if (properties == null) return;
        if (properties.List == null) return;

        // Don't enforce Validation rules on Site Assets
        if (properties.List.IsSiteAssetsLibrary) return;
        if (properties.List.IsApplicationList) return;
        if (properties.List.BaseType != SPBaseType.DocumentLibrary) return;
        if (((SPDocumentLibrary)properties.List).IsCatalog) return;
        if (properties.List.BaseTemplate == SPListTemplateType.HomePageLibrary) return;
        if (properties.List.BaseTemplate == SPListTemplateType.WebPageLibrary) return;

        string fileName = properties.AfterUrl.Substring(properties.AfterUrl.LastIndexOf('/') + 1);

        // HACK: this is kind of a crappy way of determining if the item adding is a file or a folder.
        //       In the Web client the ContentTypeId is set in the after props so we can determine if 
        //       it is a folder child. That property is not there in Explorer view (others?) and should 
        //       handle the majority of cases.  This is a "solve *most* cases" solution.
        bool isFile;

        if (null != properties.AfterProperties["vti_docstoretype"])
        {
            Int32 vtiDocStoreType = (Int32)properties.AfterProperties["vti_docstoretype"];
            isFile = (0 == vtiDocStoreType);
        }
        else if (null != properties.AfterProperties["ContentTypeId"])
        {
            SPContentTypeId contentTypeId = new SPContentTypeId(properties.AfterProperties["ContentTypeId"].ToString());
            isFile = (false == contentTypeId.IsChildOf(SPBuiltInContentTypeId.Folder));
        }
        else if (null != properties.AfterProperties["vti_filesize"])
        {
            Int32 vtiFileSize = (Int32)properties.AfterProperties["vti_filesize"];
            isFile = (vtiFileSize > 0);
        }
        else isFile = false;

        if ((fileName.Length > 25) ||
            (isFile && (false == Regex.IsMatch(fileName, _threeNumPrefixRegex))))
        {
            properties.ErrorMessage = (isFile) ? _fileErrorMessage : _folderErrorMessage;
            properties.Status = SPEventReceiverStatus.CancelWithError;
        }
    }

    /// <summary>
    /// An item is being added.
    /// </summary>
    public override void ItemAdding(SPItemEventProperties properties)
    {
        try
        {
            this.EventFiringEnabled = false;
            base.ItemAdding(properties);

            Validate(properties);
        }
        finally
        {
            this.EventFiringEnabled = true;
        }
    }

    /// <summary>
    /// An item is being updated
    /// </summary>
    public override void ItemUpdating(SPItemEventProperties properties)
    {
        try
        {
            this.EventFiringEnabled = false;
            base.ItemUpdating(properties);

            Validate(properties);
        }
        finally
        {
            this.EventFiringEnabled = true;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.