Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

migrated from stackoverflow.com Aug 2 '12 at 12:15

This question came from our site for professional and enthusiast programmers.

add comment

3 Answers 3

up vote 6 down vote accepted

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();
share|improve this answer
    
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
show 2 more comments

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");
            }
share|improve this answer
    
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
add comment

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:

http://sharepoint.stackexchange.com/questions/42164/can-you-add-comments-to-an-append-only-field-in-a-list-in-sharepoint-2010-using

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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