如何使用web服务获取SharePoint列表中新插入行的ID



我使用以下代码使用web服务在共享点列表中插入新的列表项。

 ServiceReference.DataContext sc = new ServiceReference.DataContext(new Uri("http://servername:portnumber/_vti_bin/ListData.svc"));
 sc.Credentials = new System.Net.NetworkCredential(spusername, sppassword, spdomain);
 sc.AddToSampleList(new ServiceReference.SampleList
                            {
                                Title= "Hello World"                                                               
                            });
sc.SaveChanges();

工作正常。。

我需要新插入行的ID。。

有没有办法像在服装网页部分中一样获得新插入行的ID

  SPList spList = spWeb.Lists["SampleList"];
  SPListItem spListItem = spList.Items.Add();
  spListItem["Title"] = "Hello world";
  spListItem.Update();
  int ID=spListItem.ID;//like this

有什么帮助或建议吗。。

提前感谢。。。

您将不得不更改web服务,以返回一个整数,该整数将是新插入记录的ID。所以SaveChanges()需要返回一个int。当你这样做时,你需要更新服务引用。

 ServiceReference.SampleListItem li = new ServiceReference.SampleListItem { Title= "Hello      World" };
 sc.AddToSampleList(li);
 sc.SaveChanges();
  int id = li.Id;

请查看下面的链接以了解更多详细信息

http://social.msdn.microsoft.com/Forums/en-US/5750ddc2-a14f-4770-926d-d049a2a6332f/how-to-get-the-id-of-the-new-inserted-row-in-the-sharepoint-list-by-using-web-service?forum=sharepointdevelopment

最新更新