Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, November 28, 2011

Add,Delete,Retrieve list item REST Sharepoint 2010

Add,Delete,Retrieve list item REST Sharepoint 2010

Here is a quick example below of adding an item to the Announcements list using REST in SharePoint 2010. For adding, you call the specific AddTo method for your list, such as AddToAnnouncements and for updating and deleting, you use the UpdateObject and DeleteObject methods and pass in the object you want to update or delete.

RESTReference.HomeDataContext context = new RESTReference.HomeDataContext(new Uri(“http://MySharePointSite/_vti_bin/listdata.svc”));

private void Add_Click(object sender, EventArgs e)
{
//Add a new Announcement
RESTReference.AnnouncementsItem newAnnounce = new RESTReference.AnnouncementsItem(); // Announcement type object.
newAnnounce.Title = “My New Announcement! ” + DateTime.Now.ToString();
context.AddToAnnouncements(newAnnounce);
context.SaveChanges();
}

private void Retrieve_Click(object sender, EventArgs e)
{
//Populate grid using LINQ
context.Credentials = CredentialCache.DefaultCredentials;
var q = from a in context.Announcements
select a;
this.announcementsItemBindingSource.DataSource = q; -> announcementsItemBindingSource a grid object
}

No comments:

Post a Comment