SharePoint Office 365 – Terms Provisioning using PNP Template and C#

Sathish Nadarajan
 
Solution Architect
May 14, 2019
 
Rate this article
 
Views
1596

In this article, let us provision the TermSet and Terms using PNP Provisioning Template by using C#.  The Provisioning Template is as below.

 <?xml version="1.0"?>
 <pnp:Provisioning xmlns:pnp="http://schemas.dev.office.com/PnP/2018/01/ProvisioningSchema">
   <pnp:Preferences Generator="OfficeDevPnP.Core, Version=4.0.30319, Culture=neutral, PublicKeyToken=5e633289e95c321a" />
   <pnp:Templates>
     <pnp:ProvisioningTemplate ID="SPPALS.ProvisioningTemplate.TermGroups" Version="1" Scope="RootSite">
       <pnp:TermGroups>
         <pnp:TermGroup Name="{sitecollectiontermgroupname}">
           <pnp:TermSets>
             <pnp:TermSet Name="TermSet1" Language="1033">
               <pnp:Terms>
                 <pnp:Term Name="Term1"   IsAvailableForTagging="true" IsSourceTerm="false"></pnp:Term>
                 <pnp:Term Name="Term2"  IsAvailableForTagging="true" IsSourceTerm="false"></pnp:Term>
                 <pnp:Term Name="Term3"   IsAvailableForTagging="true" IsSourceTerm="false"></pnp:Term>
               </pnp:Terms>
             </pnp:TermSet>
           </pnp:TermSets>
         </pnp:TermGroup>
       </pnp:TermGroups>
     </pnp:ProvisioningTemplate>
   </pnp:Templates>
 </pnp:Provisioning>
 

And the C# code is straight forward.  It is as below.

 static void Main(string[] args)
         {
             ProvisionTaxonomyPNP();
         }
 
         public static void ProvisionTaxonomyPNP()
         {
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://sppalsmvp.sharepoint.com/sites/TeamSite/";
             string userName = "sathish@sppals.com";
             string password = "*****";
 
             using (var clientContext = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = clientContext.Web;
                 clientContext.Load(web);
                 clientContext.Load(web.Lists);
                 clientContext.ExecuteQuery();
 
                 var templateLocation = "D:\PRACTICE SOURCE CODE\SPPALS.Console\SPPALS.Console\ProvisioningTemplate";
                 var provisioningProvider = new XMLFileSystemTemplateProvider(templateLocation, string.Empty);
 
                 var organizationSiteProvisioningTemplate = provisioningProvider.GetTemplate("SPPals.ProvisioningTemplate.TermGroups.xml");
                 organizationSiteProvisioningTemplate.Connector.Parameters[FileConnectorBase.CONNECTIONSTRING] = templateLocation;
                 clientContext.Web.ApplyProvisioningTemplate(organizationSiteProvisioningTemplate);
             }
         }
 

 

The terms will be provisioned as below.

 

 

Happy Coding,

Sathish Nadarajan.

 

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment