3

I understand that you can access comments in an append-only field like:

    public string retrieveComments(SPListItem item)
    {
        SPListItemVersionCollection versions = item.Versions;
        string latestComment = "";

        try
        {
            foreach (SPListItemVersion itemVersion in versions)
            {
                if (itemVersion.IsCurrentVersion)
                {
                    latestComment = itemVersion["Comments"].ToString().Trim();

                }
            }
        }
        catch (Exception e3)
        {
            //handles exception
        }

        return latestComment;
    }

Is there any way to create a new version of the list item and append a comment as the latest comment through C#?

My goal is for someone to email the list and take the body of their email and append it as a comment.

0
8

You don't need to go through the Versions to get the latest value. That's the current value:

item["Comments"].ToString().Trim();

To add a new comment just update the item:

item["Comments"] = "This is now the latest comment";
item.Update();
6
  • Wow. I honestly thought I tried this already. Thanks so much. Would up-vote but I don't have enough rep! – cbrad Jul 30 '12 at 18:19
  • Is there a way to add an image attachment as a comment if the user say, sends a screenshot? – cbrad Jul 30 '12 at 18:20
  • Handled it for you, cbrad. – RJ Cuthbertson Jul 30 '12 at 18:20
  • Thanks rjcup3, easy answer apparently, but well deserved. Been stuck on this for awhile. – cbrad Jul 30 '12 at 18:23
  • 1
    You can add the image as a standard attachment and then in the comments field have an <img> element pointing to it like: item["Comments"]="Comment with image: <img src='/Lists/TestList/Attachments/2/Test.png' />. Nice";. In this case it's item with ID and the Attachment is named "Test.png" – Per Jakobsen Jul 30 '12 at 18:29
0

Here is an example:

 List<Message> msgs = new List<Message>
            {
                new Message{Comments=new List<string>{"AS"},IsCurrent=false},
                new Message{Comments=new List<string>{"ASfff"},IsCurrent=true},
            };

            var ress = msgs.Where(ms => ms.IsCurrent); 


            foreach (var ms in ress)
            {
                ms.Comments.Add("Hello");
            }
5
  • I could be wrong but doesn't that just get the latest comment? I want to be able to append a comment as the latest comment. – cbrad Jul 27 '12 at 22:42
  • Means if the comment is IsCurrentVersion then what you wana do ? – Praveen Jul 27 '12 at 22:43
  • I want to add a comment, not retrieve them. – cbrad Jul 27 '12 at 22:45
  • Sorry for late reply, I will give this a try today, thanks. – cbrad Jul 30 '12 at 15:12
  • How would I go about creating the Message class? – cbrad Jul 30 '12 at 16:09
0

Posted this on sharepoint.stackexchange.com

The answer is simple (which makes me look stupid), and I'm pretty sure I tried it before with no avail, but here it is:

Can you add comments to an append-only field in a list in Sharepoint 2010 using C#?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.