C# 如何使用 .asmx Web 服务列出将项目添加到 Sharepoint 列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11356429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to list Add items to Sharepoint list using .asmx web service
提问by devan
I have used asmx web service in my project. I want to add items to existing SharePoint list.
我在我的项目中使用了 asmx Web 服务。我想将项目添加到现有 SharePoint 列表。
mylist.Url = Url.TrimEnd('/') + "/_vti_bin/lists.asmx";
mylist.UseDefaultCredentials = true;
XmlNode node = mylist.GetList(_listName);
And I have store my values in DataTable. How can I directly add data to SharePoint list from C# Datatable? Or should I convert it to Xml and add ?
我已经将我的值存储在 DataTable 中。如何从 C# 数据表直接将数据添加到 SharePoint 列表?还是应该将其转换为 Xml 并添加?
thank you
谢谢你
采纳答案by Rawling
Take a look at this pagefor the general usage of UpdateListItems, although it only has an example for updating items.
请查看此页面以了解 的一般用法UpdateListItems,尽管它只有一个用于更新项目的示例。
Then look at this pagefor an example of the XML you need to send to create a newitem rather than updating an existing one.
然后查看此页面以获取创建新项目而不是更新现有项目所需发送的 XML 示例。
You'll need to loop through your data table constructing an XML structure for each item you want to add, but you can then add them all in a single request.
您需要遍历数据表,为要添加的每个项目构建一个 XML 结构,但随后您可以将它们全部添加到单个请求中。
回答by Vadim Gremyachev
The following example demonstrates how to consume SharePoint Web Services, in particular Lists Classto create a List Item:
以下示例演示了如何使用 SharePoint Web Services,特别是Lists 类来创建列表项:
using System;
using System.Collections.Generic;
using System.Net;
using System.Xml;
namespace SharePoint.Client
{
public class ListsClient : IDisposable
{
public ListsClient(Uri webUri, ICredentials credentials)
{
_client = new Lists.Lists();
_client.Credentials = credentials;
_client.Url = webUri + "/_vti_bin/Lists.asmx";
}
public ListsClient(Uri webUri)
{
_client = new Lists.Lists();
_client.Url = webUri + "/_vti_bin/Lists.asmx";
}
/// <summary>
/// Create a List Item
/// </summary>
/// <param name="listName">List Name</param>
/// <param name="propertyValues">List Item properties</param>
/// <returns></returns>
public XmlNode CreateListItem(string listName,Dictionary<string,string> propertyValues)
{
var payload = new XmlDocument();
var updates = payload.CreateElement("Batch");
updates.SetAttribute("OnError", "Continue");
var method = payload.CreateElement("Method");
method.SetAttribute("ID", "1");
method.SetAttribute("Cmd", "New");
foreach (var propertyValue in propertyValues)
{
var field = payload.CreateElement("Field");
field.SetAttribute("Name", propertyValue.Key);
field.InnerText = propertyValue.Value;
method.AppendChild(field);
}
updates.AppendChild(method);
return _client.UpdateListItems(listName, updates);
}
public void Dispose()
{
_client.Dispose();
GC.SuppressFinalize(this);
}
protected Lists.Lists _client; //SharePoint Web Services Lists proxy
}
}
Usage
用法
How to create a Task item:
如何创建任务项:
using (var client = new SPOListsClient(webUrl, userName, password))
{
var taskProperties = new Dictionary<string, string>();
taskProperties["Title"] = "Order approval";
taskProperties["Priority"] = "(2) Normal";
var result = client.CreateListItem(listTitle, taskProperties);
}

