4

我在我的项目中使用了 asmx Web 服务。我想将项目添加到现有的 SharePoint 列表。

   mylist.Url = Url.TrimEnd('/') + "/_vti_bin/lists.asmx";
            mylist.UseDefaultCredentials = true;
            XmlNode node = mylist.GetList(_listName);

我已经将我的值存储在 DataTable 中。如何从 C# Datatable 直接将数据添加到 SharePoint 列表?还是应该将其转换为 Xml 并添加?

谢谢你

4

2 回答 2

7

查看此页面以了解 的一般用法UpdateListItems,尽管它只有一个更新项目的示例。

然后查看此页面以获取创建项目而不是更新现有项目所需发送的 XML 示例。

您需要遍历数据表,为要添加的每个项目构建一个 XML 结构,但是您可以在一个请求中将它们全部添加。

于 2012-07-06T07:46:13.503 回答
0

以下示例演示如何使用 SharePoint Web 服务,特别是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

    }
}

用法

如何创建任务项:

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);    
}

参考

于 2014-12-29T12:40:04.303 回答