Answered How to Read All Discussions and Replies

  • Tuesday, November 06, 2007 11:03 PM
     
     

    Hi,

     

    I have to read all discussions and replies of a sharepoint discussion board using object model.

    Any help is greatly appreciated.

     

    Thanks,

    Tushar

Answers

  • Wednesday, November 14, 2007 6:38 PM
     
     Answered

    Finally I was able to solve this puzzle. Thought of sharing for people still having this issue:

     

    1) Take instance of your site collection and web:

    SPSite currentSite = SPContext.Current.Site;
    SPWeb currentRootWeb = currentSite.RootWeb;

     

    2) Suppose I have the name of discussion list - "MyDiscussionList". Get GUID of this list:

     

    Guid myDiscussionListGUID = Guid.NewGuid();

    foreach (SPList list in currentRootWeb.Lists)

    {

          if (list.BaseTemplate.ToString() == "DiscussionBoard" && list.Title == "MyDiscussionList")

          {

                myDiscussionListGUID = list.ID; // Read GUID of Discussion List

                break;

           }

    }

     

    Tips:

    a) list.ItemCount will return all discussions and their replies

    b) list.Items.Count will return only replies

     

    3) Get your list:

    SPList myDiscussionList = currentRootWeb.Lists.GetList(myDiscussionListGUID, false);

     

    4) Create a new Discussion

    SPListItem newItem = SPUtility.CreateNewDiscussion(myDiscussionList.Items, "New Message using code");
    newItem["Body"] = "My new message content using code";
    newItem.Update();

     

    5) How to read all discussions:

    foreach (SPListItem folder in myDiscussionList.Folders)
    {
         Response.Write("Folder Name: " + folder.Name + "<BR>");
         Response.Write("Folder ID: " + folder.ID + "<BR>");
         Response.Write("Attachments Count: " + folder.Attachments.Count + "<BR>"); // Returns attachment count


         // Code to read attachment URL.

         for (int i = 0; i < folder.Attachments.Count; i++)
         {
                Response.Write("Attachment Url " + folder.Attachments.UrlPrefix + folder.AttachmentsIdea + "<BR>");
         }

                

         // Read body of attachment

         Response.Write("Body: " + folder.Fields["Body"].GetFieldValueAsText(folder["Body"]) + "<BR>"); 
    }

     

    6) If you want to delete a discussion

    SPListItem listItemParentToDelete = null;

     

    foreach (SPListItem folder in myDiscussionList.Folders)
    {
          listItemParentToDelete = folder;

    }

     

    if (listItemParentToDelete != null)
    {
        listItemParentToDelete.Delete();
    }

    7) Loop through all discussion replies:

    foreach (SPListItem listItem in myDiscussionList.Items)

    {

         Response.Write("Item DisplayName: " + listItem.DisplayName + "<BR>"); // Returns Title of Discussion
         Response.Write("List ID: " + listItem.ID + "<BR>");
         Response.Write("List Folder ID: " + listItem.Fields["Parent Folder Id"].GetFieldValueAsText(listItem["Parent Folder Id"]).ToString() + "<BR>"); // Returns ID of Parent Discussion
         Response.Write("Body: " + listItem.Fields["Body"].GetFieldValueAsText(listItem["Body"]) + "<BR>");
         

         // Create Parent List Item
         int parentListID = Convert.ToInt32(listItem.Fields["Parent Folder Id"].GetFieldValueAsText(listItem["Parent Folder Id"]));
         SPListItem parentListItem = lvContentItemsDiscussionsList.GetItemById(parentListID);
         Response.Write("Parent List Item Name: " + parentListItem.Name + "<BR>");

         

         // Code to Reply to a Discussion Message
         SPListItem reply = SPUtility.CreateNewDiscussionReply(parentListItem);
         reply["Body"] = "<div class=ExternalClass89C47CD7892B4279A8F42A65DD63AE3A><div> </div> <div>Reply to the new message<br><br> <hr> <b>From: </b>Admin<br><b>Posted: </b>Friday, July 20, 2007 4:01 AM<br><b>Subject: </b>New message<br><br> <div class=ExternalClass3D04672E599B486F9ECB76C138494708> <div>My new message content</div></div></div></div>";
         reply["TrimmedBody"] = "<div class=ExternalClass677134B4EA284660B1B236824800345C><div> </div> <div>Reply to the new message<br></div></div>";
         reply.Update();


        // Code to delete a discussion reply
        listItemToDelete = listItem;

    }

    8) Code to delete a discussion reply:

    SPListItem listItemToDelete = null;

     

    foreach (SPListItem listItem in myDiscussionList.Items)

    {

        listItemToDelete = listItem;

    }

     

    // Code to delete a discussion reply
    if (listItemToDelete != null)
    {
        listItemToDelete.Delete();
    }

     

    Note: You need to allow unsafe updates in order to do any operation on SharePoint list directly.

    currentRootWeb.AllowUnsafeUpdates = true;

     

    Hope this help!

     

    Thanks!

All Replies

  • Wednesday, November 14, 2007 6:38 PM
     
     Answered

    Finally I was able to solve this puzzle. Thought of sharing for people still having this issue:

     

    1) Take instance of your site collection and web:

    SPSite currentSite = SPContext.Current.Site;
    SPWeb currentRootWeb = currentSite.RootWeb;

     

    2) Suppose I have the name of discussion list - "MyDiscussionList". Get GUID of this list:

     

    Guid myDiscussionListGUID = Guid.NewGuid();

    foreach (SPList list in currentRootWeb.Lists)

    {

          if (list.BaseTemplate.ToString() == "DiscussionBoard" && list.Title == "MyDiscussionList")

          {

                myDiscussionListGUID = list.ID; // Read GUID of Discussion List

                break;

           }

    }

     

    Tips:

    a) list.ItemCount will return all discussions and their replies

    b) list.Items.Count will return only replies

     

    3) Get your list:

    SPList myDiscussionList = currentRootWeb.Lists.GetList(myDiscussionListGUID, false);

     

    4) Create a new Discussion

    SPListItem newItem = SPUtility.CreateNewDiscussion(myDiscussionList.Items, "New Message using code");
    newItem["Body"] = "My new message content using code";
    newItem.Update();

     

    5) How to read all discussions:

    foreach (SPListItem folder in myDiscussionList.Folders)
    {
         Response.Write("Folder Name: " + folder.Name + "<BR>");
         Response.Write("Folder ID: " + folder.ID + "<BR>");
         Response.Write("Attachments Count: " + folder.Attachments.Count + "<BR>"); // Returns attachment count


         // Code to read attachment URL.

         for (int i = 0; i < folder.Attachments.Count; i++)
         {
                Response.Write("Attachment Url " + folder.Attachments.UrlPrefix + folder.AttachmentsIdea + "<BR>");
         }

                

         // Read body of attachment

         Response.Write("Body: " + folder.Fields["Body"].GetFieldValueAsText(folder["Body"]) + "<BR>"); 
    }

     

    6) If you want to delete a discussion

    SPListItem listItemParentToDelete = null;

     

    foreach (SPListItem folder in myDiscussionList.Folders)
    {
          listItemParentToDelete = folder;

    }

     

    if (listItemParentToDelete != null)
    {
        listItemParentToDelete.Delete();
    }

    7) Loop through all discussion replies:

    foreach (SPListItem listItem in myDiscussionList.Items)

    {

         Response.Write("Item DisplayName: " + listItem.DisplayName + "<BR>"); // Returns Title of Discussion
         Response.Write("List ID: " + listItem.ID + "<BR>");
         Response.Write("List Folder ID: " + listItem.Fields["Parent Folder Id"].GetFieldValueAsText(listItem["Parent Folder Id"]).ToString() + "<BR>"); // Returns ID of Parent Discussion
         Response.Write("Body: " + listItem.Fields["Body"].GetFieldValueAsText(listItem["Body"]) + "<BR>");
         

         // Create Parent List Item
         int parentListID = Convert.ToInt32(listItem.Fields["Parent Folder Id"].GetFieldValueAsText(listItem["Parent Folder Id"]));
         SPListItem parentListItem = lvContentItemsDiscussionsList.GetItemById(parentListID);
         Response.Write("Parent List Item Name: " + parentListItem.Name + "<BR>");

         

         // Code to Reply to a Discussion Message
         SPListItem reply = SPUtility.CreateNewDiscussionReply(parentListItem);
         reply["Body"] = "<div class=ExternalClass89C47CD7892B4279A8F42A65DD63AE3A><div> </div> <div>Reply to the new message<br><br> <hr> <b>From: </b>Admin<br><b>Posted: </b>Friday, July 20, 2007 4:01 AM<br><b>Subject: </b>New message<br><br> <div class=ExternalClass3D04672E599B486F9ECB76C138494708> <div>My new message content</div></div></div></div>";
         reply["TrimmedBody"] = "<div class=ExternalClass677134B4EA284660B1B236824800345C><div> </div> <div>Reply to the new message<br></div></div>";
         reply.Update();


        // Code to delete a discussion reply
        listItemToDelete = listItem;

    }

    8) Code to delete a discussion reply:

    SPListItem listItemToDelete = null;

     

    foreach (SPListItem listItem in myDiscussionList.Items)

    {

        listItemToDelete = listItem;

    }

     

    // Code to delete a discussion reply
    if (listItemToDelete != null)
    {
        listItemToDelete.Delete();
    }

     

    Note: You need to allow unsafe updates in order to do any operation on SharePoint list directly.

    currentRootWeb.AllowUnsafeUpdates = true;

     

    Hope this help!

     

    Thanks!

  • Wednesday, January 23, 2008 6:49 PM
     
     

     

    I am trying to get the Subject field with GetFieldValueAsText but i always get an empty string on a sp2007 discussion list
  • Thursday, January 24, 2008 11:20 AM
     
     
    This is clearly ("Using object model") a Programming issue so I'm transfering the thread to the forum for such questions (= Development and Programming)

    Mike Walsh
  • Tuesday, April 08, 2008 4:45 PM
     
     

    Hi

     

    I am a total novice on the programming side. Could you detail where the code is to be inserted? Is it in an existing .aspx page or do you create a new one and call it

     

    Thanks

     

    schand

  • Tuesday, April 08, 2008 6:07 PM
     
     

    You can use this if you have custom UI to work on MOSS discussion list. Means via your .aspx page you are working on MOSS discussion list. You need to use SharePoint object model descibed above to access/update discussion list in SharePoint database.

     

    Hope this help!

     

    ~Tushar

  • Friday, May 23, 2008 7:24 PM
     
     

    Hi,

     

    It works fine w.r.t. createnewdiscussion/createnewdiscussionreply.

     

    However, I am not able to see it in a single thread.  How can I do that?

     

    I am creating a new discussion list.  I tried both the approach.

    1) ListItems.Add() method.
    2) CreateNewDiscussion() method.

     

    This discussion list I created is using the code.  I also created a reply using the code.

     

    Now when I see it in Sharepoint discussion list, it is not showing as a single item with replies as 1. 

     

    This is the normal behavior, if I create a discussion list it in the site.  I want the discussion lists that is created from my code to be similar to the one that is created in a discussion list.

     

    What am I missing and how can I acheive it?

     

    P.S : I saw some forums saying we need to add folders when creating the item using Add () method.  I dont know how to do that.

     

    I tried to get the splistitemcollection using list.folders, only the items that were created from the discussion list forum were in the list collection and the items I created using code were not there..

     

    Please any insights to this will be highly appreciated.

     

    Thanks