SharePoint 2010 Rename Document on Upload Fails in Explorer View

sharepoint-2010

I'm trying to implement a customization in SharePoint 2010 so that when a document is uploaded to a library, the file name is changed to include the Document ID in the name. (I know that people shouldn't worry about file names as much any more, but we have a lot of legacy files already named and users who like to have local copies).

I was able to implement a custom Event Receiver on the ItemAdded event that renames the file by adding the Document ID before the file name. This works correctly from the web Upload.

The problem is with the Explorer View. When I try to add the file using WebDAV in the Explorer View, I get two copies of the file. It seems that when a file is uploaded via the Web the events that fire are

  1. ItemAdding
  2. ItemAdded

But when I copy/paste a file into Explorer View I see the following events:

  1. ItemAdding
  2. ItemAdded
  3. ItemAdding
  4. ItemAdded
  5. ItemUpdating
  6. ItemUpdated

The result is I have two files with different names (since the Document IDs are different).

I've found a lot of people talking about this issue online (this is the best article I found). Anyone have any other ideas? Would it make more sense to do this in a workflow instead of an event receiver? I could use a scheduled job instead, but that might be confusing to the user if the document name changed a few minutes later.

This is my code that works great when using the Web upload but not when using Explorer View:

public override void ItemAdded(SPItemEventProperties properties)
{
   try
   {
       SPListItem currentItem = properties.ListItem;

       if (currentItem["_dlc_DocId"] != null)
       {
       string docId = currentItem["_dlc_DocId"].ToString();
       if (!currentItem["BaseName"].ToString().StartsWith(docId))
       {
           EventFiringEnabled = false;
           currentItem["BaseName"] = docId + currentItem["BaseName"];
           currentItem.SystemUpdate();
           EventFiringEnabled = true;
       }
       }
   }
   catch (Exception ex)
   {
       //Probably should log an error here
   }            
   base.ItemAdded(properties);
}

Best Answer

I have found that using a Visual Studio workflow allows me the most flexibility to do this. A SharePoint Designer Workflow would be simpler, but would be harder to deploy to different sites and libraries.

After reading some good articles including this and this I have come up with this code which seems to work. It starts a workflow and waits until the document is not in a LockState and then processes the filename.

The workflow looks like this:

Workflow image

And here is the code behind:

namespace ControlledDocuments.RenameWorkflow
{
    public sealed partial class RenameWorkflow : SequentialWorkflowActivity
    {
        public RenameWorkflow()
        {
            InitializeComponent();
        }

        public Guid workflowId = default(System.Guid);
        public SPWorkflowActivationProperties workflowProperties = new SPWorkflowActivationProperties();

        Boolean continueWaiting = true;


        private void onWorkflowActivated1_Invoked(object sender, ExternalDataEventArgs e)
        {
            CheckFileStatus();
        }

        private void whileActivity(object sender, ConditionalEventArgs e)
        {
            e.Result = continueWaiting;
        }

        private void onWorkflowItemChanged(object sender, ExternalDataEventArgs e)
        {
            CheckFileStatus();
        }

        private void CheckFileStatus()
        {
            if (workflowProperties.Item.File.LockType == SPFile.SPLockType.None)
            {
                continueWaiting = false;
            }
        }

        private void renameFile(object sender, EventArgs e)
        {
            try
            {
                SPListItem currentItem = workflowProperties.Item;

                if (currentItem["_dlc_DocId"] != null)
                {
                    string docId = currentItem["_dlc_DocId"].ToString();
                    if (!currentItem["BaseName"].ToString().StartsWith(docId))
                    {
                        currentItem["BaseName"] = docId + currentItem["BaseName"];
                        currentItem.SystemUpdate();
                    }
                }
            }
            catch (Exception ex)
            {
                //Should do something useful here
            }
        }

    }
}

Hope this helps someone else if they have the same problem.

Related Topic