locked
Last Updated column not updated when a reply is posted RRS feed

  • Question

  • I have a discussion board (Name: "Source") with the data as below:

    Discussion Item
    - Message Item 1
      - Message Item 1-1
      - Message Item 1-2

    My requirement is to copy the items from "Source" to another discussion board (Name: "Destination"). The items are copied successfully with the correct structure (e.g. "Message Item 1-1" is under "Message Item 1"...)

    The problem come when I reply a post under "Message Item 1-1", the "Last Updated" column is not updated. But when reply a message under "Discussion Item", the "Last Updated" column will get updated.

    This is code that I used to copy the discussion board items to the destination.

    SPList destList = web.Lists["Destination"];
    SPList sourceList = web.Lists["Source"];
    
    SPQuery query = new SPQuery();
    query.Query = "<Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Text\">1</Value></Eq></Where>";
    
    SPListItemCollection discussions = sourceList.GetItems(query);
    foreach (SPListItem discussion in discussions)
    {
      string title = discussion["Title"].ToString();
      string folderName = discussion.Folder.Name;
    
      SPListItem destDiscussion = destList.Items.Add(destList.RootFolder.ServerRelativeUrl, SPFileSystemObjectType.Folder, folderName);
      destDiscussion["ContentType"] = discussion["ContentType"].ToString();
      destDiscussion["Title"] = title;
      destDiscussion["Modified"] = DateTime.Parse(discussion["Modified"].ToString());
      destDiscussion["Created"] = DateTime.Parse(discussion["Created"].ToString());
      SPFieldUserValue authorValue = new SPFieldUserValue(web, discussion["Author"].ToString());
      destDiscussion["Author"] = authorValue.LookupId;
      SPFieldUserValue editorValue = new SPFieldUserValue(web, discussion["Editor"].ToString());
      destDiscussion["Editor"] = editorValue.LookupId;
      if (discussion["Body"] != null)
        destDiscussion["Body"] = discussion["Body"].ToString();
      if (discussion["DiscussionLastUpdated"] != null)
        destDiscussion["DiscussionLastUpdated"] = DateTime.Parse(discussion["DiscussionLastUpdated"].ToString());
      if (discussion["ThreadIndex"] != null)
        destDiscussion["ThreadIndex"] = discussion["ThreadIndex"].ToString();
    
      destDiscussion.Update();
    
      SPQuery queryPost = new SPQuery();
      queryPost.Query = "<Where><Eq><FieldRef Name=\"FSObjType\" /><Value Type=\"Text\">0</Value></Eq></Where>";
      queryPost.Folder = web.GetFolder(discussion.Url);
    
      int i = 0;
      SPListItemCollection posts = sourceList.GetItems(queryPost);
      foreach (SPListItem post in posts)
      {
        SPListItem destPost = destList.Items.Add(destDiscussion.Folder.ServerRelativeUrl, SPFileSystemObjectType.File, null);
        destPost["ContentType"] = post["ContentType"].ToString();
        destPost["Modified"] = DateTime.Parse(post["Modified"].ToString());
        destPost["Created"] = DateTime.Parse(post["Created"].ToString());
        SPFieldUserValue authorPostValue = new SPFieldUserValue(web, post["Author"].ToString());
        destPost["Author"] = authorPostValue.LookupId;
        SPFieldUserValue editorPostValue = new SPFieldUserValue(web, post["Editor"].ToString());
        destPost["Editor"] = editorPostValue.LookupId;
        if (post["Body"] != null)
          destPost["Body"] = post["Body"].ToString();
        if (post["TrimmedBody"] != null)
          destPost["TrimmedBody"] = post["TrimmedBody"].ToString();
        if (post["ThreadIndex"] != null)
          destPost["ThreadIndex"] = post["ThreadIndex"].ToString();
    
        destPost.Update();
    
        i++;
      }
    }
    

    I can't find any solution to resolve this. Anyone can help?

    Thanks in advance.


    jingzo (^_^)
    Thursday, June 2, 2011 7:33 AM

Answers

  • Hi Jingzo,

    I tested your code in my lab machine. It seems to have some issues. Add this line to your existing code while adding the replies to the destination list:

    if(post["ParentFolderId"] != null)
       destPost["ParentFolderId"] = post["ParentFolderId"].ToString();  // Note: this Id is the ItemId of your discussion post

    Besides, I will suggest that you use content deployment APIs to export data from one list to another. HTH! 

     


    AnjaliCH-MSFT
    • Marked as answer by jingzo Tuesday, June 21, 2011 11:33 PM
    Friday, June 17, 2011 11:03 PM

All replies

  • So what your actually doing is cloning those items to another list ?

    I woud personally just loop through all the fields.

    It should be something like this

     

        SPList destinationList = sourceItem.Web.Lists[destinationListName];
           
    SPListItem targetItem = destinationList.Items.Add();
           
    foreach (SPField f in sourceItem.Fields) {
               
     
               
    if (!f.ReadOnlyField && f.InternalName != "Attachments"
                   
    && null != sourceItem[f.InternalName])
               
    {
                    targetItem
    [f.InternalName] = sourceItem[f.InternalName];
               
    }
           
    }
           



    Sebastian
     


    • Edited by Mike Walsh FIN Wednesday, June 22, 2011 6:36 AM "Mark as answer if that is what you're looking for." NEVER ask people to mark your posts as answers
    Thursday, June 9, 2011 5:55 AM
  • I have no problem duplicating the discussion board list items to a new discussion board.

    The problem come in when replying under the reply item (Message Item), the "Last Updated" column is not being update.


    jingzo (^_^)
    Saturday, June 11, 2011 5:29 AM
  • Hi Jingzo,

    I tested your code in my lab machine. It seems to have some issues. Add this line to your existing code while adding the replies to the destination list:

    if(post["ParentFolderId"] != null)
       destPost["ParentFolderId"] = post["ParentFolderId"].ToString();  // Note: this Id is the ItemId of your discussion post

    Besides, I will suggest that you use content deployment APIs to export data from one list to another. HTH! 

     


    AnjaliCH-MSFT
    • Marked as answer by jingzo Tuesday, June 21, 2011 11:33 PM
    Friday, June 17, 2011 11:03 PM
  • Thank you for your replies.

    I will test your suggested code on my side.


    jingzo (^_^)
    Saturday, June 18, 2011 3:47 AM
  • Hi Jingzo,

    How did the test results go?

     


    AnjaliCH-MSFT
    Tuesday, June 21, 2011 6:34 PM
  • Yes, it's working... post["ParentFolderId"].ToString() will need to be the destination discussion id.

    Thanks =)


    jingzo (^_^)
    Tuesday, June 21, 2011 11:33 PM