使用webserivces

时间:2016-05-26 13:56:30

标签: c# sharepoint workflow sharepoint-2013 sharepoint-workflow

我有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 {“发生了意外的文件结尾。以下元素未关闭:方法。第1行,第60位。”}   System.Exception {System.Xml.XmlException}
  •   

我不知道如何解决这个问题。请帮帮我。

1 个答案:

答案 0 :(得分:1)

首先,字符串不是有效的XML,因为缺少结束Method元素。它应该是

"<Method Cmd='New'><Field Name='Title'>" + Title + "</Field></Method>"

其次,ASMX服务在2010年被弃用。您不应该将它们用于任何开发之王,特别是针对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摆弄,您只需创建一个新项目,设置其属性并调用更新