programming4us
programming4us
WEBSITE

Sharepoint 2010 : Client Object Model - Adding Data, Updating Data

9/29/2013 9:36:33 PM

Adding Data

Adding data using the Client Object Model is a relatively straightforward process, as these snippets show. However, the ListItemCreationInformation object can also be used to set a few system properties when creating a new list item. For example, when you’re creating a list item within a folder, you can use the FolderUrl property to specify the location of the folder.

The following code sample shows how a new list item can be added using the Silverlight Client Object Model:

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;
});
});
}



When using the JavaScript object model, the code is similar:

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.

Other  
  •  Sharepoint 2010 : Client Object Model - Retrieving Data
  •  Sharepoint 2010 : Client Object Model - Available Client-side Objects
  •  Sharepoint 2010 : Client Object Model - Architecture , Demonstration Environment Setup
  •  Developer Tools for SharePoint 2013 : Troubleshooting with Debugging
  •  Developer Tools for SharePoint 2013 : Setting Up Your Development Environment
  •  Developer Tools for SharePoint 2013 : Understanding Visual Studio 2012 Tools (part 3) - SharePoint Solutions
  •  Developer Tools for SharePoint 2013 : Understanding Visual Studio 2012 Tools (part 2) - Applications for SharePoint
  •  Developer Tools for SharePoint 2013 : Understanding Visual Studio 2012 Tools (part 1) - SharePoint Connections in Server Explorer
  •  Developer Tools for SharePoint 2013 : Understanding SharePoint Designer 2013
  •  Developer Tools for SharePoint 2013 : Customization Options with SharePoint , OOB Developer Experience
  •  
    PS4 game trailer XBox One game trailer
    WiiU game trailer 3ds game trailer
    Top 10 Video Game
    -   Minecraft Mods - MAD PACK #10 'NETHER DOOM!' with Vikkstar & Pete (Minecraft Mod - Mad Pack 2)
    -   Minecraft Mods - MAD PACK #9 'KING SLIME!' with Vikkstar & Pete (Minecraft Mod - Mad Pack 2)
    -   Minecraft Mods - MAD PACK #2 'LAVA LOBBERS!' with Vikkstar & Pete (Minecraft Mod - Mad Pack 2)
    -   Minecraft Mods - MAD PACK #3 'OBSIDIAN LONGSWORD!' with Vikkstar & Pete (Minecraft Mod - Mad Pack 2)
    -   Total War: Warhammer [PC] Demigryph Trailer
    -   Minecraft | MINIONS MOVIE MOD! (Despicable Me, Minions Movie)
    -   Minecraft | Crazy Craft 3.0 - Ep 3! "TITANS ATTACK"
    -   Minecraft | Crazy Craft 3.0 - Ep 2! "THIEVING FROM THE CRAZIES"
    -   Minecraft | MORPH HIDE AND SEEK - Minions Despicable Me Mod
    -   Minecraft | Dream Craft - Star Wars Modded Survival Ep 92 "IS JOE DEAD?!"
    -   Minecraft | Dream Craft - Star Wars Modded Survival Ep 93 "JEDI STRIKE BACK"
    -   Minecraft | Dream Craft - Star Wars Modded Survival Ep 94 "TATOOINE PLANET DESTRUCTION"
    -   Minecraft | Dream Craft - Star Wars Modded Survival Ep 95 "TATOOINE CAPTIVES"
    -   Hitman [PS4/XOne/PC] Alpha Gameplay Trailer
    -   Satellite Reign [PC] Release Date Trailer
    Video
    programming4us
     
     
    programming4us