PnP Core Component - Activate Or Deactivate Feature In SharePoint 2016

Please refer to the Introduction to PnP Core Component and OfficeDevPnP.Core for more details. I have created a Console Application and added SharePointPnPCore2016 NuGet package for the SharePoint 2016 version.
 
Code Snippet
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using Microsoft.SharePoint.Client;  
  7. using OfficeDevPnP.Core;  
  8. using OfficeDevPnP.Core.Extensions;  
  9. using Microsoft.SharePoint.Client.Taxonomy;  
  10.   
  11. namespace SP2016PnPCoreComponentDemo  
  12. {  
  13.     class Program  
  14.     {  
  15.         static void Main(string[] args)  
  16.         {  
  17.             // Input Parameters    
  18.             string siteUrl = "http://c7395723754/";  
  19.             string userName = "administrator";  
  20.             string password = "xxxxxxxx";  
  21.             string domain = "AD2012";  
  22.             Guid featureID = new Guid("961d6a9c-4388-4cf2-9733-38ee8c89afd4");  
  23.   
  24.             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();  
  25.   
  26.             try  
  27.             {  
  28.                 // Get the client context    
  29.                 using (var ctx = authMgr.GetNetworkCredentialAuthenticatedContext(siteUrl, userName, password, domain))  
  30.                 {  
  31.                     // Check if faeture is activated in web  
  32.                     if (ctx.Web.IsFeatureActive(featureID))  
  33.                     {  
  34.                         // Deactivate the feature in web  
  35.                         ctx.Web.DeactivateFeature(featureID);  
  36.                     }  
  37.                     else  
  38.                     {  
  39.                         // Activate the feature in web  
  40.                         ctx.Web.ActivateFeature(featureID);  
  41.                     }  
  42.                 }  
  43.             }  
  44.   
  45.             catch (Exception ex)  
  46.             {  
  47.                 Console.WriteLine("Error Message: " + ex.Message);  
  48.             }  
  49.         }  
  50.     }  
Ebook Download
View all
Learn
View all