Thursday, July 2, 2015

CRUD Operation in SharePoint List Using REST API in Windows Form with C#

Here is how we can we perform SharePoint List Add, Update and delete operations using REST API.

Structure of Form Solution we need to add a Service Reference with below URL:

http://severname:portname/sites/sitename/_vti_bin/listdata.svc

Add a Data Source using service reference by putting same URL:



Structure of the windows form application will be as below:


Now write below code to perform all operations:

namespace RESTExample
{
    public partial class REST : Form
    {
        BidManagementDataContext context;

        private void InitContext()
        {
            context = new BidManagementDataContext(new Uri("http://jsy-shpwebvt003:19891/sites/BidManagement/_vti_bin/ListData.svc"));
            context.Credentials = CredentialCache.DefaultCredentials;
        }

        public REST()
        {
            
            InitializeComponent();
        }

        private void Add_Click(object sender, EventArgs e)
        {
            InitContext();
            NotesItem objNotes = new NotesItem() { Title = "Insterted through Rest" };
            context.AddToNotes(objNotes);
            context.SaveChanges();
            BindList();
            
        }

        private void Update_Click(object sender, EventArgs e)
        {
            InitContext();
            NotesItem objNotes = context.Notes.FirstOrDefault();
            objNotes.Title = "Updated by Code...";
            context.UpdateObject(objNotes);
            context.SaveChanges();
            BindList();
        }

        private void Delete_Click(object sender, EventArgs e)
        {
            InitContext();
            NotesItem objNotes = context.Notes.FirstOrDefault();
            //objNotes.Title = "Updated by Code...";
            context.DeleteObject(objNotes);
            context.SaveChanges();
            BindList();
        }

        private void REST_Load(object sender, EventArgs e)
        {
            BindList();
        }

        private void BindList()
        {
            InitContext();
            notesBindingSource.DataSource = context.Notes;
        }

        
    }
}

1 comment:

  1. It is in point of fact a great and helpful piece of info. I am happy that you shared this useful info with us. Please stay us up to date like this. Thanks for sharing.
    SharePoint 2013 Online Training

    ReplyDelete