Here we will discuss how we can activate publishing feature in SharePoint Online using .Net managed client object model code. Here we are going to write our code in a windows application. So first create a windows application and then add the below ddls in the reference.
- Microsoft.SharePoint.Client
- Microsoft.SharePoint.Client.Runtime
Also you can read:
Here is the full code:
To activate publishing feature we need to activate below two features:
- SharePoint Server Publishing Infrastructure (Site collection feature)
- SharePoint Server Publishing (Site feature)
private void button4_Click(object sender, EventArgs e)
{
using (ClientContext context = new ClientContext("https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"))
{
string username = " **********@onlysharepoint2013.onmicrosoft.com";
context.AuthenticationMode = ClientAuthenticationMode.Default;
var secureString = new SecureString();
foreach (char c in " **********")
{
secureString.AppendChar(c);
}
context.Credentials = new SharePointOnlineCredentials(username, secureString);
try
{
ActivateFeature(context);
}
catch (Exception ex)
{
throw;
}
}
}
void ActivateFeature(ClientContext context)
{
Guid SiteFeatureID = Guid.Parse("f6924d36-2fa8-4f0b-b16d-06b7250180fa");
Guid WebFeatureID = Guid.Parse("94c94ca6-b32f-4da9-a9e3-1f3d343d7ecb");
try
{
var site = context.Site;
context.Load(site);
var Web = context.Web;
context.Load(Web);
var siteFeatures =
context.Site.Features;
var webFeatures =
context.Web.Features;
context.Load(siteFeatures);
context.Load(webFeatures);
context.ExecuteQuery();
siteFeatures.Add(SiteFeatureID, true, FeatureDefinitionScope.None);
webFeatures.Add(WebFeatureID, true, FeatureDefinitionScope.None);
context.ExecuteQuery();
}
catch (Exception ex)
{
}
}
Once you run the code, it will activate the publishing feature in SharePoint Online. Hope this will be helpful.