Search This Blog

Thursday, March 19, 2015

SharePoint - Programmatically creating comments of a blog post in blog site


If you have a problem when he was trying to create "Blog Post Comments" programmatically. He was not able to increase the Comments counts (below blog post).

I have done some R&D on that and found that any blog site in SharePoint there are two main lists: 
1. Posts and 2. Comments 

We need to update the Comments list's "Post Title" column with the Posts list's "Title" column lookupThis will automatically update the comments count.

Note: Comments lists's "Post Title" field is hidden field.

I am providing the sample code for the same:

//Update Blog Post Comments Programatically
using (SPSite site = new SPSite("http://br-pc-275:3670/blog/"))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList objPostList = web.Lists["Posts"];
        SPListItem objItem = objPostList.GetItemById(1);
        SPList objCommentsList = web.Lists["Comments"];
        SPListItem objNewComment = objCommentsList.Items.Add();
        objNewComment["Title"] = "New Comment 1";
        //Id of blogPost + ";#" + Title of blogPost
        objNewComment["Post Title"] = "1" + ";#" +Convert.ToString(objItem["Title"]);
        objNewComment.Update();
    }
}


No comments:

Post a Comment