【问题标题】:OData Service not returning complete responseOData 服务未返回完整响应
【发布时间】:2016-04-29 20:30:58
【问题描述】:

我正在使用 Odata RESTful 服务读取 Sharepoint 列表数据(>20000 个条目),详见此处 -http://blogs.msdn.com/b/ericwhite/archive/2010/12/09/getting-started-using- the-odata-rest-api-to-query-a-sharepoint-list.aspx

我能够读取数据,但我只能获得前 1000 条记录。我还检查了共享点服务器上的 List View Throttling 是否设置为 5000。请多多指教。

更新:

@Turker:你的答案是正确的!非常感谢。我能够在第一次迭代中获得前 2000 条记录。但是,我在 while 循环的每次迭代中都得到了相同的记录。我的代码如下-

                         ...initial code...
                     int skipCount =0;
  while (((QueryOperationResponse)query).GetContinuation() != null)
                {
                    //query for the next partial set of customers
                    query = dc.Execute<CATrackingItem>(
                        ((QueryOperationResponse)query).GetContinuation().NextLinkUri
                        );

                    //Add the next set of customers to the full list
                    caList.AddRange(query.ToList());

                    var results = from d in caList.Skip(skipCount)
                                  select new
                                  {
                                      Actionable = Actionable,
                                    };  Created = d.Created,

                        foreach (var res in results)
                        {

                            structListColumns.Actionable = res.Actionable;
                            structListColumns.Created= res.Created;
                        }
                         skipCount = caList.Count;
                     }//Close of while loop

【问题讨论】:

    标签: odata


    【解决方案1】:

    您在提要末尾看到&lt;link rel="next"&gt; 元素吗?

    例如,如果你看

    http://services.odata.org/Northwind/Northwind.svc/Customers/

    你会看到

    <link rel="next" href="http://services.odata.org/Northwind/Northwind.svc/Customers/?$skiptoken='ERNSH'" />
    

    在提要的末尾,这意味着服务正在实现服务器端分页,您需要发送

    http://services.odata.org/Northwind/Northwind.svc/Customers/?$skiptoken='ERNSH' 
    

    查询以获得下一组结果。

    【讨论】:

      【解决方案2】:

      我没有发现您的代码有什么特别错误的地方。您可以尝试转储请求的 URL(从代码中或使用 fiddler 之类的东西),以查看客户端是否真的发送相同的查询(从而获得相同的响应)。

      无论如何,这里有一个可以工作的示例代码(使用示例服务):

      DataServiceContext ctx = new DataServiceContext(new Uri("http://services.odata.org/Northwind/Northwind.svc"));
      
      QueryOperationResponse<Customer> response = (QueryOperationResponse<Customer>)ctx.CreateQuery<Customer>("Customers").Execute();
      do
      {
          foreach (Customer c in response)
          {
              Console.WriteLine(c.CustomerID);
          }
      
          DataServiceQueryContinuation<Customer> continuation = response.GetContinuation();
          if (continuation != null)
          {
              response = ctx.Execute(continuation);
          }
          else
          {
              response = null;
          }
      } while (response != null);
      

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题,并希望它是一个通用的解决方案。 所以我用 GetAlltems 方法扩展了 DataServiceContext。

            public static List<T> GetAlltems<T>(this DataServiceContext context)
            {
                return context.GetAlltems<T>(null);
            }
        
            public static List<T> GetAlltems<T>(this DataServiceContext context, IQueryable<T> queryable)
            {
                List<T> allItems = new List<T>();
                DataServiceQueryContinuation<T> token = null;
        
                EntitySetAttribute attr = (EntitySetAttribute)typeof(T).GetCustomAttributes(typeof(EntitySetAttribute), false).First();
        
                // Execute the query for all customers and get the response object.
                DataServiceQuery<T> query = null;
        
                if (queryable == null)
                {
                    query = context.CreateQuery<T>(attr.EntitySet);
                }
                else
                {
                    query = (DataServiceQuery<T>) queryable;
                }
        
                QueryOperationResponse<T> response = query.Execute() as QueryOperationResponse<T>;
        
                // With a paged response from the service, use a do...while loop 
                // to enumerate the results before getting the next link.
                do
                {
                    // If nextLink is not null, then there is a new page to load.
                    if (token != null)
                    {
                        // Load the new page from the next link URI.
                        response = context.Execute<T>(token);
                    }
        
                    allItems.AddRange(response);
                }
                // Get the next link, and continue while there is a next link.
                while ((token = response.GetContinuation()) != null);
        
                return allItems;
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-03-22
          • 1970-01-01
          • 2021-09-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多