SPMeta2

When using SPMeta 2, the first starting point is to read the two links below to find out more about what is SPMeta2:

Then the next step is to Learn the basics

Finally lets get cracking on how to implement and use SPMeta2. You can follow this link  Get started from scratch or alternately follow my instructions below.

Step 1: Installing NuGet

The pre-requisite is to ensure that you have NuGet installed for your Visual Studio (VS). NuGet is available for the following clients:

  1. Follow my blog here on how to install NuGet in VS.

 

Step 2: Setting up SPMeta2

  1. Go to SubPointSolutions homepage on Github and download the latest SPMeat2. NOTE: Check the release note first to ensure that you are downloading the latest stable version and NOT the beta (still have bugs) version.
  2.  Extract the zip file.
  3.  One you have SPMeta2 solutions open (SPMeta2.sln) in VS 2013, you can either build it with Visual Studio 2013 or use “build.ps1″ script located in /SPMeta2/SPMeta2.Build folder. NOTE: Trying to open the solution in previous version of VS lower than 2013 will fail in opening a project such as SPMeta2\SPMeta2.Containers.O365\SPMeta2.Containers.AppContainer\SPMeta2.Containers.AppContainer.csproj

 

Step 3: Create a CSOM Solution for SharePoint 2013

  1. Create a new Console Application in VS 2013
  2. In Console App, right click “References”->Manage NuGet Packages
  3. Search and install SPMeta2.CSOM.Foundation package
  4. Search and install SPMeta2.SSOM.Foundation package
  5. Add reference Microsoft.SharePoint.Client (v15)
  6. Add reference Microsoft.SharePoint.Client.Runtime (v15)

 

Step 4: 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 CSOM code below in your console application changing the ‘siteUrl’, run it and enjoy the outcome.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

using Microsoft.SharePoint.Client;

using SPMeta2.Models;

using SPMeta2.Syntax.Default;

using SPMeta2.CSOM.Services;

using SPMeta2.Definitions;

using SPMeta2.Enumerations;

static void Main(string[] args)
        {
            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);
        }

Alternatively if you want to instead deploy using SSOM code, then all the code is still the same as bove, but the only difference is that you need to add the reference, replace CSOM code with SSOM code below

using SPMeta2.SSOM.Services;

using Microsoft.SharePoint;

using (var spSite = new SPSite(siteUrl))

{

// deploy the model to the SharePoint site over CSOM

var ssomProvisionService = new SSOMProvisionService();

ssomProvisionService.DeploySiteModel(spSite, siteModel);

}

See other sample scenarios of SPMeta2 in action.


Have a Closer Look

 

Others

Advertisements

One thought on “SPMeta2

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s