我正在使用此代码将凭据传递给Sharepoint站点Web服务,
var client = new SiteWebReference.Lists();
System.Net.NetworkCredential passCredentials = new System.Net.NetworkCredential("username", "password", "domain");
client.Credentials = passCredentials;
我正在使用此代码来提取条目的Sharepoints列表,
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;
}
我一直在寻找有关向现有List添加新条目的信息,这意味着我想在“任务列表”中添加一个新条目。我得出的结论是代码应该可行,
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;
}
然后是CreateTask,
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;
}
然后我用一个按钮
调用CreateTaskprivate 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",
});
}
答案 0 :(得分:0)
请输入任务列表的列表ID
client.UpdateListItems(strListID,elBatch);
尝试使用StringBuilder或直接将值传递给elBatch.InnerXml =“your xml”
答案 1 :(得分:0)
这是因为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);
}