I am trying to programatically insert a discussion post into a Sharepoint discussion board list using the client object model in C#. I am using the following code:
var discussionList = sharepointContext.Web.Lists.GetByTitle("Discussion"); var discussionItem = Utility.CreateNewDiscussion(sharepointContext, discussionList, "Test"); discussionItem["Body" ] = "Hello world!" discussionItem["Author" ] = 22; discussionItem["Editor" ] = 22; sharepointContext.Load(discussionItem); discussionItem.Update(); sharepointContext.ExecuteQuery();
However, whenever I run it, I get this exception
Microsoft.SharePoint.Client.ServerException was unhandled Message=Field or property "Body" does not exist. Source=Microsoft.SharePoint.Client.Runtime ServerErrorCode=-1"
Does anyone know what I'm doing wrong?
The reason the code in the question didn't work is because you called Update()
after calling SPContext.Load()
. If you call Update()
first, you'll be fine.