Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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. http://social.technet.microsoft.com/Forums/sk/sharepoint2010programming/thread/5618e800-c4b5-4308-a5d8-c509aabb120e

My code is as follows:

using (SPSite spsite = new SPSite(strurl))
                {
                    using (SPWeb currentWeb = spsite.OpenWeb())
                    {
                        try
                        {
                            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;
                            //reasonReturnedField.Update();

                            SPFieldLink fieldLink = new SPFieldLink(currentWeb.AvailableFields.GetField(reasonReturnedField.InternalName));
                            //Check if the Field reference exists
                            if (!agendaPointsProposedCT.Fields.ContainsField(reasonReturnedField.Title))
                            {
                                agendaPointsProposedCT.FieldLinks.Add(fieldLink);
                                agendaPointsProposedCT.Update(true);
                            }
                            MoveFieldInColumnOrderToLastPosition(currentWeb, Meetings.Common.Constants.CONTENTTYPES_AGENDAPOINTPROPOSED_NAME, reasonReturnedField.InternalName);
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError("XXX", "AddReasonReturnedFieldsToContentTypeAgendaPointProposed(SPListItem item, SPWeb elevatedTargetWeb)", ex);
                            throw;
                        }
                        finally
                        {
                            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];
            }
            else
            {
                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
                };
                spFieldCollection.Add(fieldHtml);            
            }
            return fieldHtml;
        }
share|improve this question
add comment

2 Answers 2

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? –  L.V. Sharepoint Architect 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 –  L.V. Sharepoint Architect Sep 14 '12 at 10:57
    
any idea? thanks –  L.V. Sharepoint Architect 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
add comment

Your Answer

 
discard

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.