使用 webserivces 在其他 sharepoint 网站上写
Write on other sharepoint site using webserivces
我有 2 个 Sharepoint 2013 网站。
当用户在第一个 SPSite 的 SPList 中添加新项目 -> 开始工作流时,必须在第二个 SPSite 的 SPList 中添加项目的副本。这是我的代码:
public void UpdateSPList(string Title)
{
using (AuthenticationSvc.Authentication authSvc = new AuthenticationSvc.Authentication())
{
try
{
using (ListsSvc.Lists list = new ListsSvc.Lists())
{
list.Url = @"http://second-srharepoint-site.com/_vti_bin/Lists.asmx";
list.CookieContainer = new System.Net.CookieContainer();
list.AllowAutoRedirect = true;
list.PreAuthenticate = true;
list.Credentials = new System.Net.NetworkCredential("domain\username", "password");
string strBatch = "<Method Cmd='New'><Field Name='Title'>" + Title + "</Field> ";
XmlDocument xmlDoc = new XmlDocument();
XmlElement elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
elBatch.InnerXml = strBatch;
XmlNode ndReturn = list.UpdateListItems("SPListName", elBatch);
}
}
finally
{
}
}
}
但在线 elBatch.InnerXml = strBatch;
我得到异常:
- $exception {"Unexpected end of file has occurred. The following elements are not closed: Method. Line 1, position 60."}
System.Exception {System.Xml.XmlException}
我不知道如何解决这个问题。请帮助我。
首先,该字符串无效 XML,因为缺少结束 Method
元素。应该是
"<Method Cmd='New'><Field Name='Title'>" + Title + "</Field></Method>"
其次,早在 2010 年就弃用了 ASMX 服务。您不应该将它们用于任何开发之王,尤其是针对 SP 2013。客户端对象模型 (CSOM) 很多 更简单易用。文档中有 a lot 个示例。创建新项目的代码段是:
// Starting with ClientContext, the constructor requires a URL to the
// server running SharePoint.
ClientContext context = new ClientContext("http://SiteUrl");
// Assume that the web has a list named "Announcements".
List announcementsList = context.Web.Lists.GetByTitle("Announcements");
// We are just creating a regular list item, so we don't need to
// set any properties. If we wanted to create a new folder, for
// example, we would have to set properties such as
// UnderlyingObjectType to FileSystemObjectType.Folder.
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem newItem = announcementsList.AddItem(itemCreateInfo);
newItem["Title"] = "My New Item!";
newItem["Body"] = "Hello World!";
newItem.Update();
context.ExecuteQuery();
无需 XML 摆弄,您只需创建一个新项目,设置其属性并调用 Update
我有 2 个 Sharepoint 2013 网站。 当用户在第一个 SPSite 的 SPList 中添加新项目 -> 开始工作流时,必须在第二个 SPSite 的 SPList 中添加项目的副本。这是我的代码:
public void UpdateSPList(string Title)
{
using (AuthenticationSvc.Authentication authSvc = new AuthenticationSvc.Authentication())
{
try
{
using (ListsSvc.Lists list = new ListsSvc.Lists())
{
list.Url = @"http://second-srharepoint-site.com/_vti_bin/Lists.asmx";
list.CookieContainer = new System.Net.CookieContainer();
list.AllowAutoRedirect = true;
list.PreAuthenticate = true;
list.Credentials = new System.Net.NetworkCredential("domain\username", "password");
string strBatch = "<Method Cmd='New'><Field Name='Title'>" + Title + "</Field> ";
XmlDocument xmlDoc = new XmlDocument();
XmlElement elBatch = xmlDoc.CreateElement("Batch");
elBatch.SetAttribute("OnError", "Continue");
elBatch.InnerXml = strBatch;
XmlNode ndReturn = list.UpdateListItems("SPListName", elBatch);
}
}
finally
{
}
}
}
但在线 elBatch.InnerXml = strBatch;
我得到异常:
- $exception {"Unexpected end of file has occurred. The following elements are not closed: Method. Line 1, position 60."} System.Exception {System.Xml.XmlException}
我不知道如何解决这个问题。请帮助我。
首先,该字符串无效 XML,因为缺少结束 Method
元素。应该是
"<Method Cmd='New'><Field Name='Title'>" + Title + "</Field></Method>"
其次,早在 2010 年就弃用了 ASMX 服务。您不应该将它们用于任何开发之王,尤其是针对 SP 2013。客户端对象模型 (CSOM) 很多 更简单易用。文档中有 a lot 个示例。创建新项目的代码段是:
// Starting with ClientContext, the constructor requires a URL to the
// server running SharePoint.
ClientContext context = new ClientContext("http://SiteUrl");
// Assume that the web has a list named "Announcements".
List announcementsList = context.Web.Lists.GetByTitle("Announcements");
// We are just creating a regular list item, so we don't need to
// set any properties. If we wanted to create a new folder, for
// example, we would have to set properties such as
// UnderlyingObjectType to FileSystemObjectType.Folder.
ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
ListItem newItem = announcementsList.AddItem(itemCreateInfo);
newItem["Title"] = "My New Item!";
newItem["Body"] = "Hello World!";
newItem.Update();
context.ExecuteQuery();
无需 XML 摆弄,您只需创建一个新项目,设置其属性并调用 Update