向 SharePoint 2007 列表中添加新条目(使用 Web 服务拉取)
本文关键字:Web 使用 服务 新条目 2007 SharePoint 列表 添加 | 更新日期: 2023-09-27 18:37:03
我使用此代码将凭据传递给 SharePoint 网站 Web 服务,
var client = new SiteWebReference.Lists();
System.Net.NetworkCredential passCredentials = new System.Net.NetworkCredential("username", "password", "domain");
client.Credentials = passCredentials;
我正在使用此代码拉取条目的共享点列表,
private IEnumerable<TaskEntry> LoadTasks()
{
var data = GetListItems("Tasks");
var result = XElement.Parse(data.OuterXml);
XNamespace z = "#RowsetSchema";
var taskItems = from r in result.Descendants(z + "row")
select new TaskEntry
{
TaskName = r.Attribute("ows_LinkTitle").Value,
DueDate = r.Attribute("ows_DueDate") != null ? r.Attribute("ows_DueDate").Value : string.Empty,
AssignedTo = r.Attribute("ows_AssignedTo") != null ? r.Attribute("ows_AssignedTo").Value : string.Empty,
};
return taskItems;
}
我一直在四处寻找有关向现有列表添加新条目的信息,这意味着我想向任务列表添加新条目。我得出的结论是,这应该代码应该有效,
public void updateListItemsWS()
{
var client = new SPWebservices.Lists();
System.Net.NetworkCredential passCredentials = new System.Net.NetworkCredential("username", "password", "domain");
client.Credentials = passCredentials;
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
elBatch.SetAttribute("ListVersion", "1");
string strBatch = "<Method ID='1' Cmd='New'>" +
"<Field Name='ows_Client'>Miami Coalition of Partners</Field>" +
"<Field Name='ows_Title'>TestingApplication</Field>" +
"<Field Name='ows_Task_x0020_Type'>Support</Field>" +
"<Field Name='ows_Priority'>1</Field>" +
"<Field Name='ows_Status'>Assigned</Field>" +
"<Field Name='ows_AssignedTo'>57;#Sandro Perez</Field>" +
"<Field Name='ows_Owner'>57;#Sandro Perez</Field>" +
"<Field Name='ows_Body'>Testing my application</Field>" +
"<Field Name='ows_DueDate'>2014-04-21 00:00:00</Field>" +
"<Field Name='ows_Area'>2014-04-21 00:00:00</Field>" +
"<Field Name='ows_Group_x0020_Task'>WELS</Field>" +
"</Method>";
elBatch.InnerXml = strBatch;
client.UpdateListItems("Tasks", elBatch);
}
但是当我检查 SharePoint 网站时,"新条目"不存在,有什么建议吗?
这不是完整的XML,这是一小块,
<listitems
xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882"
xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882"
xmlns:rs="urn:schemas-microsoft-com:rowset"
xmlns:z="#RowsetSchema"
xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<rs:data ItemCount="691">
<z:row ows_Attachments="0" ows_ID="2108" ows_Task_x0020_Type="Issue" ows_Client="City University of New York" />
</rs:data>
编辑:我在瓦迪姆的帮助下进行了这些更改,
我现在有这个代码,
public class ListsClient : IDisposable
{
public ListsClient(Uri webUri, ICredentials credentials)
{
_client = new Bluejeanware.MWELS.Lists();
_client.Credentials = credentials;
_client.Url = webUri + "/_vti_bin/Lists.asmx";
}
public ListsClient(Uri webUri)
{
_client = new Bluejeanware.MWELS.Lists();
_client.Url = webUri + "/_vti_bin/Lists.asmx";
}
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 ICredentials Credentials
{
get { return _client.Credentials; }
set { _client.Credentials = value; }
}
public CookieContainer CookieContainer
{
get { return _client.CookieContainer; }
set { _client.CookieContainer = value; }
}
public void Dispose()
{
_client.Dispose();
GC.SuppressFinalize(this);
}
protected Bluejeanware.MWELS.Lists _client;
}
然后是创建任务,
public void CreateTask(TaskEntry taskEntry)
{
var webUri = new Uri("http://sp.site.com/apps/msite/_vti_bin/Lists.asmx");
var userName = "username";
var password = "password";
var domain = "domain";
var client = new ListsClient(webUri);
client.Credentials = new System.Net.NetworkCredential(userName, password, domain);
var taskProperties = new Dictionary<string, string>();
taskProperties["Client"] = taskEntry.Client;
taskProperties["Title"] = taskEntry.Title;
taskProperties["Task_x0020_Type"] = taskEntry.TaskType;
taskProperties["Priority"] = taskEntry.Priority;
taskProperties["Status"] = taskEntry.Status;
taskProperties["AssignedTo"] = taskEntry.AssignedTo;
taskProperties["Owner"] = taskEntry.Owner;
taskProperties["Body"] = taskEntry.Body;
taskProperties["DueDate"] = taskEntry.DueDate;
taskProperties["Area"] = taskEntry.Area;
taskProperties["Group_x0020_Task"] = taskEntry.GroupTask;
}
然后我用一个按钮调用创建任务,
private void Button_Click(object sender, RoutedEventArgs e)
{
CreateTask(new TaskEntry
{
Client = "VALUE",
Title = "TestingApplication",
TaskType = "VALUE",
Priority = "1",
Status = "Assigned",
AssignedTo = "57;#Sandro Perez",
Owner = "57;#Sandro Perez",
Body = "Testing my application",
DueDate = "2014-04-21 00:00:00",
Area = "VALUE",
GroupTask = "VALUE",
});
}
请输入任务列表的列表 ID
客户。UpdateListItems(strListID, elBatch);
尝试使用 StringBuilder 或直接将值传递给 elBatch.InnerXml="your xml"
发生这种情况是因为对于元素Name
属性Field
必须指定字段内部名称,例如Title
(而不是ows_Title
)。
您可以使用以下包装类来抽象 SharePoint Web 服务,并更轻松地创建列表项:
using System;
using System.Collections.Generic;
using System.Net;
using System.Xml;
namespace App
{
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 ICredentials Credentials
{
get { return _client.Credentials; }
set { _client.Credentials = value; }
}
public CookieContainer CookieContainer
{
get { return _client.CookieContainer; }
set { _client.CookieContainer = value; }
}
public void Dispose()
{
_client.Dispose();
GC.SuppressFinalize(this);
}
protected Lists.Lists _client; //SharePoint Web Services Lists proxy
}
}
然后,您可以创建一个任务项,如下所示:
public void CreateTask(TaskEntry taskEntry)
{
var webUri = new Uri("http://intranet.contoso.com/");
var userName = "username";
var password = "password";
var domains = "domain";
var client = new ListsClient(webUri);
client.Credentials = new System.Net.NetworkCredential(userName, password, domain);
var taskProperties = new Dictionary<string, string>();
taskProperties["Title"] = taskEntry.TaskName;
taskProperties["DueDate"] = taskEntry.DueDate;
taskProperties["AssignedTo"] = taskEntry.AssignedTo;
client.CreateListItem("Tasks", taskProperties);
}