PowerShell, SharePoint, Technical

Missing Upload.aspx in a Document Library

A departure from my normal post…I usually try to stick to only posting solutions to weird situations I run into, or recommendations from real world experiences, or the occasional post on community or work activities. Not this time. This post technically falls into the first category (solution to a weird situation), but is really a cry for help. Smile

Problem Definition
The problem is having a document library that is missing the Upload.aspx form, which should be associated to the SPList.DefaultNewFormUrl property of the library. There are a few different ways to get there. I ran into it because of a web template I built that was missing the creation of the Upload.aspx file, so as sites were created with this template, everything worked fine except for “Upload Multiple Documents” and “New Folder” functionality. Both require the existence of the Upload.aspx file mapped to the DefaultNewFormUrl property to work properly. My bad.

Once the bug was identified, I fixed it in the template, but we still had existing sites that had the problem. Deploying a new version of the web template doesn’t retroactively fix existing sites, so we needed to find another solution.

Manual Fix
Although not ideal, there is a way to manually fix this issue:

  1. Open the site in SharePoint Designer and open “All Files” –> [Affected Doc Lib] –> Forms directory
  2. Right click, New, ASPX, name it “Upload.aspx”, and open the file
  3. Still in Designer, open another library with a good Upload.aspx file and open that file
  4. Copy the source from the good Upload.aspx file and paste it into the Upload.aspx source of the new file just created
  5. Change a few values in the properties of the ListFormWebPart:
    1. “Title” should be the title of the document library
    2. “ListName” should be the ID of the list, with curly brackets surrounding the ID
    3. “ListId” should be the ID of the list, without curly brackets
  6. Save the file

That should do it. Now you can upload multiple files, and create new folders. Sweet!

Desired Fix
Here’s where I deviate from my normal theme of blog postings. Although the manual fix is nice, what I really wanted was a way to script the fix so we could quickly and repeatedly update multiple libraries that were affected. This need leads me to PowerShell and figuring out how to add the Upload.aspx file to the affected libraries through a script.

After a bit of searching, I found a blog post on how to programmatically add a custom list form to a list. This was a great kick start to what I wanted to do. Converting that to PowerShell led to:

   1: $web = Get-SPWeb http://svr1/sites/teamportal/ProjectSite

   2: $badlib = $web.Lists["Change Requests"]

   3: $uploadfile = $badlib.RootFolder.Files.Add("/sites/teamportal/ProjectSite/ChangeRequests/Forms/Upload.aspx", [Microsoft.SharePoint.SPTemplateFileType]::FormPage)

   4: $limitedwebpartmanager = $uploadfile.GetLimitedWebPartManager([System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

   5: $webpart = New-Object Microsoft.SharePoint.WebPartPages.ListFormWebPart

   6: $webpart.ListId = $badlib.ID

   7: $webpart.PageType = [Microsoft.SharePoint.PAGETYPE]::PAGE_NEWFORM

   8: $limitedwebpartmanager.AddWebPart($webpart, "Main", 0)

   9: $badlib.DefaultNewFormUrl = "/sites/teamportal/ProjectSite/ChangeRequests/Forms/Upload.aspx"

Running this script added the Upload.aspx file to the library and properly associated it with the DefaultNewFormUrl property of the library. Multiple upload worked great. Adding a new folder was looking good…clicking on New Folder would open the correct form with the ribbon and Name field, but after clicking Save, would throw an error “Value does not fall within the expected range” for SPFileSystemObjectType when trying to create the folder.

For the life of me, I cannot figure out how to fix that! So here’s the challenge:  Duplicate this in your environment by creating a document library, opening the site in SharePoint Designer, open the Forms directory in the library, delete the Upload.aspx file, and then create a PowerShell script to add a new Upload.aspx file that works. If you make more progress than I did, please comment.