SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

THe following code throws me an exception, System.ArgumentNullException: Key cannot be null. Parameter name: key at System.Collections.Hashtable.get_Item(Object key) at Microsoft.SharePoint.SPFieldLinkCollection.Exists(String name, SPFieldLink& fldFound)

also here: but nobody answered.

My code is as follows:

using (SPSite spsite = new SPSite(strurl))
                    using (SPWeb currentWeb = spsite.OpenWeb())
                            currentWeb.AllowUnsafeUpdates = true;
                            SPContentType agendaPointsProposedCT = currentWeb.ContentTypes[Meetings.Common.Constants.CONTENTTYPES_AGENDAPOINTPROPOSED_NAME];
                            SPField reasonReturnedField = CreateHtmlField(currentWeb, Meetings.Common.Constants.FIELDS_AGENDAPOINTSREASONRETURNED_NAME);
                            //SPField reasonReturnedField = currentWeb.Fields.GetFieldByInternalName(reasonReturnedFieldName);

                            reasonReturnedField.Group = "$Resources:SPNLXXX,Field_XXXColumns_Group";                           
                            string schemaXmlWithResourceTokens = reasonReturnedField.SchemaXmlWithResourceTokens;
                            string displaynamelocalized = "$Resources:SPNLXXX,Field_ReasonReturned_Name";
                            string returnValue;
                            int indexOfAttributeName = schemaXmlWithResourceTokens.IndexOf("DisplayName", StringComparison.InvariantCultureIgnoreCase);
                            int indexOfAttibuteValueBegin = schemaXmlWithResourceTokens.IndexOf('"', indexOfAttributeName);
                            int indexOfAttributeValueEnd = schemaXmlWithResourceTokens.IndexOf('"', indexOfAttibuteValueBegin + 1);
                            returnValue = schemaXmlWithResourceTokens.Substring(0, indexOfAttibuteValueBegin + 1) + displaynamelocalized + schemaXmlWithResourceTokens.Substring(indexOfAttributeValueEnd);
                            reasonReturnedField.SchemaXml = returnValue;

                            SPFieldLink fieldLink = new SPFieldLink(currentWeb.AvailableFields.GetField(reasonReturnedField.InternalName));
                            //Check if the Field reference exists
                            if (!agendaPointsProposedCT.Fields.ContainsField(reasonReturnedField.Title))
                            MoveFieldInColumnOrderToLastPosition(currentWeb, Meetings.Common.Constants.CONTENTTYPES_AGENDAPOINTPROPOSED_NAME, reasonReturnedField.InternalName);
                        catch (Exception ex)
                            Logger.LogError("XXX", "AddReasonReturnedFieldsToContentTypeAgendaPointProposed(SPListItem item, SPWeb elevatedTargetWeb)", ex);
                            currentWeb.AllowUnsafeUpdates = false;

private static HtmlField CreateHtmlField(SPWeb currentWeb, string fieldName)
            HtmlField fieldHtml;
            fieldName = fieldName.Trim();
            SPFieldCollection spFieldCollection = currentWeb.Fields;
            if (currentWeb.AvailableFields.ContainsFieldWithStaticName(fieldName))
                fieldHtml = (HtmlField)spFieldCollection[fieldName];
                fieldHtml = new HtmlField(spFieldCollection, "HTML", fieldName)
                    Title = fieldName,
                    Group = "$XXX,Field_XXXColumns_Group",
                    ShowInEditForm = true,
                    StaticName = fieldName,
                    RichText = true,
                    RichTextMode = SPRichTextMode.FullHtml,
                    AllowHyperlink = true,
                    AllowDeletion = true
            return fieldHtml;
share|improve this question

Check that your reasonReturnedField.Title and fieldName are not null.

I can not see on MSDN that is is suppose to throw any exception, not even by inherited classes. But by disassembling SPFieldCollection I see it calls this.DisplayNameDict.ContainsKey((object) fieldName) which is Dictionary that according to MSDN (ContainsKey) will throw the exception you reference.

share|improve this answer
if I do that, then the fieldlink wont be added. I am missing something else here? – Esteban V Sep 14 '12 at 10:44
First off, in theory eetings.Common.Constants.FIELDS_AGENDAPOINTSREASONRETURNED_NAME is internal name, so you could use that instead of reasonReturnedField.Title. But at the same time this might mean that if (currentWeb.AvailableFields.ContainsFieldWithStaticName(fieldName)) is true and it returns the field by static name, but perhaps it does not have Title (internal name) and that would fail when you try to use it later – eirikb Sep 14 '12 at 10:50
SPFieldLink fieldLink = new SPFieldLink(currentWeb.AvailableFields.GetField(reasonReturnedField.InternalName‌​)); internalname is null, I dont understand it..7 – Esteban V Sep 14 '12 at 10:57
any idea? thanks – Esteban V Sep 14 '12 at 11:24
I believe it might as I said, the reasonReturnedField exists by static name and is return that way. I suggest you set a breakpoint at the start and step through the whole code, keeping an eye on reasonReturnedField – eirikb Sep 14 '12 at 11:26
up vote 0 down vote accepted

Finally, I was able to solve it:

Solution I found is here:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.