More in this section

Forums / Developing with Sitefinity / Work with new metafields in News

Work with new metafields in News

4 posts, 0 answered
  1. MJia
    MJia avatar
    44 posts
    Registered:
    09 Jun 2009
    12 Mar 2010
    Link to this post
    I successfully added some new metafields in News, and also successfully customized NewsItemNew.ascx and NewsItemEdit.ascx by adding proper controls into original <fieldset><ol><li> list.
    Now I would like to do more complex customizations. Such as:
    1. when user click 'Create This News Item', do some pre-processing based on the value they input, then save the result to one metafield;
    2. when user enter the News edit page, do some pre-processing based on the value of one metafield, then display the result

    How should I do?

    Thanks.
  2. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    14 Mar 2010
    Link to this post
    Hi MJia,

    You have to create a custom views of NewsItemNew and NewsItemEdit. In NewsItemNew, override SaveContent() method and before calling the base execute your logic. In NewsItemEdit override - SetContentForm method from where you can made some changes. Another option for manipulating the data in NewsItemEdit view is crating a code behind of the template and make changes on Page_Load or PreRender. You may find the following post useful - Replacing views.

    All the best,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. MJia
    MJia avatar
    44 posts
    Registered:
    09 Jun 2009
    15 Mar 2010
    Link to this post
    Thanks Ivan.
    1. I created a code behind of the NewsItemEdit layout template, but how do I retrieve the metafield value in Page_Load event?  Do you have any sample code?
    2. Can I use similar way for NewsItemNew? e.g., create a code behind and override SaveContent() there? Because I've already created customized layout template for NewsItemNew, I added some controls (textbox, gridview, etc.), now I need to process the inputs of these controls, and then save to a metafield of News.

    Thanks.
  4. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    12 Sep 2017
    15 Mar 2010
    Link to this post
    Hi MJia,

    1. Find the control you are looking for on Page_Load or PreRender.

    sample
    //find content meta fields control where you should have metakey controls
    ContentMetaFields cmf = e.Item.FindControl("Default") as ContentMetaFields;
    // find your control
     TextBox customkey = cmf.Controls[0].FindControl("customkey ") as TextBox ;


    2. You can not use the same logic in NewsItemNew because actually there is not item you can get. In your custom view that should inherit from NewsItemNew you have to override SaveContent and process your logic

    sample

    protected override void SaveContent()
        {
     
            IContent cnt = null;
            Guid itemID = Guid.Empty;
            itemID = this.DataItemId;
                if (itemID == Guid.Empty)
                {
                    cnt = this.Manager.CreateContent("text/html");
                    ....
                          ....
                    Manager.SaveContent(cnt);
                }
     
        }


    The better option is using custom views, instead of working with the templates.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Register for webinar
4 posts, 0 answered