使用服务器端对象模型,编写程序管理SharePoint列表
2014-06-16 17:07
489 查看
使用服务器端对象模型,编写程序管理SharePoint列表
列表是SharePoint核心的工艺品。服务器端对象模型是与列表交互的方法之一。你可以在服务器上创建不同类型的应用程序,与服务器对象模型交互。比如WinForm或WPF/Web parts,甚至是Event Receiver。1. 管理员身份打开VS,新建项目WPF应用程序,确保选择.NET Framework 3.5,命名WPFSPTestApp,点击确定。
2. 添加引用Microsoft.SharePoint.dll,点击确定。
3. 添加控件。对应MainWindow.xaml代码为:4. 双击Exit按钮,添加代码:
private void btnExit_Click(object sender, RoutedEventArgs e) { Application.Current.Shutdown(); }
5. 双击Clear按钮,添加代码:
private void btnClear_Click(object sender, RoutedEventArgs e) { txtbxListName.Text = ""; txtbxSPURL.Text = ""; txtbxProdName.Text = ""; txtbxProductSku.Text = ""; txtbxProductPrice.Text = ""; }
6. 在MainWindow.xaml.cs文件中添加引用
using Microsoft.SharePoint;
7. 添加5个类级别的变量。
public partial class MainWindow : Window { string strSPSiteURL = ""; string strSPListName = ""; string strProductName = ""; string strProductSKU = ""; string strProductPrice = ""; }
8. 双击Load按钮,添加代码:
private void btnLoad_Click(object sender, RoutedEventArgs e) { strSPSiteURL = txtbxSPURL.Text; strSPListName = txtbxListName.Text; strProductName = txtbxProdName.Text; strProductSKU = txtbxProductSku.Text; strProductPrice = txtbxProductPrice.Text; using (SPSite site = new SPSite(strSPSiteURL)) { using (SPWeb web = site.OpenWeb()) { web.AllowUnsafeUpdates = true; SPList list = web.Lists[strSPListName]; SPListItem Item = list.Items.Add(); Item["Title"] = strProductName; Item["Product_SKU"] = strProductSKU; Item["Price"] = strProductPrice; Item.Update(); web.AllowUnsafeUpdates = false; } } MessageBox.Show("Load Successfully"); }
全部代码是这样的:
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 Microsoft.SharePoint;namespace WPFSPTestApp
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
string strSPSiteURL = "";
string strSPListName = "";
string strProductName = "";
string strProductSKU = "";
string strProductPrice = "";public MainWindow()
{
InitializeComponent();
}private void btnExit_Click(object sender, RoutedEventArgs e) { Application.Current.Shutdown(); }private void btnClear_Click(object sender, RoutedEventArgs e) { txtbxListName.Text = ""; txtbxSPURL.Text = ""; txtbxProdName.Text = ""; txtbxProductSku.Text = ""; txtbxProductPrice.Text = ""; }private void btnLoad_Click(object sender, RoutedEventArgs e) { strSPSiteURL = txtbxSPURL.Text; strSPListName = txtbxListName.Text; strProductName = txtbxProdName.Text; strProductSKU = txtbxProductSku.Text; strProductPrice = txtbxProductPrice.Text; using (SPSite site = new SPSite(strSPSiteURL)) { using (SPWeb web = site.OpenWeb()) { web.AllowUnsafeUpdates = true; SPList list = web.Lists[strSPListName]; SPListItem Item = list.Items.Add(); Item["Title"] = strProductName; Item["Product_SKU"] = strProductSKU; Item["Price"] = strProductPrice; Item.Update(); web.AllowUnsafeUpdates = false; } } MessageBox.Show("Load Successfully"); }
}
}
9. F5调试。输入类似产品信息。10. 打开站点导航到Products列表,可以看到更新了。
故障分析:
如果遇到下列问题请修改项目属性相关文章推荐
- 使用服务器端对象模型,编写程序管理SharePoint列表
- 使用soaoffice中间件+asp实例编写小型的企业管理程序(二)
- 使用mrjob简单的编写mapreduce模型的程序
- SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 3)
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- 使用Python3 编写简单信用卡管理程序
- 使用Qt编写服务器端程序(包括Http传输服务器端)的方法
- SharePoint服务器端对象模型 之 使用CAML进行数据查询(Part 4)
- SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 4)
- 编写高质量代码改善C#程序的157个建议——建议19:使用更有效的对象和集合初始化
- 编写高质量代码改善C#程序的157个建议——建议53:必要时应将不再使用的对象引用赋值为null
- 完成一个学生管理程序,使用学号作为键添加5个学生对象,并可以将全部信息保存在文件中,可以实现对学生信息的学号查找,输出全部学生信息的功能。
- SharePoint服务器端对象模型 之 使用CAML进行数据查询
- [编写高质量代码:改善java程序的151个建议]建议43 避免对象浅拷贝; 建议44:推荐使用序列化实现对象的深拷贝
- Web程序,使用ReportSerivce报表对象模型编程发布报表
- 使用C++与SFML编写一个简单的撞球游戏Part5——对象管理
- [编写高质量代码:改善java程序的151个建议]建议43 避免对象浅拷贝; 建议44:推荐使用序列化实现对象的深拷贝
- 使用开源工具SVNKit编写程序管理Subversion
- SharePoint服务器端对象模型 之 使用CAML进行数据查询(Part 2)
- 使用Python3 编写简单信用卡管理程序