次の方法で共有


Getting Started using the OData REST API to Query a SharePoint List

SharePoint 2010 exposes list data via OData.  I’m currently working on an article around SharePoint and OData.  As part of this effort, at various points, I’ll blog some of my intermediate samples.  This post details the minimum number of steps to query a SharePoint list using OData.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOCThis post is one in a series on using the OData REST API to access SharePoint 2010 list data.

  1. Getting Started using the OData REST API to Query a SharePoint List
  2. Using the OData Rest API for CRUD Operations on a SharePoint List

By far, the easiest way to get started with SharePoint development is to use the 2010 Information Worker Demonstration and Evaluation Virtual Machine that runs using Hyper-V.  The instructions in this and future related posts will be for that virtual machine.  If you have setup your own development environment with a different machine name, it is easy enough to adjust these procedures to work with your own servers.

Procedure: Query a SharePoint list using OData

1.    Download, extract, and boot the virtual machine.  The download includes detailed instructions.

2.    Log into the virtual machine.  Use the username brads.  The password is pass@word1.

3.    Create a new list.  Name the list ‘Inventory’.  Create a two new columns: Description, and Cost.  Make the type of the Description column be ‘Single line of text’.  Make the type of the Cost column be ‘Currency’.  Enter a couple of rows of sample data.

After completing this task, my list looked like this:

 

4.    Start Visual Studio 2010 in the virtual machine.

5.    Create a new project.  Click File -> New -> Project.  Select a directory for the project.  Set the name of the project to Gears.  The New Project dialog box will look something like this:

 

6.    Right click on the References node in the Solution Explorer window, and click Add Service Reference.  Enter https://intranet/_vti_bin/listdata.svc for the address.  Change the namespace to Data.  The Add Service Reference dialog box will look like this:

 

Click OK.

7.    Copy and paste the following code into Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Gears.Data;

namespace Gears
{
class Program
{
static void Main(string[] args)
{
TeamSiteDataContext dc =
new TeamSiteDataContext(new Uri("https://intranet/_vti_bin/listdata.svc"));
dc.Credentials = CredentialCache.DefaultNetworkCredentials;
var result = from d in dc.Inventory
select new
{
Title = d.Title,
Description = d.Description,
Cost = d.Cost,
};
foreach (var d in result)
Console.WriteLine(d);
}
}
}

8.    Press F5 to run.  If everything worked, you will see something like the following:

Comments

  • Anonymous
    December 17, 2010
    I guess pulling back an entire list is technically a query, but a where clause would make for a better example.

  • Anonymous
    January 30, 2011
    This is very true for simple list and useful. But is this can be use to work with complex lists such as list with a lookup column that allows multiples also ? Also can this be used to upload a image to a list ?

  • Anonymous
    June 14, 2011
    Hi Eric, Thanks for your post. I have used the same method to read a sharepoint list. In my case SP List has 20000 records. however, the Odata service response has only frist 1000 records. Am I doing something wrong here ?

  • Anonymous
    November 26, 2012
    Why is the URI required.  Since the site/list cannot be set at runtime and must be set during development, specifying a URI seems redundant.  If you change the value to something other than what was specified in the service reference, it blows up.  Production vs. test is not possible with SharePoint svc calls since no proxy is provided.  Must develop using production sites.  

  • Anonymous
    November 26, 2012
    What is TeamSiteDataContext?  Shouldn't it be GearsProjectHomeDataContext?  This is the site it will be run against.

  • Anonymous
    February 07, 2013
    Even I got an error at TeamSiteDataContext. Then realized thats is name of the DataSource we just created. Go to Data->Show all data sources. (Or Hit Alt+Shift+D) You can see the name you can use instead of TeamSiteDataContext ! Also you might need to change the dc.Inventory if your Sharepoint DataSouce might not provide that element. Thanks, `Anand

  • Anonymous
    April 03, 2014
    Anand and I had the same issue... what is this TeamSiteDataContext... then duh, you need to look at your data sources! Thanks to Eric White and Anand for helping me get this going.  Now I just need to get inserts working, and we have an easy, automated way to populate lists from our ERP database!

  • Anonymous
    April 06, 2014
    I have followed this guide and have no issues working with lists.  However, I can only access lists on the top level site.  I have tried several variations of uri's to access subsites, but each time there is an error. If I try http://SharePointServer/sites/Operations/_vti_bin/listdata.svc when creating a data source, it just reverts back to the top level.  If I try that uri after creating the datacontect (in the constructor), I can still only access top level lists (and get an error when trying to access sites in the operations site). What am I missing that let's me access subsites of our top level site?