I'm using this code to pass the credentials to the Sharepoint site Web Services,
var client = new SiteWebReference.Lists();
System.Net.NetworkCredential passCredentials = new System.Net.NetworkCredential("username", "password", "domain");
client.Credentials = passCredentials;
I'm using this code to pull the Sharepoints List of Entries,
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;
}
I've been searching around for information on adding a new entry to an already existing List, meaning I want to add a new entry to the Tasks List. I've came to the conclusion that this should code should work,
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);
}
But when I check the Sharepoint site the 'new entry' in not present, any suggestions?
This is not the full XML, this is a small piece,
<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>
Edit: I have made these changes with the help of Vadim,
I now have this code,
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;
}
Then the 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;
}
Then I call the CreateTask with a button,
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",
});
}
Please put the list id of task list
client.UpdateListItems(strListID, elBatch);
try to use StringBuilder or directly pass value to elBatch.InnerXml="your xml"
This occurs since for Name
attribute in Field
element the field internal name have to be specified, for example Title
(not ows_Title
).
You could utilize the following wrapper class that abstracts SharePoint Web Services and make it easier to create list items:
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
}
}
Then you could create a task item as demonstrated below:
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.