using System;
usingSystem.Collections.ObjectModel;
usingSystem.ComponentModel;
usingSystem.Windows;
usingSystem.Windows.Controls;
usingMicrosoft.SharePoint.Client;
namespaceSilverlightDataEntryApplication
{
publicpartialclassMainPage :
UserControl
{
#region
Global Variables
BackgroundWorkerbwLoadContacts =
newBackgroundWorker();
ObservableCollection<ContactInfo>gContactCollection
= null;
intgSelectedID = 0;
stringgSelectedName
= string.Empty;
stringgSelectedAddress
= string.Empty;
ListItemgListItem
= null;
#endregion
publicMainPage()
{
InitializeComponent();
bwLoadContacts.DoWork += newDoWorkEventHandler(bwLoadContacts_DoWork);
bwLoadContacts.RunWorkerCompleted += newRunWorkerCompletedEventHandler(bwLoadContacts_RunWorkerCompleted);
LoadContacts();
}
#region
Load Contacts from SharePoint List and Bind to DataGrid
privatevoidLoadContacts()
{
//Run BackgroundWorker Thread Here!
if
(!bwLoadContacts.IsBusy)
{
bwLoadContacts.RunWorkerAsync();
}
}
voidbwLoadContacts_DoWork(object sender,
DoWorkEventArgs e)
{
//Write Data Retrival Code Here!
ClientContextoClientContext =
newClientContext(ApplicationContext.Current.Url);
ListoList
= oClientContext.Web.Lists.GetByTitle("Contacts");
oClientContext.Load(oList);
CamlQueryoCamlQuery
= newMicrosoft.SharePoint.Client.CamlQuery();
#region CAML
stringstrCAMLQueryXML
= string.Format(@"<View>
<Query>
<OrderBy>
<FieldRef Name='Name' />
</OrderBy>
</Query>
</View>");
#endregion
oCamlQuery.ViewXml = strCAMLQueryXML;
ListItemCollectionoListItemCollection =
oList.GetItems(oCamlQuery);oClientContext.Load(oListItemCollection);
oClientContext.ExecuteQuery();//Execute Query
Synchronously
gContactCollection = newObservableCollection<ContactInfo>();
foreach (ListItemoIteminoListItemCollection)
{
ContactInfo contact =
newContactInfo();
contact.ID = oItem.Id;
contact.Name =
Convert.ToString(oItem["Name"]);
contact.Address
= Convert.ToString(oItem["Address"]);
gContactCollection.Add(contact);//Add to global
Variable
}
}
voidbwLoadContacts_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
//Write Data Binding Code Here!
DataGridContacts.ItemsSource = gContactCollection;
TxtName.Text =
"";
TxtAddress.Text
= "";
}
#endregion
privatevoidBtnAdd_Click(object sender,
RoutedEventArgs e)
{
LblMessage.Content = "Adding Record...";
gSelectedName =
TxtName.Text;
gSelectedAddress
= TxtAddress.Text;
ClientContextoClientContext =
newClientContext(ApplicationContext.Current.Url);
ListoList
= oClientContext.Web.Lists.GetByTitle("Contacts");
oClientContext.Load(oList);
gListItem =
oList.AddItem(newListItemCreationInformation());
gListItem["Name"] = TxtName.Text;
gListItem["Address"] = TxtAddress.Text;
gListItem.Update();
oClientContext.Load(gListItem);
oClientContext.ExecuteQueryAsync(OnRequestSucceeded,
null);
}
privatevoidBtnUpdate_Click(object sender,
RoutedEventArgs e)
{
LblMessage.Content = "Updating Record...";
ClientContextoClientContext =
newClientContext(ApplicationContext.Current.Url);
ListoList
= oClientContext.Web.Lists.GetByTitle("Contacts");
oClientContext.Load(oList);
gListItem =
oList.GetItemById(gSelectedID);
gListItem["Name"] = TxtName.Text;
gListItem["Address"] = TxtAddress.Text;
gListItem.Update();
oClientContext.Load(gListItem);
oClientContext.ExecuteQueryAsync(OnRequestSucceeded,
null);
}
privatevoidbtnDelete_Click(object sender,
RoutedEventArgs e)
{
if (MessageBox.Show("Are
you sure you want to remove this contact?","Delete
Contact",MessageBoxButton.OKCancel) ==
MessageBoxResult.OK)
{
//Delete Record
LblMessage.Content = "Deleting Record...";
Button
_btn = (Button)sender;
int ID =
Convert.ToInt32(_btn.CommandParameter.ToString());
ClientContextoClientContext =
newClientContext(ApplicationContext.Current.Url);
ListoList
= oClientContext.Web.Lists.GetByTitle("Contacts");
oClientContext.Load(oList);
gListItem =
oList.GetItemById(ID);
gListItem.DeleteObject();
oClientContext.Load(oList);
oClientContext.ExecuteQueryAsync(OnRequestSucceeded,
null);
}
}
privatevoidOnRequestSucceeded(Object sender,
ClientRequestSucceededEventArgsargs)
{
//
this is not called on the UI thread
Dispatcher.BeginInvoke(ShowMessage);
}
privatevoidShowMessage()
{
LblMessage.Content = "Operation Completed
Successfully!";
LoadContacts();
}
privatevoidbtnEdit_Click(object sender,
RoutedEventArgs e)
{
Button
_btnEdit = (Button)sender;
gSelectedID =
Convert.ToInt32(_btnEdit.CommandParameter.ToString());
GetContactByID(gSelectedID);
}
privatevoidGetContactByID(int ID)
{
ClientContextoClientContext =
newClientContext(ApplicationContext.Current.Url);
ListoList
= oClientContext.Web.Lists.GetByTitle("Contacts");
oClientContext.Load(oList);
gListItem =
oList.GetItemById(ID);
oClientContext.Load(gListItem);
//gContentsCollection.RetrieveItems().Retrieve();
oClientContext.ExecuteQueryAsync(OnRequestSucceeded_Load,
null);
}
privatevoidOnRequestSucceeded_Load(Object sender,
ClientRequestSucceededEventArgsargs)
{
//
this is not called on the UI thread
Dispatcher.BeginInvoke(BindData);
}
privatevoidBindData()
{
gSelectedID =
gListItem.Id;
TxtName.Text =
Convert.ToString(gListItem["Name"]);
TxtAddress.Text
= Convert.ToString(gListItem["Address"]);
}
}
publicclassContactInfo
{
publicint
ID { get; set; }
publicstring Name {
get; set; }
publicstring Address {
get; set; }
}
}
|