您的位置:首页 > 其它

使用服务器端对象模型,编写程序管理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列表,可以看到更新了。

故障分析:

如果遇到下列问题请修改项目属性
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐