Setting up Visual Studio for developing SharePoint online site provisioning software
We’re in the process of setting up SharePoint Online as our Intranet platform, and are looking to automate site creation using something like Azure Functions. Currently, I’m developing the business logic for creating the new sites, including setting the correct permissions, theme, links and so forth.
I’m using Visual Studio for this project. I’m new to both SharePoint Online as .NET, and as tutorials on setting up Visual Studio with regards to SharePoint Online provisioning seems to be lacking, I though I’d briefly outline the steps needed to get the basics up and running.
First, create a new Visual Studio project:

Next, install the NuGet package “SharePointPnPCoreOnline”:

You should now have Visual Studio up and running, and if you wish you can take a look at the following code snippets to get started provisioning new sites:
Content of “Program.cs”:
using System;
using System.Threading.Tasks;
using OfficeDevPnP.Core.Sites;
namespace SharePointSiteProvisioning
{
struct NewSiteConfig
{
public String name;
public String title;
public String description;
public OfficeDevPnP.Core.Sites.CommunicationSiteDesign siteDesign;
public String url;
public String ownerEmailAddress;
}
class Program
{
static async Task Main(string[] args)
{
Provisioner provisioner = new Provisioner();
string siteUrl = "https://mytenant.sharepoint.com";
NewSiteConfig siteConfig = new NewSiteConfig();
siteConfig.name = "My new site name";
siteConfig.title = "My site title";
siteConfig.description = "My description";
siteConfig.siteDesign = CommunicationSiteDesign.Blank;
siteConfig.url = siteUrl + "/sites/newsite";
siteConfig.ownerEmailAddress = "me@example.com";
await provisioner.provisionSite(siteUrl: siteUrl, siteConfig: siteConfig);
}
}
}
Content of “Provisioner.cs”:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// Project specific dependencies
using System.Configuration;
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using OfficeDevPnP.Core.Sites;
namespace SharePointSiteProvisioning
{
class Provisioner
{
String azureClientId;
String azureClientSecret;
public Provisioner(String azureClientId, String azureClientSecret)
{
this.azureClientId = azureClientId;
this.azureClientSecret = azureClientSecret;
}
public async Task provisionSite(String siteUrl, NewSiteConfig siteConfig)
{
using (var cc = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, this.azureClientId, this.azureClientSecret))
{
cc.Load(cc.Web, p => p.Title);
cc.ExecuteQuery();
Console.WriteLine(cc.Web.Title);
await this.createSite(context: cc, templateUrl: templateUrl, siteConfig: siteConfig);
};
}
private async Task createSite(ClientContext context, String templateUrl, NewSiteConfig siteConfig)
{
CommunicationSiteCollectionCreationInformation communicationSiteInfo = new CommunicationSiteCollectionCreationInformation
{
Title = siteConfig.title,
Url = siteConfig.url,
SiteDesign = siteConfig.siteDesign,
Description = siteConfig.description,
Owner = siteConfig.ownerEmailAddress
};
try
{
await context.CreateSiteAsync(communicationSiteInfo);
Console.WriteLine($"Successfully created the page {siteConfig.url}.");
}
catch (Exception e)
{
Console.WriteLine($"Something went wrong: {e.ToString()}");
}
}
private static void pressEnterToContinue()
{
Console.WriteLine("Press --ENTER-- to continue");
Console.ReadKey(true);
}
}
}