Second Life of a Hungarian SharePoint Geek

November 24, 2015

NullReferenceException in the Content Editor Web Part when Trying to Upload Images

Filed under: CEWP, SP 2013 — Tags: , — Peter Holpar @ 23:16

Recently a user complained, that when he tries to upload a photo in a Content Editor Web Part (CEWP)…

image

… an error is displayed.

image

We found the following relevant line in the ULS logs:

11/16/2015 12:46:45.29     w3wp.exe (0x21FFC)                          0x2185C    SharePoint Foundation             Runtime                           tkau    Unexpected    System.NullReferenceException: Object reference not set to an instance of an object.    at ASP._layouts_15_upload_aspx.__Render__control29(HtmlTextWriter __w, Control parameterContainer) in c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\upload.aspx:line 81     at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children)     at Microsoft.SharePoint.WebControls.ScriptBlock.Render(HtmlTextWriter writer)     at System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter)     at ASP._layouts_15_upload_aspx.__Render__control27(HtmlTextWriter __w, Control parameterContainer) in c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\15\TEMPLATE\LAYOUTS\upload.aspx:line 61     at Sy…    b324429d-5597-507f-40b1-b3a2b68192e5

In upload.aspx at line 81 there is a reference for CurrentList.ID:

function LaunchOpenInExplorer()
{
    vCurrentListID = "<%= CurrentList.ID %>";
    vCurrentListUrlAsHTML = "<%= Web.Url + "/" + (CurrentList.RootFolder.Url.Length > 0 ? CurrentList.RootFolder.Url + "/" : "") %>";

}

We got the NullReferenceException, because the CurrentList object is null.

In our case, the CurrentList was null, because somebody has deleted all document libraries from the site where the page with the CEWP was located, so there was no place the user could upload the image.

However, we should not delete all of the libraries if you want to reproduce the issue. The GetActionableDocumentLibraries method of the Microsoft.SharePoint.ApplicationPages.UploadPage class is responsible for populating the list of document libraries of the upload page that are candidates for image upload. We can find the following loop and conditions in this method:

List<SPDocumentLibrary> list = new List<SPDocumentLibrary>();
foreach (SPList list2 in web.Lists)
{
  …
  SPDocumentLibrary item = list2 as SPDocumentLibrary;
  if ((((item != null) && !item.Hidden) && ((SPBaseType.DocumentLibrary == item.BaseType) && (SPListTemplateType.WebPageLibrary != item.BaseTemplate))) && (!item.IsCatalog && (!requireAddListItemsPermissions || (requireAddListItemsPermissions && list2.DoesUserHavePermissions(SPBasePermissions.AddListItems, false)))))
  {
    list.Add(item);
  }

}

As you can see, the library should not be a hidden one, should not be the Site Pages library or any of the standard catalog libraries (like Web Part Catalog), and the user must have the permission to add items to the list. For example, if there are no document libraries the user is allowed to add items to, we have the same issue with the CEWP.

Re-creating the deleted document library solved the issue for the user.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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

Create a free website or blog at WordPress.com.

%d bloggers like this: