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