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


Sathish Nadarajan
SharePoint MVP
Published On :   28 Aug 2016
Visit Count
Today :  1    Total :   2811
Plan, Migrate, Secure, Report
SharePoint & Office 365 Tool. Simple & Easy to Use. 15-Day Trial!

Sharegate: Kick-Ass Tool
Think Your SharePoint & Office 365 Are Secure ? Find Out Now!


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.

SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Categories

Migratiin Tools for SharePoint