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.
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:
- Open Visual Studio. Create a new Windows Forms project.
- 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.
- Add a button that creates the new site.
- Add reference to Microsoft.Sharepoint dll. This dll is represented by the name Windows Sharepoint Services in the Add Reference dialog box.
- Add the namespace of Microsoft.Sharepoint dll like this.
- using Microsoft.Sharepoint;
- In the button click event, open the site collection.
- SPSite siteCollection = new SPSite(txtSiteCollectionUrl.Text);
- 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.
- SPWeb object has a property called ‘Webs’ that contains a collection of all the sites present under that site.
- Inorder to add a new site, use the following code.
- site.Webs.Add(txtSiteName.Text, txtSiteName.Text, Convert.ToUInt32(1033),site.WebTemplate, , false,false);
- 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;
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();
}
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SPSite siteCollection;
SPWeb site;
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();
}
}
{
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();
}
}
}
}
}
No comments:
Post a Comment