Sunday, April 10, 2011

Sharepoint Object Model

The Microsoft.Office.Server namespace is the root namespace of all Office Server objects and Microsoft.SharePoint
 
is the root namespace for all WSS objects.

Figure 1 illustrates some of the key classes contained in each of these namespaces, as well as to which functional
area they belong. The namespace of each group of classes is shown in parentheses next to the functional area.
For example, all list-related functionality is handled within the
Microsoft.SharePoint namespace.

FIGURE 1 Key classes in the SharePoint object model, segmented by functional area.

Object Model                                                                       Corresponding Microsoft Class for these
Web application                                                                  
Site Collections                                                                    SPSiteCollectioin
Site Collection                                                                      SPSite
Sites                                                                                       SPWebCollection
Site                                                                                         SPWeb
Lists                                                                                        SPListCollection
List/Library                                                                            SPList
Items/Rows                                                                           SPListItemCollection
Item/Row                                                                               SPListItem

A Simple Program to demonstrate SharePoint Object Model.

Create a list in your website and create three columns named: Title, Desc, Status

1.Create a Windows application and a form in it.
Figure :  Object Model Form
2. now code it is code for this application to work with list items of your SharePoint Website:
sing 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 SPObjectModel
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadData();
        }
/* this is like bind function in asp.net databinding with gridview */
        private void LoadData()
        {
            try
            {
//first create your main site- in yello box write your sharepoint website url
                using (SPSite spSite = new SPSite("http://s-e0912c86c85d4:40/"))
                {   //now create get the object of your website that have list
                    using (SPWeb spWeb = spSite.OpenWeb())
                    {
                        lstItems.Items.Clear();
                        //create object or get your list from database using this. 
                        SPList list = spWeb.Lists["Employee"];
                        foreach (SPListItem item in list.Items)
                        {
                            lstItems.Items.Add(item.Title);
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }
        }

        private void lstItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                using (SPSite spSite = new SPSite("http://s-e0912c86c85d4:40/"))
                {
                    using (SPWeb spWeb = spSite.OpenWeb())
                    {
                        SPList list = spWeb.Lists["Employee"];

                        SPQuery spQuery = new SPQuery();
                        spQuery.Query = "<Where><Eq><FieldRef Name='Title' />"
                            + "<Value Type='Text'>" + lstItems.SelectedItem + "</Value></Eq></Where>";

                        SPListItemCollection items= list.GetItems(spQuery);
                        SPListItem item = items[0];

                        txtTitle.Text = Convert.ToString(item["Title"]);
                        txtDesc.Text = Convert.ToString(item["Desc"]);
                        txtStatus.Text = Convert.ToString(item["Status"]);
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                using (SPSite spSite = new SPSite("http://s-e0912c86c85d4:40/"))
                {
                    using (SPWeb spWeb = spSite.OpenWeb())
                    {
                        SPList list = spWeb.Lists["Employee"];

                        SPListItem item = list.Items.Add();

                        item["Title"] = txtTitle.Text;
                        item["Desc"] = txtDesc.Text;
                        item["Status"] = txtStatus.Text;

                        item.Update();

                        txtTitle.Text = "";
                        txtDesc.Text = "";
                        txtStatus.Text = "";

                        LoadData();
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                using (SPSite spSite = new SPSite("http://s-e0912c86c85d4:40/"))
                {
                    using (SPWeb spWeb = spSite.OpenWeb())
                    {
                        SPList list = spWeb.Lists["Employee"];

                        SPQuery spQuery = new SPQuery();
                        spQuery.Query = "<Where><Eq><FieldRef Name='Title' />"
                            + "<Value Type='Text'>" + lstItems.SelectedItem + "</Value></Eq></Where>";

                        SPListItemCollection items = list.GetItems(spQuery);
                        SPListItem item = items[0];

                        item["Title"] = txtTitle.Text;
                        item["Desc"] = txtDesc.Text;
                        item["Status"] = txtStatus.Text;

                        item.Update();

                        LoadData();
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                using (SPSite spSite = new SPSite("http://s-e0912c86c85d4:40/"))
                {
                    using (SPWeb spWeb = spSite.OpenWeb())
                    {
                        SPList list = spWeb.Lists["Employee"];

                        SPQuery spQuery = new SPQuery();
                        spQuery.Query = "<Where><Eq><FieldRef Name='Title' />"
                    + "<Value Type='Text'>" + lstItems.SelectedItem + "</Value></Eq></Where>";

                        SPListItemCollection items = list.GetItems(spQuery);
                        SPListItem item = items[0];

                        item.Delete();

                        LoadData();
                    }
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error : " + ex.Message);
            }
        }
    }
}


1 comment :

  1. This is a really good read for me. Must admit that you are one of the best bloggers I ever saw. Thanks for posting this useful article.

    ReplyDelete