Monday, 30 June 2014

Add New Item to List in Sharepoint using ClientContext

List list;
ListItem item;
string siteUrl = "http://sp2010";
private void ButtonCreate_Click(object sender, RoutedEventArgs e)
{
ClientContext spContext = new ClientContext(siteUrl);
Web oWebsite = spContext.Web;
ListCollection collList = oWebsite.Lists;
list = spContext.Web.Lists.GetByTitle("Announcements");
item = list.AddItem(new ListItemCreationInformation());
item["Title"] = "My new item";
item["Body"] = "My new Silverlight item.";
item.Update();
spContext.ExecuteQueryAsync(OnCreateSuccess, OnFailure);
}
private void OnCreateSuccess(object sender, ClientRequestSucceededEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("New Item Title: " + item["Title"]);
});
}
private void OnFailure(object sender, ClientRequestFailedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Request failed. " + args.Message + "\n" + args.StackTrace);
});
}