Retrieve Discussion Lists Topics and Its Replies with Its Author value using CSOM

In this blog, we are going to retrieve all topics including replies and their authors values of a discussion list .

Below image shows a discussion list having  topic and its replies from different authors . In our blog we will see how to retrieve it using CSOM programmatically.

using System;
using Microsoft.SharePoint.Client;
using System.Security;

namespace DiscussionList
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext ctx = new 
   ClientContext("https://portal.sharepoint.com/sites/testsite");
            string password = "Password";
            SecureString secureString = new SecureString();


            foreach (char c in password.ToCharArray())
            {
              secureString.AppendChar(c);
            }
            ctx.Credentials = new 
       SharePointOnlineCredentials("user@domain.onmicrosoft.com", secureString);

           List list = ctx.Web.Lists.GetByTitle("Discussion List");
           ctx.Load(list);
           ctx.ExecuteQuery();

           ListItem topic = null;

           ListItem discussionReply = null;
           CamlQuery query = CamlQuery.CreateAllFoldersQuery();
           ListItemCollection topics = list.GetItems(query);
           ctx.Load(topics);
           ctx.ExecuteQuery();

        if (topics != null && topics.Count > 0)
        {
            for (int iItemCount = 0; iItemCount < topics.Count; iItemCount++)
            {
                topic = topics[iItemCount];
                query = CamlQuery.CreateAllItemsQuery(100, "Title", "ID", "Body", "Author", "MyAuthor", "MyEditor", "Modified", "Created");  // add the properties that you want to retrieve

                query.FolderServerRelativeUrl = topic["FileRef"].ToString();

                ListItemCollection repliesColl = list.GetItems(query);
                ctx.Load(repliesColl);
                ctx.ExecuteQuery();

                if (repliesColl == null)
                    repliesColl = GetAllRepliesFromSpecificTopic(topic.Id.ToString(), list, ctx);

                Console.WriteLine("Topic Content : " + topic["Body"] + "\n");


                if (repliesColl != null && repliesColl.Count > 0)
                {
                    for (int iReplyCount = 0; iReplyCount < repliesColl.Count; iReplyCount++)
                    {
                        discussionReply = repliesColl[iReplyCount];

                        Console.WriteLine("Reply Content : " + discussionReply["Body"] +"\n");

                        FieldUserValue objUser = null;
                        objUser = (FieldUserValue)discussionReply["Author"];

                        Console.WriteLine("Reply Author : " + objUser.LookupValue+ "\n");
                    }
                }
            }
        }

        Console.ReadKey();
    }

    private static ListItemCollection GetAllRepliesFromSpecificTopic(string id, List lst, ClientContext ctx)
    {
        CamlQuery camlQuery = null;
        ListItemCollection replies = null;

        try
        {
            camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View Scope=\"Recursive\"> " +
                                               "<Query>" +
                                               "<Where>" +
                                                           "<Eq>" +
                                                               "<FieldRef Name=\"ParentFolderId\" />" +
                                                               "<Value Type=\"Integer\">" + id + "</Value>" +
                                                            "<Eq>" +
                                               "</Where>" +
                                               "</Query>" +
                                        "</View>";

            replies = lst.GetItems(camlQuery);
            ctx.Load(replies);
            ctx.ExecuteQuery();
        }
            catch (Exception ex) { }

            return replies;
        }
    }
}

Result- Displays “topic” and the content from replies and names of its authors in console window.

Keywords:

How to retrieve SharePoint Discussion Board replies and other information programmatically in CSOM
Retrieve topic and Replies of Discussion board using CSOM
Read Discussion Board & Replies using CSOM
Retrieve Discussion Lists Topics and Its Replies with Its Author value using CSOM
How to access Discussion Lists Topics and Its Replies with Its Author value using CSOM

Leave a Reply

Your email address will not be published. Required fields are marked *