图 9 设计器中的 MainPage.xaml
在设计器中双击该按钮并使用图 10 中的代码替换类。
图 10 addNonStanPurchaseReq_Click
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.Client;
namespace NonStandBusPurchaseReqsSLOM
{
public partial class MainPage : UserControl
{
private string webUrl;
public MainPage(string url)
{
webUrl = url;
InitializeComponent();
}
private void addNonStanPurchaseReq_Click(object sender, RoutedEventArgs e)
{
ClientContext clientContext = new ClientContext(webUrl);
Web webSite = clientContext.Web;
ListCollection webLists = webSite.Lists;
List nonStandBusPurList =
clientContext.Web.Lists.GetByTitle(
"Non-Standard Business Purchase Requests");
ListItem newListItem =
nonStandBusPurList.AddItem(new ListItemCreationInformation());
newListItem["Title"] = Title.Text;
newListItem["RequestDescription"] = Description.Text;
newListItem["Price"] = Price.Text;
newListItem.Update();
clientContext.Load(nonStandBusPurList, list => list.Title);
clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
}
private void onQuerySucceeded(
object sender, ClientRequestSucceededEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("New item added.");
});
}
private void onQueryFailed(object sender,
ClientRequestFailedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Request failed. "
+ args.Message + "\n" +
args.StackTrace);
});
}
}
}
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.Client;
namespace NonStandBusPurchaseReqsSLOM
{
public partial class MainPage : UserControl
{
private string webUrl;
public MainPage(string url)
{
webUrl = url;
InitializeComponent();
}
private void addNonStanPurchaseReq_Click(object sender, RoutedEventArgs e)
{
ClientContext clientContext = new ClientContext(webUrl);
Web webSite = clientContext.Web;
ListCollection webLists = webSite.Lists;
List nonStandBusPurList =
clientContext.Web.Lists.GetByTitle(
"Non-Standard Business Purchase Requests");
ListItem newListItem =
nonStandBusPurList.AddItem(new ListItemCreationInformation());
newListItem["Title"] = Title.Text;
newListItem["RequestDescription"] = Description.Text;
newListItem["Price"] = Price.Text;
newListItem.Update();
clientContext.Load(nonStandBusPurList, list => list.Title);
clientContext.ExecuteQueryAsync(onQuerySucceeded, onQueryFailed);
}
private void onQuerySucceeded(
object sender, ClientRequestSucceededEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("New item added.");
});
}
private void onQueryFailed(object sender,
ClientRequestFailedEventArgs args)
{
Dispatcher.BeginInvoke(() =>
{
MessageBox.Show("Request failed. "
+ args.Message + "\n" +
args.StackTrace);
});
}
}
}