+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Generic Content: "The given key was not present in the dictionary" Exception

Generic Content: "The given key was not present in the dictionary" Exception

12 posts, 0 answered
  1. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    29 Oct 2009
    Link to this post
    I'm running Sitefinity 3.7 SP1 and am unable to set a metadata value on a Generic Content based provider. I get an exception: The given key was not present in the dictionary.

    Here's the code:
                ContentManager GCManager = new ContentManager("PageEx"); 
                IContent PageProvider = GCManager.CreateContent("text/html"); 
                GCManager.SaveContent(PageProvider); 
                Guid m_GCID = PageProvider.ID; 
     
                PageProvider = GCManager.GetContent(m_GCID); 
                PageProvider.SetMetaData("Headline""This is a test"); 
                GCManager.SaveContent(PageProvider); 
     

    And here's the definition in web.config:
                <providers> 
                    <clear/> 
                    <add name="Generic_Content" urlRewriteFormat="[Publication_Date]/[Name].aspx" urlDateTimeFormat="yy-MM-dd" urlWhitespaceChar="_" visible="True" defaultMetaField="Name" securityProviderName="" allowLocalization="False" allowVersioning="True" allowWorkflow="False" allowComments="false" commentsModeration="true" versioningProviderName="" connectionStringName="GenericContentConnection" type="Telerik.Cms.Engine.Data.Providers.DefaultProvider, Telerik.Cms.Engine.Data"/> 
                    <add name="PageEx" urlRewriteFormat="~/{Provider}/[Publication_Date]/[Guid].aspx" urlDateTimeFormat="yy-MM-dd" urlWhitespaceChar="_" visible="True" defaultMetaField="Headline" securityProviderName="" allowLocalization="False" allowVersioning="True" allowWorkflow="False" allowComments="True" commentsModeration="true" versioningProviderName="" connectionStringName="GenericContentConnection" type="Telerik.Cms.Engine.Data.Providers.DefaultProvider, Telerik.Cms.Engine.Data"/> 
                </providers> 
                <metaFields> 
                    <add key="PageEx.Headline" valueType="ShortText" visible="True" searchable="True" sortable="True" defaultValue=""/> 
                </metaFields> 

    Am I missing something? I've had a generic content provider working in the past. I'm not sure why I can't get this to work.

    Thanks,
    Jeff
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    02 Nov 2009
    Link to this post
    Hello Jeff,

    Could you send us the stack trace of the error? The code seems to be correct.

    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  3. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    02 Nov 2009
    Link to this post
    Sure. Here it is:

    The given key was not present in the dictionary. 
     
    Stack Trace: 
       at System.ThrowHelper.ThrowKeyNotFoundException() 
       at System.Collections.Generic.Dictionary`2.get_Item(TKey key) 
       at Telerik.Cms.Engine.Data.ContentVersion.SetMetaData(String key, Object value) 
       at Telerik.Cms.Engine.Data.CmsContentBase.SetMetaData(String key, Object value) 
       at Devise.Content.Pages.DevisePage.SetMetaData(String strKey, Object Value, Boolean bSave) in D:\Devise\Devise\Content\Pages\DevisePage.cs:line 488 
     

    I'm looking forward to getting to the bottom of this.

    Thanks,
    Jeff
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    02 Nov 2009
    Link to this post
    Hi Jeff,

    The error seems to be thrown from the metakey. The error says that the key does not exists. I tried to reproduce the problem at my end with no avail. Can you create a simple control ( just a  button where you create a new item and set the key) and check whether the Metadata of the newly created IContent object will be set?

    All the best,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  5. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    02 Nov 2009
    Link to this post
    Hi Ivan,

    Actually the code I posted originally is exactly what you're asking for: It's a button that creates a new content item and then tries to set a metadata value. However, the stack trace was from my development code. I apologize for the confusion.

    Here's the stack trace from my sample code:
    The given key was not present in the dictionary.  
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.  
     
    Exception Details: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. 
     
    Source Error:  
     
     
    Line 539: 
    Line 540:           PageProvider = GCManager.GetContent(m_GCID); 
    Line 541:           PageProvider.SetMetaData("Headline", "This is a test"); 
    Line 542:           GCManager.SaveContent(PageProvider); 
    Line 543: 
      
     
    Source File: d:\Program Files\telerik\Sitefinity3.6\WebSites\Take7\PageWithGenerics.aspx.cs    Line: 541  
     
    Stack Trace:  
     
     
    [KeyNotFoundException: The given key was not present in the dictionary.] 
       System.ThrowHelper.ThrowKeyNotFoundException() +28 
       System.Collections.Generic.Dictionary`2.get_Item(TKey key) +7454012 
       Telerik.Cms.Engine.Data.ContentVersion.SetMetaData(String key, Object value) +155 
       Telerik.Cms.Engine.Data.CmsContentBase.SetMetaData(String key, Object value) +50 
       Devise.UI.PageWithGenerics.Button3_Click(Object sender, EventArgs e) in d:\Program Files\telerik\Sitefinity3.6\WebSites\Take7\PageWithGenerics.aspx.cs:541 
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111 
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110 
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10 
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13 
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36 
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565 
     
      
     
     
    -------------------------------------------------------------------------------- 
    Version Information: Microsoft .NET Framework Version:2.0.50727.3603; ASP.NET Version:2.0.50727.3082  

    Thanks for your help.

    Jeff
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    03 Nov 2009
    Link to this post
    Hi Jeff,

    The stack trace that the metakey you use cannot be found. Try adding the code below to isolate the issue.

    string MetaKey = "Headline";
    // GETS ALL METAKEYS FOR YOUR PROVIDER
    foreach (MetaInfo meta in GCManager.MetaKeys.Values)
    {
        if (meta.Caption == MetaKey)
        {
            // ADD YOUR LOGIC HERE
        }
    }

    You can also run the code in debug mode and see what the debugger will show.

    Best wishes,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  7. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    03 Nov 2009
    Link to this post
    Hi Ivan,

    I tried the code you provided. Unfortunately, there are no MetaKeys in GCManager.MetaKeys.Values.
    If I use "Generic_Content" as the provider, I get all of its MetaKeys.

    So, I'm still stuck. Is there anything else I can send you or anything else to try?

    Thanks,
    Jeff
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    03 Nov 2009
    Link to this post
    Hi Jeff,

    Since we cannot reproduce the problem locally if you could prepare a simple project and provide us with a link from where we could download it. Generally the problem seems to be in the logic you use.

    Also check whether the provider is accessed

    foreach (KeyValuePair<string, ContentProviderBase> item in ContentManager.Providers)
           {
               Response.Write(item.Key.ToString() + "<br />");
           }

    If you copy paste the Generic_Content provider ( just rename it) and copy paste it metakeys does the same problem persists?

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  9. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    04 Nov 2009
    Link to this post
    Hi Ivan,

    My PageEx provider shows up in ContentManager.Providers. If you drill down to MetaKeys in the debugger, there are none for PageEx.

    I created a Headline MetaKey for Generic_Content by copying and pasting the PageEx.Headline entry in web.config. The metakey shows up for the Generic_Content provider.

    As far as getting you a sample project, what do you need me to do, exactly. Do you want me to:

    1. Create a new Sitefinity project
    2. Add the PageEx Provider to it
    3. Create a page with the MetaKey sample code

    If there's an easier way let me know. Otherwise, I'll get it to you.

    Thanks,
    Jeff
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    04 Nov 2009
    Link to this post
    Hi Jeff,

    I need a project where the problem can be reproduced. If you want first you can send the code of the control you use ( remove all reference to external classes) and your web.config. If  we are not able to reproduce the problem I will need sample project where you reproduce the problem.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
  11. Jeff
    Jeff avatar
    124 posts
    Registered:
    05 Aug 2007
    04 Nov 2009
    Link to this post
    Hi Ivan,

    I solved the problem. It was an error in web.config for a different provider that caused a problem for my PageEx provider.

    I created a new Sitefinity project and, of course, the provider worked just fine. So, I went back to my project in development and looked again at Sitefinity.log. That's when I noticed this inner exception:

    - L2 ------------------------------------------------------------------------------- 
     
    Exception Type: System.Configuration.ConfigurationErrorsException 
     
    Message: Missing provider: Sidebar 
     
    Source: Telerik.Cms.Engine 
     
    Stack Trace:  
       at Telerik.Cms.Engine.ContentManager.OnInitialized() 
       at Telerik.ManagerBase`1.Initialize() 
       at Telerik.ManagerBase`1..ctor(String providerName) 
       at Telerik.Cms.Engine.ContentManager..ctor(String providerName) 
       at Telerik.Cms.Engine.ContentManager..ctor() 
       at Telerik.Cms.Engine.ContentManager.get_Providers() 
       at Telerik.Blogs.BlogManager..cctor() 
    ------------------------------------------------------------------------------------ 
     

    When upgrading to 3.7 SP1, I had failed to define the Sidebar provider in <CmsEngine><providers>. However, I copied over the metafields for that provider.

    When Sitefinity threw an exception, it gave up on loading the remaining providers.

    So, my fault for fumbling through the tricky upgrade process. However, it would be nice if Sitefinity had a try/catch block in the loop that loads the providers. That way, if one fails to load, it doesn't take the rest down with it.

    Thanks for helping me through this.

    Jeff
  12. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    04 Nov 2009
    Link to this post
    Hello Jeff,

    I am glad to hear that finally the issue has been sorted out.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
Register for webinar
12 posts, 0 answered