Writing console app
Writing a simple console app
SPMeta2 supports SharePoint 2010, SharePoint 2013 and SharePoint Online. We use NuGet to deliver all the packages, handle versioning, updates and patches.
This article introduces a NuGet packages description, consuming NuGet packages in your applications and usage of github repository if you like to built SPMeta2 library from scratch.
Referencing SPMeta2 from NuGet
The easiest way to start with SPMeta library is to use NuGet packages. We support a range of SharePoint versions - 2010, 2013 and Online as well as CSOM/SSOM, so we have a bunch of the NuGet packages out there.
The core library
- SPMeta2.Core - contains the core of the library and definitions for SharePoint Foundation
CSOM provision
CSOM provision (built with SharePoint 2013 onprem v15 assemblies):
- SPMeta2.CSOM.Foundation - includes CSOM provision implementation for SharePoint Foundation 2013
- SPMeta2.CSOM.Standard - includes CSOM provision implementation for SharePoint Standard 2013
CSOM provision for SharePoint Online (built with SharePoint Online v16 assemblies):
- SPMeta2.CSOM.Foundation-v16/ - includes CSOM provision implementation for SharePoint Online
- SPMeta2.CSOM.Standard-v16 - includes CSOM provision implementation for SharePoint Online
SSOM provision
SSOM provision for SharePoint 2013 (built with SharePoint 2013 assemblies):
- SPMeta2.SSOM.Foundation - includes SSOM provision implementation for SharePoint Foundation 2013
- SPMeta2.SSOM.Standard - includes SSOM provision implementation for SharePoint Standard 2013
SSOM provision for SharePoint 2010 (built with SharePoint 2010 assemblies):
- SPMeta2.SSOM.Foundation-v14 - includes SSOM provision implementation for SharePoint Foundation 2010
- SPMeta2.SSOM.Standard-v14 - includes SSOM provision implementation for SharePoint Standard 2010
Building SPMeta2 from the source code
The other options you might consider is to use SPMeta2 github repository and built the library from scratch. Although it is not a recommended options, but still possible one.
Here are a few links to get started:
One you have SPMeta2 solutions up and running, you can either build it with Visual Studio 2013 or use "build.ps1" script located in /SPMeta2/SPMeta2.Build folder.
Creating a simple CSOM application for SharePoint 2013
Let's get started and create a simple console application which provisions a few fields and a content type on a target SharePoint site.
As we target SharePoint 2013, we would need to use SPMeta2.CSOM.Foundation package. Here are detailed steps to get startes:
Step 1, bootstrap a console application project
Start your Visual Studio and bootstrap a simple console application:
SPMeta2.CSOM.Foundation package.
Step 2, use NuGet to getSimple use NuGet package manager to find and install SPMeta2.CSOM.Foundation package or use the following command in the "Package Manager Console":
install-package SPMeta2.CSOM.Foundation
Step 3, setup your definitions, model and provision service
There are a few outtanding things we need to complete - create definitions, setup relationships between them and, finally, push the model to the SharePoint site.
Include the following code in your console application changing the 'siteUrl', run it and enjoy the outcome.
var siteUrl = "http://tesla-dev:31415/";
// define fields
var clientDescriptionField = new FieldDefinition
{
Title = "Client Description",
InternalName = "dcs_ClientDescription",
Group = "SPMeta2.Samples",
Id = new Guid("06975b67-01f5-47d7-9e2e-2702dfb8c217"),
FieldType = BuiltInFieldTypes.Note,
};
var clientNumberField = new FieldDefinition
{
Title = "Client Number",
InternalName = "dcs_ClientNumber",
Group = "SPMeta2.Samples",
Id = new Guid("22264486-7561-45ec-a6bc-591ba243693b"),
FieldType = BuiltInFieldTypes.Number,
};
// define content type
var customerAccountContentType = new ContentTypeDefinition
{
Name = "Customer Account",
Id = new Guid("ddc46a66-19a0-460b-a723-c84d7f60a342"),
ParentContentTypeId = BuiltInContentTypeId.Item,
Group = "SPMeta2.Samples",
};
// define relationships and the model
var siteModel = SPMeta2Model.NewSiteModel(site =>
{
site
.AddField(clientDescriptionField)
.AddField(clientNumberField)
.AddContentType(customerAccountContentType, contentType =>
{
contentType
.AddContentTypeFieldLink(clientDescriptionField)
.AddContentTypeFieldLink(clientNumberField);
});
});
var clientContext = new ClientContext(siteUrl);
// deploy the model to the SharePoint site over CSOM
var csomProvisionService = new CSOMProvisionService();
csomProvisionService.DeploySiteModel(clientContext, siteModel);
Creating a simple SSOM application for SharePoint 2013
Essentially, we would need to bootstrap the same console application (check previous CSOM application) and add SPMeta2.SSOM.Foundation package or use the following command in the "Package Manager Console":
install-package SPMeta2.SSOM.Foundation
The actual code will be the same, but to enable SSOM based provision we need to use 'CSOMProvisionService'. Include the following code in your console application changing the 'siteUrl', run it and enjoy the outcome.
var siteUrl = "http://tesla-dev:31415/";
// define fields
var clientDescriptionField = new FieldDefinition
{
Title = "Client Description",
InternalName = "dcs_ClientDescription",
Group = "SPMeta2.Samples",
Id = new Guid("06975b67-01f5-47d7-9e2e-2702dfb8c217"),
FieldType = BuiltInFieldTypes.Note,
};
var clientNumberField = new FieldDefinition
{
Title = "Client Number",
InternalName = "dcs_ClientNumber",
Group = "SPMeta2.Samples",
Id = new Guid("22264486-7561-45ec-a6bc-591ba243693b"),
FieldType = BuiltInFieldTypes.Number,
};
// define content type
var customerAccountContentType = new ContentTypeDefinition
{
Name = "Customer Account",
Id = new Guid("ddc46a66-19a0-460b-a723-c84d7f60a342"),
ParentContentTypeId = BuiltInContentTypeId.Item,
Group = "SPMeta2.Samples",
};
// define relationships and the model
var siteModel = SPMeta2Model.NewSiteModel(site =>
{
site
.AddField(clientDescriptionField)
.AddField(clientNumberField)
.AddContentType(customerAccountContentType, contentType =>
{
contentType
.AddContentTypeFieldLink(clientDescriptionField)
.AddContentTypeFieldLink(clientNumberField);
});
});
using (var spSite = new SPSite(siteUrl))
{
// deploy the model to the SharePoint site over CSOM
var ssomProvisionService = new SSOMProvisionService();
ssomProvisionService.DeploySiteModel(spSite, siteModel);
}