Home > development, error, MOSS, MS SharePoint Sever, sharepoint, sps > COM exception while adding item to Document Library

COM exception while adding item to Document Library

What I was doing:
I created a SharePoint Designer workflow that will run automatically as soon as an item is added into the document library. It will simply add some metadata to the item.

What happened:
When you add an item to a document library, it will upload the document and then redirect you to EditForm.aspx where you can edit metadata for the document. On this form when I clicked “OK”, I received the following exception:

Microsoft.SharePoint.SPException: The file Shared Documents/Test.doc has been modified by Sydney\rehman on 20 Oct 2010 16:51:42 +1000. ---> System.Runtime.InteropServices.COMException (0x81020037): The file Shared Documents/Test.doc has been modified by Sydney\rehman on 20 Oct 2010 16:51:42 +1000.

Why it happened:
As soon as the document was uploaded, my SPD workflow went in and updated the metadata on the document (as it is configured to run automatically on the creation of the item). Now I am redirected to EditForm.aspx to edit metadata on the document. Here, even if I modify nothing and click “OK”, the COM Exception is generated, since the document metadata is already modified by the workflow and this is not liked by SharePoint. My clicking “OK” means I am trying to update/modify a previous version of this document. The current version is the one modified by my workflow. Hence the error: “Document has been modified by so and so……..”. If you want to edit, go and edit the current version from the document library. Here, on EditForm.aspx, if you click “Cancel”, you can see that document has actually been modified and has the metadata attached as a result of the workflow execution. (Ofcourse, it is already uploaded to the doc lib, clicking cancel on EditForm.aspx doesnt stop it.)

What I did to resolve:
Mine is just a workaround, its not a proper solution to the problem. I simply forced a redirect to AllItems.aspx page i.e. as soon as the document is uploaded, the page will not redirect to EditForm.aspx, it will be redirected to AllItems.aspx…………so the user will not get an option to edit metadata on the document and click “OK” (due to which the error is generated).

I didnt use pause in the workflow because it takes too long and if the user clicks okey during that pause, which is most likely, he wont see the metadata that should have been attached by my workflow, and this will freak him out, in my case the user must see that metadata on the document.

so I added simple javascript on EditForm.aspx, which will redirect the user to AllItems.aspx when the Mode is equal to Upload……………..this is important. We dont want to redirect when the user is just going to EditFrom.aspx by clicking Edit Properties from the context menu of the item. Have a look at the javascript:

Put this code on line 71 of the EditForm.aspx of your document library.

<script language="javascript" type="text/javascript">
    function Redirect() 
       var mode = getURLParam('Mode');
       var sourceURL = getURLParam('Source');
       var  properSourceURL = replaceCharacters(sourceURL);
       if (mode == "Upload")
          window.location = properSourceURL;    	   
    function getURLParam(name)
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( window.location.href );
	  if( results == null )
	    return "";
	    return results[1];
	function replaceCharacters(str)
	   str = str.replace(/%20/g, " ");
	   str = str.replace(/%2F/g, "/");
	   str = str.replace(/%3A/g, ":");
	   str = str.replace(/%2E/g, ".");
	   return str;	  

Following is the URL on the EditForm.aspx when redirected from an upload page:


So, you can see there are two parameters in the URL:

1. Mode
2. Source

My code simply checks if the Mode is equal to Upload, then redirect to the URL as mentioned by the Source parameter. In the Source parameter, the given URL is in quite a distorted format, so I have written some additional javascript to replace weird characters and codes in the URL with proper ones like “:” “/” and “.” etc.

Hope this helps–

  1. Dmitriy
    February 25, 2011 at 12:32 pm

    You helped me a lot. Thank you very much for that. A very long time trying to do redirect by using SPUtility.Redirect (strRedirectTo, SPRedirectFlags.Trusted, current); when the event ItemAdding (SPItemEventProperties properties) but all in vain. Using your solution, the entire page EditForm.aspx unfortunately loaded, and only then is a redirect that is not very good, but still, it is still better than nothing. Thanks again.

  2. Dmitriy
    March 2, 2011 at 12:10 pm

    I slightly modified your decision, I did not like that users will see an edit form for upload. Because I am in silence hid the edit form to enclose it in


    Next, I show it if it does not upload

    window.attachEvent (‘onload’, WindowOnLoad);

    function WindowOnLoad ()
        var mode = getURLParam (‘Mode’);
        if (mode! = “Upload”)
    var objHide = document.getElementById (“formhide”);
          objHide.style.display = “block”;

    Just need to take into account that if the site looks to the Internet and uses the domain instead of the internal server name need to change the name server for the domain, well it’s not hard understand as is done

      str = str.replace (/localhost:8000/g, “mysait.com”);

  3. vergil
    May 5, 2011 at 1:31 pm


    You are simply great to share this solution.

    Can you please explain a bit more how/where you included this spec?

  4. May 19, 2011 at 11:08 am

    Hi Rehman,

    This solution works fine when a user is adding the file manually. But I am creating a doc file and manipulating using OpenXml. And this document library has metadata which I need to update once I have upload the file.

    What do you suggest in this case, should I delay the thread? But somehow I don’t like the “Adding Delay” solution. Can you suggest a better solution?

    Thank you.


  5. Aza
    February 2, 2012 at 4:48 pm

    Thank you for sharing this.

    -A happy Sharepointer

  6. Ciaran
    December 17, 2013 at 4:04 pm

    Hi I really need your help. Like everyone else I am running in to this issue. I tried the above and add a at the end, which is missing. I put the cursor on line 71 and paste the code but I get the following error

    System.Web.UI.WebControls.MenuItemStyleCollection must have items of type ‘System.Web.UI.WebControls.MenuItemStyle’. ‘script’ is of type ‘System.Web.UI.HtmlControls.HtmlGenericControl’.

    Can anyone help, the pause solution just wont work for our situation. I need the column that I am trying to update be updated without the user having to refresh the screen.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: