使用客户端对象模型回写SharePoint列表

使用客户端对象模型回写SharePoint列表

客户端对象模型是一个有效的方式回写SharePoint列表。
1. 管理员身份打开VS,新建WPF应用程序SPWriteListApp,确保选择.NET Framework 3.5。
2. 添加以下类型控件。

界面如下:

对应代码:

    
    
    
     
     
        
      
      
        
      
      
        
      
      
        
      
      
        
      
      
        
      
      
        
      
      
        
      
      
        
      
      
        
      
      
        
      
      
    
     
     

    
    
3. 开始编程序之前,必须添加引用 Microsoft.SharePoint.Client.dll 和the Microsoft.SharePoint.Client.Runtime.dll。然后添加
using ClientOM = Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client;
4. 完整代码
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.Client;
using ClientOM = Microsoft.SharePoint.Client;
namespace SPWriteListApp
{
    public partial class MainWindow : Window
    {
        string strSPURL = "";
        string strProdName = "";
        string strProdSKU = "";
        string strProdPrice = "";
        public MainWindow()
        {
            InitializeComponent();
        }
        private void btnExit_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            txtbxURL.Text = "";
            txtbxProdName.Text = "";
            txtbxProdSKU.Text = "";
            txtbxProdPrice.Text = "";
        }
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            strSPURL = txtbxURL.Text;
            strProdName = txtbxProdName.Text;
            strProdSKU = txtbxProdSKU.Text;
            strProdPrice = txtbxProdPrice.Text;
            ClientOM.ClientContext mySPContext = new ClientContext(strSPURL);
            ClientOM.List productsList = mySPContext.Web.Lists.GetByTitle
            ("Products");
            mySPContext.Load(mySPContext.Web);
            mySPContext.Load(productsList);
            mySPContext.ExecuteQuery();
            ListItemCreationInformation newProdRecord =
            new ListItemCreationInformation();
            ClientOM.ListItem newProdItem = productsList.AddItem(newProdRecord);
            newProdItem["Title"] = strProdName;
            newProdItem["Product_SKU"] = strProdSKU;
            newProdItem["Price"] = strProdPrice;
            newProdItem.Update();
            mySPContext.ExecuteQuery();
            MessageBox.Show("Done");
        }
    }
}
5. 运行。

查看列表

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值