Lars Nielsen's Discoveries

February 10, 2011

Site column gallery and “The given key was not present in the dictionary”

Filed under: Customization,SharePoint,Troubleshooting — Lars Nielsen @ 8:02 pm
Tags: ,

In creating a custom field type recently, I got my site collection into a state where I could no longer look at the site columns gallery in Site Settings.

The given key was not present in the dictionary

This happened after I had retracted the feature that deployed the custom field type, but there was still a field of that type left in the site column gallery. I eventually fixed this by redeploying the feature to bring back the user controls and fldtypes_OOOO.xml files for the custom field types.  That meant I could get to the site columns gallery, but there was one site column which I could not delete or edit because whenever I did, I got an exception screen.

I had to remove the offending site column through code.  To do this I followed through this thread and simplified the page code in there so I could be sure that I was doing what I intended to do, and deleting only the field that I wanted to delete.  There’s no way to recover a site column once it has been deleted – it doesn’t go in the recycle bin.

I created a page called DeleteMyField.aspx and put into the LAYOUTS folder in the 12 Hive.  This will delete a site column with the static name that you put into the call to DeleteField in the Page_Load method on line 14 of the listing below.  In this case the field is called “MyCustomFieldName” but you can substitute in whatever your site column is called.  Note that I had to set AllowUnsafeUpdates to true on the site.  To use this page you add this file into the LAYOUTS folder in all the web servers.  Then browse to the page in the site where the site column is.  For example if the site column is in this site:

http://mydomain.com/sites/HR/payroll

Then you would browse to:

http://mydomain.com/sites/HR/payroll/_layouts/DeleteMyField.aspx

Then after that, if you go to the site column gallery on the site you should find that the site column has been removed.

Here’s the code for the DeleteMyField.aspx page:

<% @ Page Language="C#" Debug="true" EnableSessionState="true" ValidateRequest="False" MasterPageFile="~/_layouts/application.master"%>
<% @ Import Namespace="Microsoft.SharePoint" %>
<% @ Import Namespace="Microsoft.SharePoint.Utilities" %>
<% @ Import Namespace="System.IO" %>
<% @ Import Namespace="System.Xml" %>
<script language="C#" runat="server">

private void Page_Load(object sender, EventArgs e)
{
  if (!IsPostBack)
  {
     try
     {
        DeleteField("MyCustomFieldName");
     }
     catch (Exception ex)
     {
        this.results.Text += ex.Message + "<br />" + ex.StackTrace + "<br />";
     }
  }
}

private void DeleteField(string fieldStaticName)
{
   SPWeb w = SPContext.Current.Site.RootWeb;
   w.AllowUnsafeUpdates = true;
   w.Update();
   this.results.Text += "Attempting to delete field " + fieldStaticName + "<br/>";
   w.Fields.Delete(fieldStaticName);
   this.results.Text += "Deleted OK<br/>";
   w.AllowUnsafeUpdates = false;
   w.Update();
}

</script>

<asp:content id="Content1" contentplaceholderid="PlaceHolderMain" runat="server">
<asp:literal runat="server" id="results" />
</asp:content>

Advertisements

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: