Friday, 22 August 2008

Site Creation on Sharepoint 2007

Site Creation on Sharepoint 2007

The only way to create a site remotely is developing your own web service on the main Sharepoint 2007 server, just because you will need to use Microsoft.SharePoint(Microsoft.SharePoint.dll) and Microsoft.SharePoint.Search(Microsoft.SharePoint.Search.dll).

What I have developed is a plain class. This class will be the one in charge of doing the job by creating the site.

I attach the code of the class so after that you can call this class from your own web service:

-------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SiteManager
{
public class SiteManager
{
public enum LocalID { EnglishUK = 2057, EnglishUS = 1033, SpanishTraditional = 1034, SpanishModern = 1034, French = 1036, Italian = 1040, GermanGermany = 1031 };

public static bool CreateSite(string _sParentSiteURL, string _sSiteURLRequested, string _sSiteTitle, int _iSiteTemplateNumber, LocalID LCID)
{
bool _bResult = false;

try
{
using (SPSite siteCollection = new SPSite(_sParentSiteURL))
{
SPWeb parentWeb = siteCollection.OpenWeb();

SPWebTemplateCollection Templates = siteCollection.GetWebTemplates(Convert.ToUInt32(LCID));
SPWebTemplate _sptSiteTemplate = Templates[_iSiteTemplateNumber];
if (parentWeb.Webs[_sSiteURLRequested].Exists)
{
parentWeb.Webs.Delete(_sSiteURLRequested);
}

parentWeb.Webs.Add(_sSiteURLRequested, _sSiteTitle, "", Convert.ToUInt32(LCID), _sptSiteTemplate, false, false);

_bResult = true;
}

}
catch (Exception ex)
{
_bResult = false;
}

return _bResult;
}
}

}
-------------------------------------------------------------------------------------------


In order to call this class you will need to create your own web service, and it should looks like something like this:


-------------------------------------------------------------------------------------------
using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

[WebService(Namespace = "http://sheep/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () {

//Uncomment the following line if using designed components
//InitializeComponent();
}

///
/// Description.
///
/// Description
[WebMethod]
public string WebServiceDescription() {
return "Site Mangnager v1.0";
}

[WebMethod]
public bool CreateNewSite(string _sParentSiteURL, string _sSiteURLRequested, string _sSiteTitle, int _iSiteTemplateNumber, SiteManager.SiteManager.LocalID LCID)
{
bool _bResult = false;

try
{
_bResult = SiteManager.SiteManager.CreateSite(_sParentSiteURL, _sSiteURLRequested, _sSiteTitle, _iSiteTemplateNumber, LCID);
}
catch (Exception ex)
{
_bResult = false;
}

return _bResult;
}

}
-------------------------------------------------------------------------------------------

The web service should look like this:

No comments: