【问题标题】:Write on other sharepoint site using webserivces使用 webserviceces 在其他共享点站点上写入
【发布时间】:2016-05-26 13:56:30
【问题描述】:

我有 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}

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

【问题讨论】:

  • XML 字符串无效。您需要为 all 元素添加结束元素,而不仅仅是最后一个
  • 你为什么不使用 CSOM 呢?您可以像使用任何其他 ORM 或 OData 客户端一样简单地更新 Name Title 属性。 asmx 网络服务早在 2010 年就被弃用了,所以你的代码从定义上来说是有问题的。
  • 不知道怎么做比较好。

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


【解决方案1】:

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

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

其次,ASMX 服务早在 2010 年就被弃用了。您不应该将它们用于任何开发之王,尤其是针对 SP 2013。客户端对象模型 (CSOM)很多更简单,更易于使用。文档中有a lot 的示例。创建新项目的 sn-p 是:

// 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 摆弄,您只需创建一个新项目,设置其属性并调用更新

【讨论】:

  • list.Credentials... 之后编写代码时,我采用+ ex {"The remote server returned an error: (401) Unauthorized."} System.Exception {System.Net.WebException}。怎么修?谢谢。
猜你喜欢
  • 1970-01-01
  • 2016-05-21
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多