How to display Sharepoint Data in 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 Tue, 10 Apr 2012 21:49:00 GMT Indexed on 2012/04/11 11:31 UTC
Read the original article Hit count: 287

Filed under:

In this post 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. (Your solution should look like the one below)

solution

5. Open the Form1 in design view and from the Toolbox menu Add a Button, TextBox, Label and DataGridView on the form.

form

6. Next double click on the Load Button, this will open the code view of the form. Add Using statement to reference the Sharepoint Client Library then create two method for the Load Site Title and LoadList. See below:

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
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
    {
        // url of the Sharepoint site
        const string _context = "theurlofthesharepointsite";

        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();
         }

    }

}

7. That’s it. Hit F5 to run the application then click the Load Button. Your screen should like the one below.

formrun

Hope this helps. Winking smile

© Geeks with Blogs or respective owner