使用Server-Side Object Model修改列表
新建一个列表:Products
新建一个wpf项目,选择framework3.5,4.0的话编译会报错。
添加 sharepoint的引用
代码如下:
<Grid>
<Label Content="站点" Height="28" HorizontalAlignment="Left" Margin="59,17,0,0" Name="lblSPUrl" VerticalAlignment="Top" Width="100" />
<Label Content="列表名称" Height="28" HorizontalAlignment="Left" Margin="57,52,0,0" Name="lblListName" VerticalAlignment="Top" Width="100" />
<Label Content="库存号" Height="28" HorizontalAlignment="Left" Margin="59,124,0,0" Name="lblProductSKU" VerticalAlignment="Top" Width="100" />
<Label Content="产品名称" Height="28" HorizontalAlignment="Left" Margin="59,88,0,0" Name="lblProdName" VerticalAlignment="Top" Width="100" />
<Label Content="产品价格" Height="28" HorizontalAlignment="Left" Margin="59,167,0,0" Name="lblProductPrice" VerticalAlignment="Top" Width="100" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="171,0,0,217" Name="txtbxProductSku" VerticalAlignment="Bottom" Width="169" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="171,172,0,0" Name="txtbxProductPrice" VerticalAlignment="Top" Width="169" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="171,52,0,0" Name="txtbxListName" VerticalAlignment="Top" Width="169" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="171,19,0,0" Name="txtbxSPURL" VerticalAlignment="Top" Width="169" />
<Button Content="更新" Height="23" HorizontalAlignment="Right" Margin="0,212,346,0" Name="btnLoad" VerticalAlignment="Top" Width="75" Click="btnLoad_Click" />
<Button Content="清空" Height="23" HorizontalAlignment="Left" Margin="246,212,0,0" Name="btnClear" VerticalAlignment="Top" Width="75" Click="btnClear_Click" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="171,88,0,0" Name="txtbxProdName" VerticalAlignment="Top" Width="169" />
</Grid>
<Label Content="列表名称" Height="28" HorizontalAlignment="Left" Margin="57,52,0,0" Name="lblListName" VerticalAlignment="Top" Width="100" />
<Label Content="库存号" Height="28" HorizontalAlignment="Left" Margin="59,124,0,0" Name="lblProductSKU" VerticalAlignment="Top" Width="100" />
<Label Content="产品名称" Height="28" HorizontalAlignment="Left" Margin="59,88,0,0" Name="lblProdName" VerticalAlignment="Top" Width="100" />
<Label Content="产品价格" Height="28" HorizontalAlignment="Left" Margin="59,167,0,0" Name="lblProductPrice" VerticalAlignment="Top" Width="100" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="171,0,0,217" Name="txtbxProductSku" VerticalAlignment="Bottom" Width="169" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="171,172,0,0" Name="txtbxProductPrice" VerticalAlignment="Top" Width="169" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="171,52,0,0" Name="txtbxListName" VerticalAlignment="Top" Width="169" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="171,19,0,0" Name="txtbxSPURL" VerticalAlignment="Top" Width="169" />
<Button Content="更新" Height="23" HorizontalAlignment="Right" Margin="0,212,346,0" Name="btnLoad" VerticalAlignment="Top" Width="75" Click="btnLoad_Click" />
<Button Content="清空" Height="23" HorizontalAlignment="Left" Margin="246,212,0,0" Name="btnClear" VerticalAlignment="Top" Width="75" Click="btnClear_Click" />
<TextBox Height="23" HorizontalAlignment="Left" Margin="171,88,0,0" Name="txtbxProdName" VerticalAlignment="Top" Width="169" />
</Grid>
cs代码:
1 public partial class MainWindow : Window
2 {
3 string strSPSiteURL = "";
4 string strSPListName = "";
5 string strProductName = "";
6 string strProductSKU = "";
7 string strProductPrice = "";
8
9 public MainWindow()
10 {
11 InitializeComponent();
12 }
13
14 private void btnLoad_Click(object sender, RoutedEventArgs e)
15 {
16 strSPSiteURL = txtbxSPURL.Text;
17 strSPListName = txtbxListName.Text;
18 strProductName = txtbxProdName.Text;
19 strProductSKU = txtbxProductSku.Text;
20 strProductPrice = txtbxProductPrice.Text;
21 using (SPSite site = new SPSite(strSPSiteURL))
22 {
23 using (SPWeb web = site.OpenWeb())
24 {
25 web.AllowUnsafeUpdates = true;
26 SPList list = web.Lists[strSPListName];
27 SPListItem Item = list.Items.Add();
28 Item["Title"] = strProductName;
29 Item["Product_SKU"] = strProductSKU;
30 Item["Price"] = strProductPrice;
31 Item.Update();
32 web.AllowUnsafeUpdates = false;
33 }
34 }
35 }
36
37 private void btnClear_Click(object sender, RoutedEventArgs e)
38 {
39 txtbxListName.Text = "";
40 txtbxSPURL.Text = "";
41 txtbxProdName.Text = "";
42 txtbxProductSku.Text = "";
43 txtbxProductPrice.Text = "";
44 }
45 }
3 string strSPSiteURL = "";
4 string strSPListName = "";
5 string strProductName = "";
6 string strProductSKU = "";
7 string strProductPrice = "";
8
9 public MainWindow()
10 {
11 InitializeComponent();
12 }
13
14 private void btnLoad_Click(object sender, RoutedEventArgs e)
15 {
16 strSPSiteURL = txtbxSPURL.Text;
17 strSPListName = txtbxListName.Text;
18 strProductName = txtbxProdName.Text;
19 strProductSKU = txtbxProductSku.Text;
20 strProductPrice = txtbxProductPrice.Text;
21 using (SPSite site = new SPSite(strSPSiteURL))
22 {
23 using (SPWeb web = site.OpenWeb())
24 {
25 web.AllowUnsafeUpdates = true;
26 SPList list = web.Lists[strSPListName];
27 SPListItem Item = list.Items.Add();
28 Item["Title"] = strProductName;
29 Item["Product_SKU"] = strProductSKU;
30 Item["Price"] = strProductPrice;
31 Item.Update();
32 web.AllowUnsafeUpdates = false;
33 }
34 }
35 }
36
37 private void btnClear_Click(object sender, RoutedEventArgs e)
38 {
39 txtbxListName.Text = "";
40 txtbxSPURL.Text = "";
41 txtbxProdName.Text = "";
42 txtbxProductSku.Text = "";
43 txtbxProductPrice.Text = "";
44 }
45 }
如果站点url总是报错的话有可能是因为目标平台默认为X86,在项目属性中把它设置为X64或者anycpu即可。