簡體   English   中英

如何使用.asmx Web服務將項目添加到Sharepoint列表

[英]How to list Add items to Sharepoint list using .asmx web service

我在我的項目中使用了asmx web服務。 我想將項目添加到現有SharePoint列表。

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

我將我的值存儲在DataTable中。 如何從C#Datatable直接將數據添加到SharePoint列表? 或者我應該將其轉換為Xml並添加?

謝謝

請查看此頁面以了解UpdateListItems的一般用法,盡管它只有一個更新項目的示例。

然后查看此頁面以獲取您需要發送以創建項目而不是更新現有項目的XML示例。

您需要循環遍歷數據表,為要添加的每個項目構建XML結構,但您可以在一個請求中將它們全部添加。

以下示例演示如何使用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);    
}

參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM