How to retrieve Sharepoint data from a Windows Forms Application.
Posted
by Michael M. Bangoy
on Geeks with Blogs
See other posts from Geeks with Blogs
or by Michael M. Bangoy
Published on Wed, 04 Apr 2012 09:22:21 GMT
Indexed on
2012/04/04
11:31 UTC
Read the original article
Hit count: 266
In this demo I'm going to demonstrate how to retrieve Sharepoint data and display it on a Windows Forms Application.
1. Open Visual Studio 2010 and create a new Project.
2. In the project template select Windows Forms Application.
3. In order to communicate with Sharepoint from a Windows Forms Application we need to add the 2 Sharepoint Client DLL located in c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI.
4. Select the Microsoft.Sharepoint.Client.dll and Microsoft.Sharepoint.Client.Runtime.dll. That's it we're ready to write our codes. Note: In this example I've added to controls on the form, the controls are Button, TextBox, Label and DataGridView.
using
System;using System.Collections.Generic;
using
System.ComponentModel;using
System.Data;using
System.Data.Objects;using
System.Drawing;using
System.Linq;using
System.Text;using
System.Security;using
System.Windows.Forms;using
SP = Microsoft.SharePoint.Client;
namespace
ClientObjectModel{
public partial class Form1 : Form{
// declare string url of the Sharepoint site string _context = "theurlofyoursharepointsite"; public Form1(){
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e){
}
private void getsitetitle(){
SP.
ClientContext context = new SP.ClientContext(_context);SP.
Web _site = context.Web;context.Load(_site);
context.ExecuteQuery();
txttitle.Text = _site.Title;
context.Dispose();
}
private void loadlist(){
using (SP.ClientContext _clientcontext = new SP.ClientContext(_context)){
SP.
Web _web = _clientcontext.Web;SP.
ListCollection _lists = _clientcontext.Web.Lists;_clientcontext.Load(_lists);
_clientcontext.ExecuteQuery();
DataTable dt = new DataTable(); DataColumn column; DataRow row;column =
new DataColumn();column.DataType =
Type.GetType("System.String");column.ColumnName =
"List Title";dt.Columns.Add(column);
foreach (SP.List listitem in _lists){
row = dt.NewRow();
row[
"List Title"] = listitem.Title;dt.Rows.Add(row);
}
dataGridView1.DataSource = dt;
}
private void cmdload_Click(object sender, EventArgs e){
getsitetitle();
loadlist();
}
}
That's it. Running the application and clicking the Load Button will retrieve the Title of the Sharepoint site and display it on the TextBox and also it will retrieve ALL of the Sharepoint List on that site and populate the DataGridView with the List Title.
Hope this helps. Thank you.
© Geeks with Blogs or respective owner