private void Add_Click(object sender, RoutedEventArgs e)
{
ClientContext ctx = new ClientContext("Your Server Here");
List announcements = ctx.Web.Lists.GetByTitle("Announcements");
ListItemCreationInformation createInfo = new ListItemCreationInformation();
ListItem newItem = announcements.AddItem(createInfo);
newItem["Title"] = "A new item";
newItem.Update();
ctx.ExecuteQueryAsync((s, args) =>
{
Dispatcher.BeginInvoke(() =>
{
label1.Content = "Item Added";
});
}, (s, args) =>
{
Dispatcher.BeginInvoke(() =>
{
label1.Content = args.Message;
});
});
}
function Add_Click() {
var ctx = new SP.ClientContext.get_current();
var announcements = ctx.get_web().get_lists().
getByTitle("Announcements");
var createInfo = new SP.ListItemCreationInformation();
var newItem = announcements.addItem(createInfo);
newItem.set_item("Title", "A new javascript item");
newItem.update();
ctx.executeQueryAsync(function (s, args) {
var console = document.getElementById('DemoConsole');
console.innerHTML = "Add Completed";
}, null);
}
Updating Data
So far, you’ve seen a few ways to retrieve data
using the Client Object Model, as well as how to add new items to
SharePoint lists and libraries. Our next logical step is to look at how
we can update the data that we’ve retrieved and submit the changes back
to SharePoint.
Here’s how to do this in Silverlight:
private void Update_Click(object sender, RoutedEventArgs e)
{
ClientContext ctx = new ClientContext(Your Server Here");
CamlQuery query = new CamlQuery();
query.ViewXml = "<View><Query><OrderBy>" +
"<FieldRef Name=\"Editor\" Ascending=\"False\" />" +
"</OrderBy></Query></View>";
List announcements = ctx.Web.Lists.GetByTitle("Announcements");
ListItemCollection listItems = announcements.GetItems(query);
ctx.Load(listItems);
ctx.ExecuteQueryAsync((s, args) =>
{
foreach (var item in listItems)
{
item["Title"] = "Updated";
item.Update();
}
ctx.ExecuteQueryAsync((s1, args1) =>
{
Dispatcher.BeginInvoke(() =>
{
label1.Content = "Records Updated";
});
}, null);
}, (s, args) =>
{
Dispatcher.BeginInvoke(() =>
{
label1.Content = args.Message;
});
});
}
And here it is in JavaScript:
function Update_Click() {
var ctx = new SP.ClientContext.get_current();
var query = new SP.CamlQuery();
query.viewXml = "<View><Query><OrderBy>" +
"<FieldRef Name=\"Editor\" Ascending=\"False\" />" +
"</OrderBy></Query></View>";
var announcements = ctx.get_web().get_lists().getByTitle("Announcements");
var listItems = announcements.getItems(query);
ctx.load(listItems, "Include(Title)");
ctx.executeQueryAsync(function (s, args) {
var itemEnum = listItems.getEnumerator();
while (itemEnum.moveNext()) {
var item = itemEnum.get_current();
item.set_item("Title","JavaScript Update");
item.update();
}
ctx.executeQueryAsync(function (s, args) {
var console = document.getElementById('DemoConsole');
console.innerHTML =" JavaScript Update Completed";
}, null);
}, null);
}
As
you can see from these code samples, updating values from the Client
Object Model works in a similar way to updating objects on the server,
in that the Update method must be called to commit the changes.