Dotnetdreamer's Weblog

SharePoint, Silverlight and Azure

Posts Tagged ‘Sharepoint Object Model’

MOSS Web Services – Accessing Sharepoint List data

Posted by Ramprasad Navaneethakrishnan on June 4, 2009

Sharepoint 2007 provides rich set of API’s that allows developers to develop custom components in Sharepoint to meet custom requirements.

SharePoint API’s can be classified into two types.

  1. Sharepoint Object Model
  2. Sharepoint Web Services

Sharepoint Object Model enables developers to customize almost anything that is in Sharepoint right from creating a Site Collection to connecting to external data sources. This object model is enclosed in 10 dll’s with 30 namespaces. A series of articles on consuming Sharepoint object model can be found here and here.

In on of my previous posts, I wrote down the steps to access SharePoint list data using SharePoint object model and CAML queries.

Now, lets see how to consume the Sharepoint Web Services in order to retrieve the Sharepoint List Data.

Step By Step

  1. Create a new windows form project.
  2. Add Web Reference to Sharepoint Lists Web Service
    1. Right-click ‘Service Reference’ and select ‘Add Service Reference’.
    2. In the Add Service Reference dialog box, click Advanced button.
    3. In the ‘Service Reference settings’ dialog box, click Add Web Reference.
    4. In the Url dropdown box, enter the url of your sharepoint site’s web service. If you are developing in the same system as the system in which MOSS is installed, then you can find the web services by clicking the link ‘Web Services in this Solution’ available in the Add Web Reference dialog box.
      1. Note : The _vti_bin directory present under the sharepoint website directory lists down all the web services available.
    5. Scroll through the list of Web Services. Click in the ‘Lists’ web service. Check the url in the url column and ensure that this Lists web service corresponds to your sharepoint site.
    6. Lists Web Service
    7. In the Web Reference name text box, give a name to the Lists web reference. Lets name it ListsWS.
    8. Click Add Reference.
    9. AddWebReference
  3. Place a button in the form. Name it btnGetListItems and the Caption can be Get List Items. Double click the button and generate the event handler.
  4. The Lists web service contains an class called Lists. Lets create an object for this Lists class.
    1. ListsWS.Lists list = new ListsWS.Lists();
  5. The List object contains a method called GetListItems(). We need to invoke this method with appropriate parameters to get the items from the list
    1. The first parameter to this method is the name of the list.
    2. The remaining parameters are there to filter the list based on user defined criteria’s. Without any criteria, to fetch all the items from the list, the method should look like the following.
      1. list.GetListItems(“MyListName”, “”, null, null, “”, null,null);
      2. The signature of GetListItems in MSDN
  6. The return type of the GetListItems() is an Xml Node.
  7. The XmlNode’s inner xml looks like the following.
  8. We need to process the xml node in order to access the list values.
    1. Below, I have processed the xml to obtain the value of the column ‘Title’ that is present in the list and displayed it in a label.
  9. The entire code is given below

ListWS.Lists lists = new ListWS.Lists();
lists.UseDefaultCredentials = true;

    XmlNode nodes = lists.GetListItems(“MyList1”, “”, null, null, “”, null, null);foreach (XmlNode node in nodes)
    {
        if (node.Name == “rs:data”)
        {
            for (int i = 0; i < node.ChildNodes.Count; i++)
            {
                if (node.ChildNodes[i].Name == “z:row”)
                {
                   lblResults.Text =  node.ChildNodes[i].Attributes[“ows_Title”].Value;
                }
            }
        }
    }

Hope this helps.

Thanks.

Posted in MOSS 2007 | Tagged: , , , , , , , , , | 22 Comments »

Creating Lists using Sharepoint Object Model

Posted by Ramprasad Navaneethakrishnan on May 26, 2009

In my previous post I wrote about creating sites using sharepoint object model. I strongly recommend to read that post before proceeding with this. It discusses about Sharepoint Site Architectures in detail.

Listed below are the Sharepoint site components and their corresponding object in the sharepoint object model.

Sharepoint Site Architecture Component Object in Sharepoint Object Model
Site Collection SPSite
Site SPWeb
List Collection SPListCollection
List SPList
List Field Collection SPFieldCollection
List Field SPField
List Item Collection SPListItemCollection
List Item SPListItem

Steps to create a new Sharepoint List:

  1. Open Visual Studio. Create a new Windows Forms project.
  2. Add a textboxes, one to accept the name of the site collection and another to accept the name of the new site to be created under the site collection.
  3. Add a button that creates the new site.
  4. Add reference to Microsoft.Sharepoint dll. This dll is represented by the name Windows Sharepoint Services in the Add Reference dialog box.
  5. Add the namespace of Microsoft.Sharepoint dll like this.
    • using Microsoft.Sharepoint;
  6. In the button click event, open the site collection.
    • SPSite siteCollection = new SPSite(txtSiteCollectionUrl.Text);
  7. Now open the top level site of the site collection.
    • SPWeb site = siteCollection.OpenWeb();
    • If you want to open a different site under the site collection other than the top level site, you can use the overloaded methods of the OpenWeb() method that accepts the name of the site as argument.
  8. The Lists property present in the SPWeb object will return all the lists that are present in that site as an SPListCollection object
    • SPListCollection listCollection = site.Lists;
  9. In order to add a new list, use the add method of the SPListCollection object.
    • listCollection.Add(“listname”, “list description”, SPListTemplateType.GenericList);
    • Note that the third argument accepts the template in which the list should be created. GenericTemplate represents the custom list. We can choose the template we need from the SPListTemplateType enum.
  10. Close and dispose and the site and site collection
    • site.dispose();
    • siteCollection.close();

The complete code can be found below..

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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

SPSite siteCollection;
SPWeb site;

private void btnCreateSite_Click(object sender, EventArgs e)
{

try
{
siteCollection = new SPSite(txtSiteCollection.Text);
site = siteCollection.OpenWeb();
SPListCollection listCollection = site.Lists;

listCollection.Add(txtListName.Text, “list description”, SPListTemplateType.GenericList);
}
catch (Exception)
{ }
finally
{
site.Dispose();
siteCollection.Close();
}
}

}
}

Hope this is useful.

Posted in MOSS 2007 | Tagged: , , , , , , , , , , | 4 Comments »

Creating Sites using Sharepoint Object Model

Posted by Ramprasad Navaneethakrishnan on May 24, 2009

MOSS 2007 provides rich set of API’s that allows developers to programatically accomplish  almost everything that can be done in sharepoint. Sharepoint Object Model comes with 10 different dll’s comprising 30 namespaces.

The aim of this post is to leverage the sharepoint object model in order to create a new sharepoint site programatically.

Before getting into that, lets look at the sharepoint site structure in the following diagram.

SiteArchitecture

Each layer in the above architecture diagram is represented by an object in the sharepoint object model. The object mapping is represented in the following table.

Sharepoint Site Architecture Component Object in Sharepoint Object Model
Site Collection SPSite
Site SPWeb
List Collection SPListCollection
List SPList
List Field Collection SPFieldCollection
List Field SPField
List Item Collection SPListItemCollection
List Item SPListItem

Steps to create a new Sharepoint Site:

  1. Open Visual Studio. Create a new Windows Forms project.
  2. Add a textboxes, one to accept the name of the site collection and another to accept the name of the new site to be created under the site collection.
  3. Add a button that creates the new site.
  4. Add reference to Microsoft.Sharepoint dll. This dll is represented by the name Windows Sharepoint Services in the Add Reference dialog box.
  5. Add the namespace of Microsoft.Sharepoint dll like this.
    • using Microsoft.Sharepoint;
  6. In the button click event, open the site collection.
    • SPSite siteCollection = new SPSite(txtSiteCollectionUrl.Text);
  7. Now open the top level site of the site collection.
    • SPWeb site = siteCollection.OpenWeb();
    • If you want to open a different site under the site collection other than the top level site, you can use the overloaded methods of the OpenWeb() method that accepts the name of the site as argument.
  8. SPWeb object has a property called ‘Webs’ that contains a collection of all the sites present under that site.
  9. Inorder to add a new site, use the following code.
    • site.Webs.Add(txtSiteName.Text, txtSiteName.Text, Convert.ToUInt32(1033),site.WebTemplate, , false,false);
  10. Close and dispose and the site and site collection
    • site.dispose();
    • siteCollection.close();

The complete code can be found in this file.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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

SPSite siteCollection;
SPWeb site;

private void btnCreateSite_Click(object sender, EventArgs e)
{

try
{
siteCollection = new SPSite(txtSiteCollection.Text);
site = siteCollection.OpenWeb();
site.Webs.Add(txtSiteName.Text, txtSiteName.Text,
txtSiteName.Text + ” Desc”, Convert.ToUInt32(1033),  site.WebTemplate,
false, false);
}
catch (Exception)
{ }
finally
{
site.Dispose();
siteCollection.Close();
}
}

}
}

Hope this is useful.

Thanks.

Posted in MOSS 2007 | Tagged: , , , , , , | 2 Comments »