C# 如何使用.asmx web服务将项目添加到Sharepoint列表
我在我的项目中使用了ASMXWeb服务。我想将项目添加到现有SharePoint列表中C# 如何使用.asmx web服务将项目添加到Sharepoint列表,c#,winforms,list,sharepoint-2010,asmx,C#,Winforms,List,Sharepoint 2010,Asmx,我在我的项目中使用了ASMXWeb服务。我想将项目添加到现有SharePoint列表中 mylist.Url = Url.TrimEnd('/') + "/_vti_bin/lists.asmx"; mylist.UseDefaultCredentials = true; XmlNode node = mylist.GetList(_listName); 我已经在DataTable中存储了我的值。如何从C#Datatable直接向Share
mylist.Url = Url.TrimEnd('/') + "/_vti_bin/lists.asmx";
mylist.UseDefaultCredentials = true;
XmlNode node = mylist.GetList(_listName);
我已经在DataTable中存储了我的值。如何从C#Datatable直接向SharePoint列表添加数据?
或者我应该将其转换为Xml并添加
感谢您了解一下更新列表的一般用法
,尽管它只有一个更新项目的示例
然后看一个创建新项目而不是更新现有项目所需发送的XML示例
您需要在数据表中循环,为要添加的每个项目构建一个XML结构,但您可以在一个请求中添加所有项目。以下示例演示如何使用SharePoint Web Services,特别是创建列表项目:
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
}
}
使用系统;
使用System.Collections.Generic;
Net系统;
使用System.Xml;
名称空间SharePoint.Client
{
公共类列表客户端:IDisposable
{
公共列表客户端(Uri webUri、ICredentials凭据)
{
_client=newlists.Lists();
_client.Credentials=凭证;
_client.Url=webUri+“/\u vti\u bin/Lists.asmx”;
}
公共列表客户端(Uri webUri)
{
_client=newlists.Lists();
_client.Url=webUri+“/\u vti\u bin/Lists.asmx”;
}
///
///创建一个列表项
///
///列表名
///列表项属性
///
公共XmlNode CreateListItem(字符串listName、字典属性值)
{
var payload=新的XmlDocument();
var updates=payload.CreateElement(“批处理”);
updates.SetAttribute(“OnError”、“Continue”);
var method=payload.CreateElement(“方法”);
方法.SetAttribute(“ID”,“1”);
方法SetAttribute(“Cmd”、“New”);
foreach(propertyValue中的var propertyValue)
{
var field=payload.CreateElement(“字段”);
field.SetAttribute(“Name”,propertyValue.Key);
field.InnerText=propertyValue.Value;
方法:追加子项(字段);
}
AppendChild(方法);
返回_client.UpdateListItems(列表名,更新);
}
公共空间处置()
{
_client.Dispose();
总干事(本);
}
受保护的列表。列表_client;//SharePoint Web Services列表代理
}
}
用法
如何创建任务项:
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);
}
使用(var客户端=新的SPOListsClient(webUrl、用户名、密码))
{
var taskProperties=new Dictionary();
taskProperties[“Title”]=“订单审批”;
taskProperties[“优先级”]=“(2)正常”;
var result=client.CreateListItem(listTitle、taskProperties);
}
工具书类