How to Create SharePoint List and Insert List Item programmatically 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 Tue, 10 Apr 2012 23:26:00 GMT
Indexed on
2012/04/11
11:31 UTC
Read the original article
Hit count: 321
In this post I’m going to demonstrate how to create SharePoint List and also Insert Items on the List from a Windows Forms Application.
1. Open Visual Studio and create a new project. On the project template select Windows Form Application under C#.
2. 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.
3. Select the Microsoft.Sharepoint.Client.dll and Microsoft.Sharepoint.Client.Runtime.dll. (Your solution should look like the one below)
4. Open the Form1 in design view and from the Toolbox menu add a button on the form surface. Your form should look like the one below.
5. Double click the button to open the code view. Add Using statement to reference the Sharepoint Client Library then create method for the Create List. Your code should like the codes 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 = "urlofthesharepointsite";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void cmdcreate_Click(object sender, EventArgs e)
{
try
{
// declare the ClientContext Object
SP.ClientContext _clientcontext = new SP.ClientContext(_context);
SP.Web _site = _clientcontext.Web;
// declare a ListCreationInfo
SP.ListCreationInformation _listcreationinfo = new SP.ListCreationInformation();
// set the Title and the Template of the List to be created
_listcreationinfo.Title = "NewListFromCOM";
_listcreationinfo.TemplateType = (int)SP.ListTemplateType.GenericList;
// Call the add method to the ListCreatedInfo
SP.List _list = _site.Lists.Add(_listcreationinfo);
// Add Description field to the List
SP.Field _Description = _list.Fields.AddFieldAsXml(@"
<Field Type='Text'
DisplayName='Description'>
</Field>", true, SP.AddFieldOptions.AddToDefaultContentType);
// declare the List item Creation object for creating List Item
SP.ListItemCreationInformation _itemcreationinfo = new SP.ListItemCreationInformation();
// call the additem method of the list to insert a new List Item
SP.ListItem _item = _list.AddItem(_itemcreationinfo);
_item["Title"] = "New Item from Client Object Model";
_item["Description"] = "This item was added by a Windows Forms Application";
// call the update method
_item.Update();
// execute the query of the clientcontext
_clientcontext.ExecuteQuery();
// dispose the clientcontext
_clientcontext.Dispose();
MessageBox.Show("List Creation Successfull");
}
catch(Exception ex)
{
MessageBox.Show("Error creating list" + ex.ToString());
}
}
}
}
6. Hit F5 to run the application. A message will be displayed on the screen if the operation is successful and also if it fails.
7. To make that the operation of our Windows Form Application has really created the List and Inserted an item on it. Let’s open our SharePoint site. Once the SharePoint is open click on the Site Actions then View All Site Content.
7. Click the List to open it and check if an Item is inserted.
That’s it. Hope this helps.
© Geeks with Blogs or respective owner