I am currently trying to write a Dynamics CRM 2011 plugin that creates a new SharePoint 2010 site (within an existing site collection) for every account that is created. I have the code for creating a library, which uses the following methods:
private static ClientContext CreateSharePointConnection()
{
try
{
Uri spSiteUrl = new Uri(Constants.SharePointSiteName);
using (ClientContext spcontext = new ClientContext(spSiteUrl))
{
spcontext.Credentials
= new System.Net.NetworkCredential(Constants.UserName
, Constants.Password, Constants.DomainName);
return spcontext;
}
}
catch (Exception ex)
{
throw new Exception("SharePointMethods.CreateSharePointConnection (General Exception: "
+ ex.Message + ")");
}
}
private static void CreateDocumentLibrary(ClientContext _clientContext
, string _documentLibraryName, string _documentLibraryDescription)
{
try
{
Web web = _clientContext.Web;
_clientContext.Load(web);
_clientContext.ExecuteQuery();
ListCreationInformation lci = new ListCreationInformation();
lci.Title = _documentLibraryName;
lci.Description = _documentLibraryDescription;
lci.TemplateType = 101;
List newDocumentLibrary = web.Lists.Add(lci);
newDocumentLibrary.ContentTypesEnabled = true;
newDocumentLibrary.Update();
_clientContext.ExecuteQuery();
}
catch (Exception ex)
{
throw new Exception("SharePointMethods.CreateDocumentLibrary('" + _documentLibraryName
+ "') (General Exception: " + ex.Message + ")");
}
}
So I would like to write a method CreateSite instead of CreateDocumentLibrary. However, all examples I can find on the web for programmatically creating a Site are using SPSite, SPWeb, etc. Something like this:
private static void CreateSite(string _siteName, string _siteDescription)
{
try
{
SPSite site = new SPSite(Constants.SharePointSiteName);
SPWeb web = site.OpenWeb();
SPWebCollection wc = web.Webs;
string siteTemplate = web.WebTemplate;
wc.Add(Constants.SharePointSiteName, _siteName, _siteDescription, 1043, siteTemplate, true, false);
}
catch (Exception ex)
{
throw new Exception("SharePointMethods.CreateSite('" + _siteName
+ "') (General Exception: " + ex.Message + ")");
}
}
However this is NOT a solution for my problem, because the SPSite, SPWeb, etc. only work on the local machine and I want to create a Site from a remote (in my case CRM) server. Anyone got an idea how to solve this.
PS: Code might still contain memory leaks, so please be carful when you use it for production purposes!