How to do refresh all published content types on next update in Content Type Hub in SharePoint Office 365 Programmatically (CSOM)

Sathish Nadarajan
 
Solution Architect
August 28, 2016
 
Rate this article
 
Views
7669

How to do refresh all published content types on next update in Content Type Hub in SharePoint Office 365 Programmatically (CSOM)

In the last article, we saw about the Content Type Hub elaborately. Now, let us see how to make it programmatically.

As a first step, we need to Refresh all Published Content Types in Consumer Site collection.

 

image

 

To do that, in Office 365, we need to use Client Side Object Model (CSOM). It is very straight forward and updating a PropertyBag Value will do the magic for us. The Straight forward code is as below.

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 
 using Microsoft.SharePoint.Client;
 using System.Security;
 using Microsoft.Online.SharePoint.TenantAdministration;
 
 namespace Console.Office365
 {
     class Program
     {
         static void Main(string[] args)
         {
             RefreshAllPublishedContentTypes();
         }
 
 
         public static void RefreshAllPublishedContentTypes()
         {
 
             OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
 
             string siteUrl = "https://********.sharepoint.com/sites/DeveloperSite";
             string userName = "sathish@***********.onmicrosoft.com";
             string password = "**********";
 
             using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(siteUrl, userName, password))
             {
                 Web web = ctx.Web;
                 ctx.Load(web);
 
                 bool b = web.PropertyBagContainsKey("metadatatimestamp");
                 if (b)
                 {
                     web.AllProperties["metadatatimestamp"] = string.Empty;
                     web.Update();
                     ctx.Load(web);
                     ctx.ExecuteQueryRetry();
                 }
             }
         }
     }
 }
 

A small piece of code will automate the entire site collections which consume the Content Type Hub.

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