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.
Leave a comment