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

Forums / Developing with Sitefinity / SetMetaData not persisting in database

SetMetaData not persisting in database

10 posts, 0 answered
  1. Gilbert van Ooijen
    Gilbert van Ooijen avatar
    6 posts
    Registered:
    27 Aug 2009
    02 Oct 2009
    Link to this post
    Hi there,

    Currently we are developing some custom modules. One of them reads the newsitems from SiteFinity.

    Those IContent items have a metadatafield set in the web.config

    Now i want to change the property of that field at a certain time. This doesn't store in the database.

    Code :
            NewsManager newnewsManager = new NewsManager("News");  
     
            mNewsItem.SetMetaData("ItemISMailItem", false);  
            newsManager.Content.SaveContent(mNewsItem); 

    mNewsItem is Icontent .

    Do the IContent items need to be in draft to be changed or something ?

  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    02 Oct 2009
    Link to this post
    Hello Gilbert van Ooijen,

    As I see you have two instance of NewsManager class "newnewsManager" and "newsManager", but I could not see the declaration of the second one. The code below works fine for each of the standard Generic Content based modules.

    var newnewsManager = new NewsManager("News");   
    IContent mNewsItem = newnewsManager.Content.GetCurrentState(new Guid("7257CC87-8ECE-4CB4-A6D2-18F4ED647217")); 
    mNewsItem.SetMetaData("TrueOrFalse"false ); 
    newnewsManager.Content.SaveContent(mNewsItem, ContentStatus.Published);  


    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. Gilbert van Ooijen
    Gilbert van Ooijen avatar
    6 posts
    Registered:
    27 Aug 2009
    02 Oct 2009
    Link to this post
    Something must have gone wrong because that is not the correct code that i posted

    I see that the editor add's the blue printed new.

     

    NewsManager newsManager = new NewsManager("News");

     

    mNewsItem.SetMetaData(

    "ItemISMailItem", false);

     

    newsManager.Content.SaveContent(mNewsItem,

    ContentStatus.Published);

    This is the way it is.

    Yet the mNewsItem is a private member of the class that was filled on Page_Load. The code printed is hit on postback.

    It is still not persisting yet i see that the field "DateChange" updates every time the SaveContent is passed.

     

  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    02 Oct 2009
    Link to this post
    Hello Gilbert van Ooijen,

    I am not able to reproduce any problems with the code below:

                var newnewsManager = new NewsManager("News");   
                IContent mNewsItem = newnewsManager.Content.GetCurrentState(new Guid("7257CC87-8ECE-4CB4-A6D2-18F4ED647217")); 
                mNewsItem.SetMetaData("TEST1"true); 
                newnewsManager.Content.SaveContent(mNewsItem, ContentStatus.Published); 
                Response.Write(mNewsItem.GetMetaData("TEST1").ToString()); 

    metakey declaration:

    <add key="News.TEST1" valueType="Boolean" visible="True" searchable="True" sortable="True" defaultValue=""></add> 

    You can attach your code to debugger and see whether the value is altered somewhere else or send us an example that we could observe.

    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.
  5. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    03 Feb 2010
    Link to this post
    Hello,

    I am having the same problem.
    I am using Sitefinity 3.7 SP2. I have custom Module and I need a couple of meta fields just to persist check info. So I made them invisible :

    <add key="Retailers.Latitude" valueType="FloatingPoint" visible="False" searchable="True" sortable="True" defaultValue="0" />
    <add key="Retailers.Longitude" valueType="FloatingPoint" visible="False" searchable="True" sortable="True" defaultValue="0"  />
    <add key="Retailers.HasCoordinates" valueType="Boolean" visible="False" searchable="True" sortable="True"  defaultValue="False" />
    <add key="Retailers.Count" valueType="Integer" visible="False" searchable="True" sortable="True" defaultValue="0" />
    <add key="Retailers.DateSavedCoordinates" valueType="DateTime" visible="False" searchable="True" sortable="True" defaultValue="1/1/2000" />


    I am setting their values the following way :

    ContentManager manager = new ContentManager("Retailers");
                IList listOfContentItems = manager.GetContent();
     
                if (listOfContentItems.Count > 0)
                {
                    foreach (IContent content in listOfContentItems)
                    {
     
     content.SetMetaData("Longitude", decimal.ToDouble(retailer.Longitude));
                            content.SetMetaData("Latitude", decimal.ToDouble(retailer.Latitude));
                            content.SetMetaData("HasCoordinates", true);
                            content.SetMetaData("DateSavedCoordinates", DateTime.Now);
                            manager.SaveContent(content, ContentStatus.Published);
    }}

    If I am debugging and Get meta data immediately I see that their values are set.
    If I refresh the page and get in this "foreach" and try to get meta data - nothing - empty.

    I see the DateModified  - it is the date I saved the content.

    So why it is not in the DB?

    Please help ASAP,

    Thanks Nadya

  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    03 Feb 2010
    Link to this post
    Hello Nadya,

    Get the content as StagedContent and then try to save it. The value should be persisted.

    StagedContent stg = Manager.GetStagedContent(cntItem.ID) as StagedContent;


    Sincerely yours,
    Ivan Dimitrov
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
  7. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    03 Feb 2010
    Link to this post
    Hi Ivan,

    Thank you for the help, but this seems not to work.

    I have now 

    StagedContent stgContent = manager.GetStagedContent(content.ID) as StagedContent;
                            stgContent.SetMetaData("Longitude", decimal.ToDouble(retailer.Longitude));
                            stgContent.SetMetaData("Latitude", decimal.ToDouble(retailer.Latitude));
                            stgContent.SetMetaData("HasCoordinates", true);
                            stgContent.SetMetaData("DateSavedCoordinates", DateTime.Now);
                            manager.SaveContent(stgContent, ContentStatus.Published);

    but I always get an Invalid cast exception for the manager.SaveContent(...).

    I can see that StageContent is implementing IContent and that the method wants IContent , but still. And there's no additional info about it - just "System.InvalidCastException: Specified cast is not valid."

    Am I missing something.
    The version of Telerik.Cms.Engine.dll is 3.7.2022.2.

    Thanks,
    Nadya

  8. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    03 Feb 2010
    Link to this post
    Hi,

    It seems the problem was local - I did an ultimate restart - reboot everything and the exception disappeared.

    Thank you very much and sorry for bothering the second time .

    Best Regards, 
    Nadya
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    03 Feb 2010
    Link to this post
    Hi Nadya,

    Generally you should be able to save the key without getting the SagedContent object.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
    Follow the status of features or bugs in PITS and vote for them to affect their priority.
  10. Nadya
    Nadya avatar
    52 posts
    Registered:
    17 Dec 2008
    03 Feb 2010
    Link to this post
    Hi Ivan,

    Sorry to disagree, but it is not saving it if object is not Staged Content.

    I found several articles and posts  in Support that have code for changing meta field value and all are using Staged Content. These that are not - are having problems.

    I am not sure why it is not saving it. As I said it looks like it is saved but on the next attempt (I mean new Content manager, new call to GetContent() ) to get the value of the meta field - it is empty. Is it possible to save it just for the current instance of the Content Manager and not actually in DB? 

    Anyway you fixed my issue, so thanks again :)

    Have a good evening.

Register for webinar
10 posts, 0 answered