tags:

views:

191

answers:

5

I have the need to create Sharepoint blog comments by code:

                       SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                    sw.AllowUnsafeUpdates = true;

                    SPList spList = SPContext.Current.Web.Lists["Kommentare"];
                    SPListItem listItem = spList.Items.Add();
                    listItem[listItem.Fields["Titel des Beitrags"].InternalName] = SPContext.Current.Item["Title"];
                    listItem[listItem.Fields["Titel"].InternalName] = titlearea.Value;
                    listItem[listItem.Fields["Textkörper"].InternalName] = CommentArea.Value;
                    listItem[SPBuiltInFieldId.Author] = curUser;
                  //  listItem[SPBuiltInFieldId.Modified] = curUser;
                    listItem.Update();
                    //spList.Update();
                    sw.AllowUnsafeUpdates = false;
                    });

but I allways get a "Invalid data has been used to update the list item. The field you are trying to update may be read only"

I tried Systemupdate and the values seem to be valid.

Has Anybody an idea?

Greetings ren

+1  A: 

I think the Author field is readonly and sharepoint automattically asign the loggined user name there.

So could you try it after commenting the line listItem[SPBuiltInFieldId.Author] = curUser;

Hojo
Thanks, but sadly makes no difference
Ren Hoek
A: 

Maybe this is unrelated to your problem but you are creating the SPWeb object (or using the context) outside the delegate, if a user without proper privileges runs the code, it will not elevate properly. Do something like this:

SPSecurity.RunWithElevatedPrivileges(delegate {
using (SPSite elevatedSite = new SPSite(SPContext.Current.Site.ID))
using (SPWeb elevatedSite = elevatedSite.RootWeb)
{

//impl

}});
F.Aquino
A: 

listItem[listItem.Fields["Titel des Beitrags"].InternalName] = SPContext.Current.Item["Title"];

Does SPContext.Current.Item point to a refence to the blog post you are adding the comment to?

By this I mean is your code running in a custom web part that replaces the standard "Add comments" web part on a blog post?

Paul Lucas
Yes it does.According to the post http://stackoverflow.com/questions/1769612/userimage-in-sharepoint-blog-comment I had to re implement the blog show Webpart, but let the original Post-Comment Part in place.Unfortunately by this the page refresh after a comment post leads back to the page before the Comment is pushed into the List.
Ren Hoek
A: 

This is a good post, I stumbled across your article while looking for song downloads. Thanks for sharing, I’ll be sure to recommend this site to others.

aion kinah
A: 

Ok, the Posttitle is a SPLookupField, and has to be filled by: "{ID}#;{Title}"

Ren Hoek