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

Forums / Developing with Sitefinity / How to retroactively populate a newly added metafield?

How to retroactively populate a newly added metafield?

6 posts, 0 answered
  1. ddblee
    ddblee avatar
    20 posts
    Registered:
    24 Jul 2008
    01 Dec 2008
    Link to this post
    I have added a metafield to Sitefinity that is used  by the Blogs module. I need to "backfill" this metafield with values for all currently existing blog posts. What is the best way to do this?

    Thanks!
  2. John
    John avatar
    32 posts
    Registered:
    08 Oct 2008
    01 Dec 2008
    Link to this post
    Hi,

    When you add a new meta field, Sitefinity creates a corresponding column in the database. All values in that column will be null and each record will have access to that column. To add values, you could open up your sitefinity database, select the sf_blogs table and run a query similar to the one:

    INSERT INTO sf_blogs (MyMetaField)
    VALUES ('1')
    Where MyMetaField is Null
    ~John
  3. ddblee
    ddblee avatar
    20 posts
    Registered:
    24 Jul 2008
    01 Dec 2008
    Link to this post
    Well, that would *kind of* work, though a much safer query would be along the lines of:
    update dbo.sf_GCMetaData
    set ShortText = @authorname
    where ContentID = @contentguid
    and KeyValue = 'Author'
    and Application = '/Blogs'
    and ShortText = ''

    No INSERTs would be required, as the fields already exist (and, in this case, are not even NULL -- they have empty string values).

    And I've been burned pretty badly trying to do custom SQL against Sitefinity's tables -- I'm hoping there is an API-driven way that this task can be done.

    Thanks!
  4. Pepi
    Pepi avatar
    981 posts
    Registered:
    31 Jan 2017
    02 Dec 2008
    Link to this post
    Hello Lee,

    We strongly recommend you use the API to achieve the required functionality:
        1. Create a new instance of ContentManager for the Blogs provider.
        2. Get all content items - use GetContent method of the manager.
        3. Iterate through the content items and set the value of the newly added meta field - use SetMetaData method of the IContent object.
        4. Save the content item - use SaveContent method of the manager.

    For details please refer to the following Developer Manual topics:
    Modifying Content Items
    Managing Posts

    Do let us know if you need any further assistance.

    Best wishes,
    Pepi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  5. bemara57
    bemara57 avatar
    135 posts
    Registered:
    27 Mar 2008
    18 Jul 2010
    Link to this post
    Instead of simply doing this:

    //UPDATE NEWS
    ContentManager manager = new ContentManager(NewsManager.DefaultContentProvider);
    IList list = manager.GetContent();
    int i = 0;
    foreach (IContent item in list)
    {
        item.SetMetaData("NewMetaKey", true);
        manager.SaveContent(item);
        i++;
    }
    Response.Write(i + " news items updated.");

    For some reason I had to do this (notice I had to get each item again):

    //UPDATE NEWS
    ContentManager manager = new ContentManager(NewsManager.DefaultContentProvider);
    IList list = manager.GetContent();
    List<Guid> ids = new List<Guid>();
    int i = 0;
    foreach (IContent item in list)
    {
        ids.Add(item.ID);
    }
    foreach (Guid item in ids)
    {
        IContent content = manager.GetContent(item);
        content.SetMetaData("NewMetaKey", true);
        manager.SaveContent(content);
        i++;
    }
    Response.Write(i + " news items updated.");

    I do not know what the problem is, but in my first case it just would not save. I had to just save the id's then get each item again one by one, then save. I do know if this is some bug or I am missing something. In any case, the latter case works.
  6. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    16 Jun 2017
    18 Jul 2010
    Link to this post
    Hi bemara57,

    The problem is that if you do not get the content by using GetContent it is not in an transaction and you cannot make any changes to the object.

    Best wishes,
    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
6 posts, 0 answered