Geeks With Blogs

News Sharepoint Server 2010, MS SQL Server 2008 R2 and Windows Server 2008 R2 Better Together.
Read the whitepaper here:MSDN


Disclaimer
The opinions expressed herein are my personal opinions and does not represent the opinions of my employer.


free counters


mike's Blog (mr. NICE guy) "Talent, Ambition and Work Ethics ~ my way of being a SharePoint Mutt"

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

Posted on Wednesday, April 11, 2012 6:49 AM Client Object Model | Back to top


Comments on this post: How to display Sharepoint Data in a Windows Forms Application

# custom facebook app, custom facebook applications, customized facebook app development
Requesting Gravatar...
is that JAVA ORACLE app ?
and for what perpos we can you that ?

Left by custom facebook app on Apr 11, 2012 9:27 PM

# Custom SharePoint Development
Requesting Gravatar...
This article provides an excellent and detailed step by step tutorial on how to do this using Linq and Web services. It does create a console application instead of a Windows application but it is very easy to translate this into a Windows Form app if needed.
Left by Sawyer Spence on Jan 30, 2013 1:47 PM

# re: How to display Sharepoint Data in a Windows Forms Application
Requesting Gravatar...
Excellente article. Good Job.
Left by Eduardo Ramírez on Oct 12, 2013 2:22 AM

# re: How to display Sharepoint Data in a Windows Forms Application
Requesting Gravatar...
Thanks... Thats exactly what I want...
Left by Gokmen Bulut on Nov 21, 2013 10:00 PM

# re: How to implment Sharepoint in exsisting Windows Forms Application
Requesting Gravatar...
I want to know about how to implement share point in window form application
Left by Vijendra on Dec 31, 2013 3:19 PM

# re: How to display Sharepoint Data in a Windows Forms Application
Requesting Gravatar...
I tried adapting open-source code forum this project, but ended up way beyond my depth. You can use it for reference purposes, I'm sure it'll help you.
Left by patilsudhir on Jun 10, 2015 7:05 PM

# re: How to display Sharepoint Data in a Windows Forms Application
Requesting Gravatar...
When I run my code I am getting a (403) Forbidden error ...
An unhandled exception of type 'System.Net.WebException' occurred in Microsoft.SharePoint.Client.dll

What do I need to do to connect to my SharePoint site?
Left by Laura on Mar 13, 2018 10:33 PM

Your comment:
 (will show your gravatar)


Copyright © Michael M. Bangoy | Powered by: GeeksWithBlogs.net