Click here to Skip to main content
6,294,871 members and growing! (16,778 online)
Email Password   helpLost your password?
Enterprise Systems » SharePoint Server » General     Beginner License: The Code Project Open License (CPOL)

Accessing SharePoint Object Model - A beginner Overview

By Arif Habib Shadan - www.i-arif.com

Accessing SharePoint Object Model - A beginner Overview
C# (C# 2.0), ASP.NET
Posted:12 Jul 2008
Views:11,768
Bookmarked:12 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
5 votes for this article.
Popularity: 1.68 Rating: 2.40 out of 5
2 votes, 40.0%
1
1 vote, 20.0%
2
1 vote, 20.0%
3

4
1 vote, 20.0%
5

Introduction

SharePoint provides a solid framework for the .Net developers to write code against and extend sharepoint functionality. As sharepoint is done on ASP.NET 2.0 and have a power code base of .Net Class libraries, a lot of developers can now make use of them and create excellent applications utilizing sharepoint features and libraries. In this tutorial i will explain on how to open sharepoint site using Visual Studio and then connect with a document library inside sharepoint and get the creation date of the document library. This feature is not given in the sharepoint out of the box so we need to do custom development to find out this information.

Using the code

Lets get directly to the code. The first thing we need to do is to setup our development environment. This is a bit tricky. We can do couple of things.

  • Setup of development tools where sharepoint portal is installed
  • Create virtual pc of sharepoint and install sharepoint on that
  • copy sharepoint dlls to your development pc and reference those dlls in the visual studio but keep in mind that debugging will be very difficult

OK now in my scenario i used a virtual pc and sharepoint VHD boot it up and opened the visual studio. I referenced Microsoft.SharePoint dll and referenced it inside the code behind as well.

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

I dropped a couple of drop down lists on the form and a text box where the user will give its portal name. Once typing portal address the user will click connect. This event will populate the drop down with all the sites created on the portal.

private void cmdConnect_Click(object sender, EventArgs e)
        {
            cboLists.Items.Clear();
            SPSite mysite = new SPSite(txtURL.Text.Trim());
            foreach (SPWeb myweb in mysite.AllWebs)
            {
                cboSites.Items.Add(myweb.Title);
            }
        }

As you can see from above, i have used SPSite class to open a site and passed the url of the site as constructor. Once opened this site class has a web collection class. I looped through all the webs created and listed them in the drop down.

Now the user will select the site where the document library exists. On the selected index changed, i have populated next drop down with all the lists and libraries.

        private void cboSites_SelectedIndexChanged(object sender, EventArgs e)
        {
            mysite2 = new SPSite(txtURL.Text.Trim());
            myweb2 = mysite2.AllWebs[cboSites.SelectedItem.ToString()];
            foreach (SPList lst in myweb2.Lists)
            {
                cboLists.Items.Add(lst.Title);
            }           
        }

What happened just now is the most intresting part. Here we opened the site, got the web from the user selection of the drop down and put it in SPWeb class. SPWeb class now contains all the document libraries and lists you have in your site. As you can see we are looping through the list collection in the spweb.We are adding all the lists and libraries in the drop down as we loop.

Now once the user selects the library, he will see the creation date of the library in the label control.

private void cboLists_SelectedIndexChanged(object sender, EventArgs e)
        {
            SPList mylist = myweb2.Lists[cboLists.SelectedItem.ToString()];
            DateTime datecreated = mylist.Created;
            lblDate.Text = datecreated.ToShortDateString();
        }

As can be seen, we got the list in SPList class and utilized its created property to find out the list creation date.

Points of Interest

The main point of interest part here is to see how to connect to a site, open its web and iterate through its list and libraries. Then to see how to get the properties of the lists and libraries. SPSite, SPWeb and SPList are the major classes we used to achieve our goal. In my next writing i wil explain how to go beyond and access individual list items.

Hence the object models looks like

SPSite

SPWeb

SPList

SPListItem

Ok now i will take a leave :-)

Arif Habib Shadan

History

No old verision of the document

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Arif Habib Shadan - www.i-arif.com


Member
Working as solution architect. Developing solutions based on Microsoft .Net based Technologies.
Have my own blog at
www.i-arif.com
Occupation: Architect
Company: www.i-arif.com
Location: Pakistan Pakistan

Other popular SharePoint Server articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralMy vote of 1 PinmemberManu Agrawal7:27 8 Jan '09  
GeneralCustomizing UI of Announcements webpart. PinmemberSyed Mujtaba Hassan3:30 22 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Jul 2008
Editor:
Copyright 2008 by Arif Habib Shadan - www.i-arif.com
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project