Fixing Error: Invalid File Name When Generating Entity Classes for SharePoint with SPMetal

Don't look at me like that - the long title was for SEO.

If you're developing in SharePoint and at one point included a list in your solution that you later removed from the solution, but didn't first delete from the site, you'll find that that list is now corrupted. You won't be able to navigate to the list within SharePoint to delete it, because it is corrupted.

This situation also causes an error when using SPMetal to generate entity classes for using Linq-to-SharePoint in your project. When running SPMetal, you'll get a very unhelpful error: Error: Invalid file name.. Which will make you think "that's weird - that command didn't even ask for a file name." Honeybadger SharePoint Don't Care.

You'll have to delete the offending list, but since you can't delete it from the SharePoint web interface and you've already deleted it from your solution, you'll have to use the SharePoint Management Shell (powershell). Identify the offending list (click through all the lists in All Site Content until you find the one that won't open, if you don't know which one was deleted but still appears on the site's list of lists. Fire up the Management Shell and run each of these lines:

  • $web = get-spweb -Identity http://my-site:myport/sitename
  • $list = $web.lists["MyDeletedListName"]
  • $list.AllowDeletion = $true
  • $list.Update()
  • $list.Delete()

After this, the list should be gone from All Site Content, and you'll be able to run the SPMetal tool:
SPMetal /web:http://my-site:myport/sitename /code:MyEntities.cs without error.