How to Create Discussions List Reply to a topic and Reply to a Reply Programmatically using C# Patterns and Practice (PNP)


Sathish Nadarajan
SharePoint MVP
Published On :   13 Nov 2018
Visit Count
Today :  2    Total :   314



Usually in the discussion list, we will start a topic and a lot of replies will be given by the users and sometimes, there could be a chance that a reply to reply also. In this article, let us see how to create

1. Reply to a Topic

2. Reply to a Reply.

The current scenario will be useful, while creating any sort of tools which will create a bunch of discussion items from the legacy application to the sharepoint.

Reply to a Topic

 public static void CreateReplyToATopic()
         {
             try
             {
                 OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
                 using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
                 {
                     Web web = ctx.Web;
                     ctx.Load(web);
                     ctx.Load(web.Lists);
                     ctx.ExecuteQueryRetry();
                     List list = web.Lists.GetByTitle("Discussions List");
                     ctx.Load(list);
                     ctx.ExecuteQueryRetry();
                     ListItem topicItem = list.GetItemById(1);
                     ctx.Load(topicItem);
                     ctx.ExecuteQuery();
 
                     var replyItem = Microsoft.SharePoint.Client.Utilities.Utility.CreateNewDiscussionReply(ctx, topicItem);
                     replyItem.Update();
                     replyItem["Title"] = "Test Reply";
                     replyItem["Body"] = "Test body Content";
                     replyItem.Update();
                     ctx.ExecuteQuery();
                 }
             }
             catch (Exception ex)
             {
                 System.Console.WriteLine("Exception occurred : " + ex.Message);
                 System.Console.ReadLine();
             }
         }
 


Reply to a Reply

To Create a reply to a reply, the same above code will be fine. But, the only thing is, change the topicItem to replyItem of the appropriate reply.

 var replyItem = Microsoft.SharePoint.Client.Utilities.Utility.CreateNewDiscussionReply(ctx, topicItem);

Happy Coding,

Sathish Nadarajan.

Categories