Error occurred while parsing EntityName.

So had an issue today.  We were migrating data into SharePoint and once that was complete we had a request from the client to build a set of views based on a lookup list.  So out comes the old trusty PowerShell script ISE and I started plugging away at the code.  When the script was complete I ran it in the test environment where the data was contained.  When the script ran the add on the SPView collection the script stalled with error: “Exception calling “Add” with “6” argument(s): “An error occurred while parsing EntityName.”  Well that error is really easy to under stand.

Determining the Cause

First let me show you the script so you can see what I was working with:

foreach($spListItem  in $sourceFieldList.Items)
        $viewTitle = $spListItem[$createViewsConfig.ListSourceField];
        $filterField = $createViewsConfig.FilterField

        #add the columns to display in the view
        $viewFields = New-Object System.Collections.Specialized.StringCollection;

        foreach($viewField in $createViewsConfig.ViewFields.ViewField)
            $fieldInternalName = $viewList.Fields[$viewField].StaticName;
            $viewFields.Add($fieldInternalName) | Out-Null;

        $viewQuery = 
                        <FieldRef Name ='$filterField' />
                        <Value Type='Lookup'>$viewTitle</Value>               
        $viewRowLimit = [int]$createViewsConfig.ViewRowLimit;
        $newLibraryView = $viewList.Views.Add($viewTitle, $viewFields, $viewQuery, 30, $true, $false);

            $logMessage = ("Successfully created view: {0} in list\library: {1} of site: {2}" -f $newLibraryView.Title, $viewList.Title, $spWeb.Url);
            AppendToLog $fullLogFilePath $logMessage;
            $logMessage = ("No error occurred, but failed to created view: {0} in list\library: {1} of site: {2}" -f $newLibraryView.Title, $viewList.Title, $spWeb.Url);
            AppendToLog $fullLogFilePath $logMessage;

The code is actually reading from a config file that contains information like the site URL, list\library for the views, lookup list containing the view names, a list of fields to display in the view and a few other options.  The snippet here is looping through each item in the lookup list and then building the view off of that.

This isn’t anything I haven’t done before, but for the life of me I couldn’t find the culprit nor anyone else who had it when adding a view.  I spent some time manually writing out the code and variables in a PowerShell Command Window and didn’t have any problems.

I finally found the culprit when I captured the field it was trying to insert.  I don’t know why I didn’t do this first, it’s just been one of those days.

The Cause of my Woes and Why

The first item the view was grabbing from the list contained an ampersand (&).  I should have known better.  While SharePoint doesn’t mind if ampersands exist as metadata there are certain places you cannot have them around.  They can’t exist in a filename or anything that has to do with a hyperlink because it is used as a argument separator in your URL.  Since views are linkable (when you create a view it generates a corresponding html link) SharePoint doesn’t like it and has a fit when you try.  It does allow you to do it in the title of a view when you use the GUI, but I dare you to try it and then take a look at your view name.  Looks a little different doesn’t it.

Why the weird error message?

SharePoint doesn’t actually care about the & in the title… well it does, but it isn’t going to stop you.  The error occurs when SharePoint runs the process that creates the link.  The link is supposed to contain the title of the view.  There doesn’t appear to be any special code in place to fix the special characters, so SharePoint starts the process and then rolls back when the error occurs.  The error itself is a standard HTML\ASPX error when you are trying to use an & incorrectly.  This is where the error is coming from and SharePoint doesn’t do anything to hijack the message and make it more developer friendly.


Anyways, I hope my pain today helps you in the future.  Thanks for reading!