SharePoint 创建列表并使用Windows Presentation Foundation应用程序管理列表

时间:2023-03-08 21:26:03
SharePoint 创建列表并使用Windows Presentation Foundation应用程序管理列表

SharePoint创建列表并使用程序管理列表

        列表是SharePoint开发者输入数据的方式之中的一个。使用Web界面创建一个列表并加入一些数据。过程例如以下:
1. 打开站点。
2. 点击全部站点内容。
3. 点击创建。
4. 选择自己定义列表,命名Customers,并输入描写叙述。

选中在高速启动导航显示,点击创建。

        这将创建一个自己定义列表。接下来我们加入三个栏:Region/Size/Sales。
1. 点击功能区创建栏button,命名Region,选择“选项”类型。

添加四个选项:East/West/North/South。其余默认。点击确定。

2. 添加Size栏,相同“选项”类型:Small/Medium/Enterprise,点击确定。
3. 添加Sales栏。选择单行文本,点击确定。
4. 最后,点击功能区列表设置,点击Title链接,将默认的Title改为Name,点击确定。
5. 虽然你更改了Title为Name,在编程时。你仍须要使用Title。将鼠标放在Name字段上,看一下URL的Field=Title。
SharePoint 创建列表并使用Windows Presentation Foundation应用程序管理列表
6. 为了加入一些项目到列表。点击Customers列表,点击加入新项目。完毕例如以下结果。
SharePoint 创建列表并使用Windows Presentation Foundation应用程序管理列表
        你能够对SharePoint 2010 中的列表编敲代码,使你能够管理/创建/读取/更新/删除(CRUD)操作。
1. 管理员身份打开VS 2010.新建项目WPF应用程序。
2. 命名WPFSPListAPP。点击确定。右击MainWindow.xaml文件,选择视图设计器。
3. 加入5个标签/4个文本框/3个button。
SharePoint 创建列表并使用Windows Presentation Foundation应用程序管理列表
        以下是一些Name属性
SharePoint 创建列表并使用Windows Presentation Foundation应用程序管理列表
5. 右击MainWindwow.xaml,查看代码。
6. 右击引用,点击加入服务引用。点击高级。点击加入Web引用
7. 在加入Web引用对话框中,点击本地计算机的Web服务链接。
SharePoint 创建列表并使用Windows Presentation Foundation应用程序管理列表
8. 选择Lists服务形如http://<server name>/_vti_bin/Lists.asmx的Web服务。

你须要更改为自己的服务器名。

提供一个名称。点击加入引用。

SharePoint 创建列表并使用Windows Presentation Foundation应用程序管理列表
9. 这时你能够为每一个button加入事件处理器。Updatebutton负责Web服务连接到SharePoint。你也须要一系列类层次的变量获得用户输入并传递给Lists Web service。当你调用 Lists Web service时,你也须要创建XML将数据从WPF传递到SharePoint列表。XML被称为Collaborative Application Markup Language(CAML)。
        你须要更新MainWindow.xaml的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.Linq; namespace WPFSPListAPP
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
string strCompanyName = "";
string strRegion = "";
string strSize = "";
string strSales = "";
string strListID = "";
string strViewID = ""; public MainWindow()
{
InitializeComponent();
} private void btnUpdate_Click(object sender, RoutedEventArgs e)
{
strCompanyName = txtbxCompanyName.Text;
strRegion = txtbxRegion.Text;
strSize = txtbxSize.Text;
strSales = "$" + txtbxSales.Text; WPFSPListAPP.MySPWebService.Lists myListService =
new MySPWebService.Lists();
myListService.Credentials = System.Net.CredentialCache.DefaultCredentials;
myListService.Url = "http://smallville-pc/_vti_bin/Lists.asmx"; XmlNode myListView = myListService.GetListAndView("Customers", ""); strListID = myListView.ChildNodes[0].Attributes["Name"].Value;
strViewID = myListView.ChildNodes[1].Attributes["Name"].Value; XmlDocument myListDoc = new XmlDocument();
XmlElement batchXML = myListDoc.CreateElement("Batch"); batchXML.InnerXml="" +
strCompanyName + "" + strRegion +
"" + strSize +
"" + strSales +
"" + ""; XmlNode myListReturn = myListService.UpdateListItems(strListID, batchXML);
MessageBox.Show("SharePoint List was updated!");
} private void btnClear_Click(object sender, RoutedEventArgs e)
{
txtbxCompanyName.Text = "";
txtbxRegion.Text = "";
txtbxSales.Text = "";
txtbxSize.Text = "";
} private void btnExit_Click(object sender, RoutedEventArgs e)
{
Application.Current.Shutdown();
}
}
}

10. F5调试。

点击更新。

SharePoint 创建列表并使用Windows Presentation Foundation应用程序管理列表
11. 查看列表。
SharePoint 创建列表并使用Windows Presentation Foundation应用程序管理列表

原理

        本例中你使用了Lists Web service,它提供了很多方式与列表交流----比如加入/删除列表/加入附件/获取列表等。本例中你使用了GetListAndView方法。这次调用。你传递列表名Customers,将返回值映射为XMLNode对象。
XmlNode myListView = myListService.GetListAndView(“Customers”, ““);
        本例也使用了CAML插入数据到SharePoint列表。不得不说CAML冗长而啰嗦。
SharePoint 创建列表并使用Windows Presentation Foundation应用程序管理列表
        最后关键的UpdateListItems方法。传入列表ID和列表架构。
XmlNode myListReturn = myListService.UpdateListItems(strListID, batchXML);
        这种方法使用了native Web services。有优缺点。长处包含易于使用且研究了现有的service,否则你须要自己创建;缺点包含潜在的服务集成性能和CAML语法的冗长复杂性。