Online Training On SharePoint
                      

Monday, 14 April 2008

Common tasks done with SharePoint Object Model

1. Fetching all the servers in the Farm (We have to add one more namespace for this piece:
Using Microsoft.SharePoint.Administration):
SPSite _site = new SPSite("http://servername:Portnumber/");
foreach (SPServer _server in _site.WebApplication.Farm.Servers)
{
MessageBox.Show(_server.DisplayName.ToString());
}

2.Fetching all the Site Collection in the Web Application.
SPSite _site = new SPSite("http://servername:Portnumber/");
SPWebApplication _webapp = _site.WebApplication;
SPSiteCollection _sitecollection = _webapp.Sites;

foreach (SPSite _IndividualsiteCollection in _sitecollection)
{
MessageBox.Show(_IndividualsiteCollection.Url.ToString());
}

3.Fetching all the sites in a Site Collection.
SPSite _site = new SPSite("http://servername:Portnumber/");
foreach (SPWeb _web in _site.AllWebs)
{
MessageBox.Show(_web.Url.ToString());
}

4.Fetching all the lists in a Site.
SPSite _site = new SPSite("http://servername:Portnumber/");
foreach (SPList _list in _site.OpenWeb().Lists)
{
MessageBox.Show(_list.Title.ToString());
}

5.Fetching all the items in a list.
SPSite _site = new SPSite("http://servername:Portnumber/");
SPListCollection _listCollection = _site.OpenWeb().Lists;
foreach (SPListItem _item in _listCollection[10].Items)
{
MessageBox.Show(_item.Name.ToString());
}

You may also find interesting to read: Important Classes with SharePoint Object Model & Object Model Hierarchy

Post a Comment
Related Posts with Thumbnails