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

Forums / Developing with Sitefinity / Trouble Updating Meta Data With News Manager

Trouble Updating Meta Data With News Manager

3 posts, 1 answered
  1. James
    James avatar
    4 posts
    Registered:
    25 Aug 2012
    22 Sep 2009
    Link to this post
    Hi,

    I am using a modified news module with a custom Image management module for a property site.  The problem I am having is updating a listings PreviewImageID, which is a custom meta data field on the news item that holds the listing.  Watching the code in the debugger it looks as if it should work.

    This is what I have...

    A command field in a grid view with a "Set as preview" button
                if (e.CommandName.Equals("SelectAsPreview")) 
                { 
                    int imageID = Convert.ToInt32(e.CommandArgument); 
                    Images.SetImageAsMainPreview(imageID, QScontentMasterID); 
     
                    //if the item is published must also update the meta PreviewImageID field. 
                    if (((int)ContentMasterBLL.GetContentMasterMeta(QScontentMasterID).First().StatusID) == ((int)PublishStates.PublishedLive)) 
                    { 
                        SitefintyNews.UpdatePreviewImage(QScontentMasterID, imageID); 
                    } 
                    gridPhotos.DataBind(); 
                } 
            } 

    And this is the update method that doesn't work...
            public static bool UpdatePreviewImage(int contentMasterID, int previewImageID) 
            { 
                bool result = false
     
                NewsManager newsManager = new NewsManager(NewsManager.DefaultContentProvider); 
     
                List<IMetaSearchInfo> filter = new List<IMetaSearchInfo>(); 
     
                filter.Add(new MetaSearchInfo(MetaValueTypes.ShortText, "ContentMasterID", Convert.ToString(contentMasterID), SearchCondition.Equal)); 
     
                IList listOfNewsItems = newsManager.Content.GetContent(filter.ToArray()); 
     
                if (listOfNewsItems.Count > 0) 
                { 
                    foreach (IContent newsContent in listOfNewsItems) 
                    { 
                        newsContent.SetMetaData("PreviewImageID", Convert.ToString(previewImageID)); 
                        newsManager.Content.SaveContent(newsContent, ContentStatus.Published); 
                        result = true
                        break
                    } 
                } 

    I'm probably missing something very obvious, which is usually the case.

    Any ideas much appreciated.

    James
  2. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    26 Sep 2009
    Link to this post
    Hello James,

    Yes, it is something small :)

    The problem is that you are not getting the content item for editing (as Staged Content). To do so, consider the following code:
    ... 
            if (listOfNewsItems.Count > 0) 
            { 
                foreach (IContent newsContent in listOfNewsItems) 
                { 
                    IContent staged = newsManager.Content.GetStagedContent(newsContent.ID); 
                    staged.SetMetaData(........); 
                    newsManager.Content.SaveContent(staged, ContentStatus.Published); 
                } 
            }  

    Let me know if you have any further questions. 

    Sincerely yours,
    Georgi
    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.
    Answered
  3. James
    James avatar
    4 posts
    Registered:
    25 Aug 2012
    01 Oct 2009
    Link to this post
    Thanks Georgi - fixed!

    Best regards,

    James
Register for webinar
3 posts, 1 answered