Programming SharePoint List

No.of Views1128
Bookmarked0 times
Downloads 
Votes0
By  Dhananjay Kumar   On  16 Feb 2010 00:02:56
Tag : SharePoint , Development and Programming
In this article, I am going to show how we could work with SharePoint items using object model or in other words using .Net code
emailbookmark add commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Objective

In this article, I am going to show how we could work with SharePoint items using object model or in other words using .Net code. I will show you

1. How to add item into the SharePoint list?
2. How to retrieve item from the SharePoint list?
3. How to update item into the SharePoint list?
4. How to delete item from the SharePoint list?

Assumption:

1. I have created a custom list called Reader.
2. There are two columns. One is Reader’sName. This is a single line text column. Second is Reader’sArticle. This is numeric column.
3. I have put some item also in Reader list.

In SharePoint site, Reader list look like as below,

Image Loading

Here, I assume you know how to create a custom list in a SharePoint site and how to put some data into that. If you do not know, read my article on the same here on this site. Now I am going to manipulate the above list through code.
I am going to follow the below steps.

1. Create a window application
2. Add reference of Microsoft.SharePoint dll.
3. Add few controls like buttons and text boxes. I am a bad UI designer. So don’t follow my window form here. You design of yours.
4. Return a site collection using SPSite.
5. Return particular website where list is added using SPWeb.
6. Return collection of list items using SPListItemCollection.
7. Work with a particular list item using SPListItem. 

Adding Microsoft.SharePoint dll

 

Image Loading

Design of the Form

1. There are two text boxes.
2. Three buttons to add, delete and update.

 

Image Loading

 

Explanation

1. I am returning the site collection on the form load.
2. I am returning the top level site also on form load.

Adding to the List

1. Readers is name of the list
2. I am returning the List collection and adding the item into that.
3. I am reading the items to be added from the textboxes.


Code

private void btnAdd_Click(object sender, EventArgs e)
    {string Name = txtName.Text;int Numofarticle = Convert.ToInt32(txtNumberofArticle.Text);
        _ItemCollection = _MyWeb.Lists["Readers"].Items;
        _MyItem = _ItemCollection.Add();
        _MyItem["Reader'sName"] = Name;
        _MyItem["Reader'sArticle"] = Numofarticle;
        _MyItem.Update();
        txtName.Text = "";
        txtNumberofArticle.Text = "";
        MessageBox.Show("Item Added");
    }

Updating to the List

1. Readers is name of the list
2. I am returning the List collection and updating the item into that.
3. I am reading the items to be added from the textboxes.
4. I am iterating through the collection and searching for the Reader’sName to be updated.

Code

private void btnEdit_Click(object sender, EventArgs e)
    {
        _ItemCollection = _MyWeb.Lists["Readers"].Items;int count = _ItemCollection.Count;for (int i = 0; i < count; i++)
        {
            SPListItem item = _ItemCollection[i];if (item["Reader'sName"].ToString() == txtName.Text.ToString())
            {
                item["Reader'sArticle"] = Convert.ToInt32(txtNumberofArticle.Text);
                item.Update();
            }
        }

        MessageBox.Show("Updated");

    }

Deleting from the List

1. Readers is name of the list
2. I am returning the List collection and updating the item into that.
3. I am reading the items to be added from the textboxes.
4. I am iterating through the collection and searching for the Reader’sName to be deleted

Code

private void btnDelete_Click(object sender, EventArgs e)
    {
        _ItemCollection = _MyWeb.Lists["Readers"].Items;int count = _ItemCollection.Count;for (int i = 0; i < count; i++)
        {
            SPListItem item = _ItemCollection[i];if (item["Reader'sName"].ToString() == txtName.Text.ToString())
            {
                _ItemCollection.Delete(i);
                count--;
            }
        }
        MessageBox.Show("Deleted");
    }

Complete Code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SharePoint;

namespace ProgrammingList
{public partial class Form1 : Form
    {

        SPSite _MySite = null;
        SPWeb _MyWeb = null;
        SPListItemCollection _ItemCollection = null;
        SPListItem _MyItem = null;public Form1()
        {

            InitializeComponent();

        }private void btnAdd_Click(object sender, EventArgs e)
        {
            MessageBox.Show(_MyWeb.Title);string Name = txtName.Text;int Numofarticle = Convert.ToInt32(txtNumberofArticle.Text);

            _ItemCollection = _MyWeb.Lists["Readers"].Items;

            _MyItem = _ItemCollection.Add();
            _MyItem["Reader'sName"] = Name;
            _MyItem["Reader'sArticle"] = Numofarticle;
            _MyItem.Update();
            txtName.Text = "";
            txtNumberofArticle.Text = "";
            MessageBox.Show("Item Added");


        }private void Form1_Load(object sender, EventArgs e)
        {

            _MySite = new SPSite("http://adfsaccount:2222/");// _MyWeb = _MySite.AllWebs["Document Center"];_MyWeb = _MySite.OpenWeb();

        }private void btnDelete_Click(object sender, EventArgs e)
        {
            _ItemCollection = _MyWeb.Lists["Readers"].Items;int count = _ItemCollection.Count;for (int i = 0; i < count; i++)
            {
                SPListItem item = _ItemCollection[i];if (item["Reader'sName"].ToString() == txtName.Text.ToString())
                {
                    _ItemCollection.Delete(i);
                    count--;
                }
            }

            MessageBox.Show("Deleted");

        }private void btnEdit_Click(object sender, EventArgs e)
        {
            _ItemCollection = _MyWeb.Lists["Readers"].Items;int count = _ItemCollection.Count;for (int i = 0; i < count; i++)
            {
                SPListItem item = _ItemCollection[i];if (item["Reader'sName"].ToString() == txtName.Text.ToString())
                {
                    item["Reader'sArticle"] = Convert.ToInt32(txtNumberofArticle.Text);
                    item.Update();
                }
            }

            MessageBox.Show("Updated");

        }
    }
}


Conclusion

In this article, I showed how to manipulate SharePoint list using code. Thanks for reading. Happy Coding

 
Sign Up to vote for this article
 
About Author
 
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Location-India
Joined date-20 Jul 2009
Home Page-http://dhananjaykumar.net/
Blog Page-http://dhananjaykumar.net/
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at http://dhananjaykumar.net/ . You can follow him http://twitter.com/debugmode_/ and reach him at dhananjay.25july@gmail.com
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top