Add to My Yahoo! | Google Reader or Homepage | Add to Windows Live | Add to Windows Live Alerts

Wictor Wilén

Microsoft Certified Master (MCM) - SharePoint 2010 | Microsoft Most Valuable Professional (MVP) - SharePoint Server MVP | Author

NullReferenceException when uploading multiple files in SharePoint 2010

Posted at 2010-10-14 01:38 by Wictor Wilén in Microsoft Office , SharePoint 2010 with 7 comments.

Recently came across a really interesting bug in SharePoint 2010. It's when you are trying to upload multiple files using the ActiveX control, where you can drag and drop files or select multiple files. I started receiving "Object reference not set to an instance of an object" exceptions from the STSUpld control. The usual Google, ahem, Bing check revealed nothing. Just that I was not alone having this problem (hence this post).

SNAGHTMLe5d8a

The strange thing is that this only appeared on some sites and on some computers. After some fiddling around I noticed that it actually only happened on sites where I used a custom master page. My initial thought was that this had something to do with invalid HTML or missing delegate controls - but everything was just fine (alright then, I actually found a missing end div tag, but that didn't solve it).

So I pulled out the almighty Reflector tool and checked the STSUpld.UploadCtl.GetFormInformation method to see what could possibly go wrong there. By inspecting the code for possible situations where a NullReferenceException could be thrown I found a couple of places where this is not checked for (ouch!):

  • The control needs a hidden input element with the ID destination
    This one was there, I did not touch the upload.aspx file - and you should not either!
  • This destination field must have a value
    This is populated by the code behind
  • The control needs an input element of the type button with an AccessKey attribute set to "O"
    For the OK button (might be different in other languages than English)
  • The control needs an input element of the type button with an AccessKey attribute set to "C"
    For the Cancel button (might be different in other languages than English)
  • All input element of the type button must have the AccessKey value set to non empty
    The control iterates through all input elements looking for the AccessKey to find the OK and Cancel buttons. In my case this was the one playing games with me!

The final point above was the thing that caused the NullReferenceException in the multiple upload control. In this case I had a few input elements with the type button without any AccessKey attributes in my master page (let's not go into why they are there...). Nevertheless once I added the AccessKey attribute with a non-empty value the upload worked like a charm. Note that setting AccessKey="" causes the same exception, you need to set a value for it for instance; AccessKey="_".

What can we learn from this! You should always check for null! Can't be said too many times. Calling ToString() on objects without checking for null is the eight deadly sin.

PS: I think this post is worthy of a KB article, Microsoft? At least as a patch in upcoming Office updates...

Comments and trackbacks

#  Amazing solution by Ashutosh India
Screenshot from websnpr Hi Wictor, you have helped as tremendously to come out of this grave issue. Dont have words to thankyou
#  Unghosted masterpage by InFrA
Screenshot from websnpr Hi, can you possibly shed some light as to how to do this if your masterpage is unghosted/customized and stored in the Content Database? We are experiencing this with Sharepoint 2007 after Upgrading Office 2007 to Office 2010.
#  @InFrA by Wictor
Screenshot from websnpr The only way is to manually edit all thos customized master pages. And don't expect this to be fixed either in any near future...
#  Saved me hours! by SPZ
Screenshot from websnpr Huge miss on microsoft's part. Would not have spent the time looking into the issue if I hadn't found this post. Much Appreciated!
#  Thank you! by Clayton
Screenshot from websnpr Hi Wictor, Came across your post and it has saved my bacon. We are about to roll out to the wider company and I had added inputs to a custom master page without access keys. Thanks to your post this won't be an issue ... cheers!
#  Excellent by Ram
Screenshot from websnpr Vow..Fabulous...You are great
#  Thanks!!! by Ahmed IG
Screenshot from websnpr Thanks alot! Had the same issue, and you just saved my day! Thanks alot!
Make a comment on this post:
Subject:  

Your name:  
Your Url:  
Note: submissions may have to be approved before being visible, so don't submit your comment multiple times.