Compartilhar via


Using the OData Rest API for CRUD Operations on a SharePoint List

SharePoint 2010 exposes list data via OData.  This post contains four super-small code snippets that show how to Create, Read, Update, and Delete items in a SharePoint list using the OData Rest API.

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

These snippets work as written with the 2010 Information Worker Demonstration and Evaluation Virtual Machine.  That VM is a great way to try out SharePoint 2010 development.  Also see How to Install and Activate the IW Demo/Evaluation Hyper-V Machine.

See the first post in this series, Getting Started using the OData REST API to Query a SharePoint List, for detailed instructions on how to build an application that uses OData to query a list.  These snippets use the list that I describe how to build in that post.

Query a List

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

class Program
{
static void Main(string[] args)
{
// query
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);
}
}

Create an Item

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

class Program
{
static void Main(string[] args)
{
// create item
TeamSiteDataContext dc =
new TeamSiteDataContext(new Uri("https://intranet/_vti_bin/listdata.svc"));
dc.Credentials = CredentialCache.DefaultNetworkCredentials;
InventoryItem newItem = new InventoryItem();
newItem.Title = "Boat";
newItem.Description = "Little Yellow Boat";
newItem.Cost = 300;
dc.AddToInventory(newItem);
dc.SaveChanges();
}
}

Update an Item

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

class Program
{
static void Main(string[] args)
{
// update item
TeamSiteDataContext dc =
new TeamSiteDataContext(new Uri("https://intranet/_vti_bin/listdata.svc"));
dc.Credentials = CredentialCache.DefaultNetworkCredentials;
InventoryItem item = dc.Inventory
.Where(i => i.Title == "Car")
.FirstOrDefault();
item.Title = "Car";
item.Description = "Super Fast Car";
item.Cost = 500;
dc.UpdateObject(item);
dc.SaveChanges();
}
}

Delete an Item

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

class Program
{
static void Main(string[] args)
{
// delete item
TeamSiteDataContext dc =
new TeamSiteDataContext(new Uri("https://intranet/_vti_bin/listdata.svc"));
dc.Credentials = CredentialCache.DefaultNetworkCredentials;
InventoryItem item = dc.Inventory
.Where(i => i.Title == "Car")
.FirstOrDefault();
dc.DeleteObject(item);
dc.SaveChanges();
}
}

Comments

  • Anonymous
    October 13, 2011
    Is it possible to insert a .pdf file into a document library this way?

  • Anonymous
    May 29, 2013
    My list has a person or group column and when I try to run an insert or update, all fields get updated except that one. Any thoughts as to why?

  • Anonymous
    August 13, 2015
    How to update the file and field a single request?