Tuesday 12 June 2012

How to edit List Items in Sharepoint Silverlight Client Object Model

Here consider we have a list of Employee Details ,first we will fetch the details for a particular employee into text boxes then edit them and update them to the list
  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;


namespace EmployeeDetailsEdit
{

public partial class MainPage : UserControl
{

ClientContext cc1;

ListItem litem;

List EmployeeDetails;

ListItemCollection _icoll; 




string tmp, Title, EmployeeName, JoinDate,Address;

public MainPage() 
{


InitializeComponent();


cc1 = new ClientContext(ApplicationContext.Current.Url);
cc1.Load(cc1.Web);
EmployeeDetails = cc1.Web.Lists.GetByTitle("EmployeeDetails");
cc1.Load(EmployeeDetails );

CamlQuery qry = new Microsoft.SharePoint.Client.CamlQuery();
string q = "";
qry.ViewXml = q;

_icoll = auditp.GetItems(qry);

cc1.Load(_icoll);
cc1.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(sucess), null);

 }



private void SubmitButton_Click(object sender, RoutedEventArgs e)
{




litem = EmployeeDetails.GetItemById(ID);




litem["Title"] = textBox1.Text;
litem["Employee_x0020_Name"] = textBox2.Text;
litem["Join_x0020_Date"] = datePicker1.Text.ToString();

litem["Address"] = textBox3.Text;
litem.Update();


cc1.Load(EmployeeDetails, list => list.Title);

cc1.ExecuteQueryAsync(sucess, null);




}


private void sucess(Object sender, ClientRequestSucceededEventArgs args)

{

// This is not called on the UI thread.

Dispatcher.BeginInvoke(datafetch);




}

private void datafetch()
{


foreach (ListItem l in _icoll)
{


if (l["Employee_x0020_Name"].ToString() == "Joseph")
{
tmp = l["ID"].ToString();

Title= l["Title"].ToString();
EmployeeName= l["Employee_x0020_Name"].ToString();
JoinDate= l["Join_x0020_Date"].ToString();
Address= l["Address"].ToString();

}



}

ID = Convert.ToInt32(tmp);

textBox1.Text = Title;

textBox2.Text = EmployeeName;

datePicker1.Text = JoinDate;

textBox3.Text = Address;}




}




}

} 

No comments:

Post a Comment