Before you can proceed further with the example verify that the WCF Data services are installed in your environment, as it does not comes with the SharePoint installation. To verify the install enter the following URL to your browser: http://<server name>/_vti_bin/ListData.svc
- Re-Design the form as shown in the image.
- On the Form_Load add the code
- · I have added three buttons
- Add: This is to add the new Item in the List.
- Refresh: This is to refresh the list so as to see the current added item in the List.
- Clear: This is to clear the textboxes.
- Full Code from the code behind
namespace RestSharePointList
{
public partial class Form1 : Form
{
//Create a Proxy of the service
RESTListService.LearningDataContext objContext = new LearningDataContext(new Uri("http://br-pc-350:2789/_vti_bin/ListData.svc"));
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//provide the credentials for the proxy to access the SharePoint List
objContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
//Create a Linq Query to get the data from the list using the proxy
var listData = from p in objContext.LearningList
select p;
//You can also filter the data from the List by modifing the LINQ Query
//Bind the DataGrid with the data retreived from the List
this.learningListBindingSource.DataSource = listData;
}
private void btnClear_Click(object sender, EventArgs e)
{
//Emptying the Text Boxes
txtAuthor.Text = "";
txtInStock.Text = "";
txtPrice.Text = "";
txtTitle.Text = "";
}
private void btnAdd_Click(object sender, EventArgs e)
{
//create a List object and assign the values
LearningListItem objItem = new LearningListItem();
objItem.Author = txtAuthor.Text;
objItem.Price = txtPrice.Text;
objItem.InStock = txtInStock.Text;
objItem.Title = txtTitle.Text;
try
{
//Add the Items to the List
objContext.AddToLearningList(objItem);
objContext.SaveChanges();
MessageBox.Show("Item added Successfully");
}
catch(Exception ex)
{
MessageBox.Show("Error Occured: " + ex.Message);
}
}
private void btnRefresh_Click(object sender, EventArgs e)
{
//provide the credentials for the proxy to access the SharePoint List
objContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
//Create a Linq Query to get the data from the list using the proxy
var listData = from p in objContext.LearningList
select p;
//You can also filter the data from the List by modifing the LINQ Query
//Bind the DataGrid with the data retreived from the List
this.learningListBindingSource.DataSource = listData;
}
}
}
- Now press F5 and the output will look something like this.