Activating Document Lib Tree navigation using CSOM

Some of you looking for how to activate treeview metadata navigation using CSOM.

Featured image

At the moment i don’t see this in the api, maybe it comes in the next CUs. But i found following working around.

The metadata navigation settings are stored in the rootfolder of the document as property bag key “client_MOSS_MetadataNavigationSettings”.

Featured image

Prerequisites: In order to activate the navigation you need  include a metadata field in the document lib and “Meteadata navigation and Filtering” feature is activated in the site.

Featured image

In your CSOM code using the following function to activate the treeview.

private static void AddMetadataNavigationToDocLib(ClientContext ctx, List docLib)
	var rootWeb = ctx.Site.RootWeb;

	ctx.Load(rootWeb, p => p.AvailableFields);
	ctx.Load(docLib, p => p.RootFolder, p => p.RootFolder.Properties);

	if (rootWeb.AvailableFields.Any(p => p.InternalName.Equals(Common.Constants.TagsFieldName)))
		var field = rootWeb.Fields.GetByInternalNameOrTitle(Common.Constants.TagsFieldName);

		ctx.Load(field, p => p.Id, p => p.InternalName, p => p.TypeAsString);

		var sb = new StringBuilder();
		sb.Append("<MetadataNavigationSettings SchemaVersion='1' IsEnabled='True' AutoIndex='True'>");
		sb.Append("<NavigationHierarchies><FolderHierarchy HideFoldersNode='False' />");
		sb.AppendFormat("<MetadataField FieldID='{0}' FieldType='{1}' CachedName='{2}' CachedDisplayName='{3}' />", field.Id, field.TypeAsString, field.InternalName, field.InternalName);
		sb.Append("</NavigationHierarchies><KeyFilters /></MetadataNavigationSettings>");

		docLib.RootFolder.Properties["client_MOSS_MetadataNavigationSettings"] = sb.ToString();
Quote | This entry was posted in SharePoint 2013 and tagged , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s