SharePoint Champion Tutorial – Assigning Meta Folder Defaults in SharePoint To Make Tagging and Searching Easier

In my last SharePoint Champion Tutorial: How to Assign Metadata Values Based On A Folder To Make Tagging & Searching Easier, I spoke about how you can ensure that SharePoint documents are automatically tagged properly with default meta. If you haven’t already reviewed that post, I highly recommend doing so before proceeding with this one. Click here to read.

Building on that concept, many might be asking, “Is there a way to automate this configuration setting because there are a lot of folders in a SharePoint library?”  The answer is YES and this tutorial will walk you through how to do it.

This method is more advanced than the last so if you need any help, please contact the SharePoint team at CRGroup sharepoint@crgroup.com and we can certainly help

1 – Open Visual Studio and create a new Console Application

Open Visual Studio in SharePoint

2 – Add references

Add references to Microsoft.SharePoint.Client, Microsoft.SharePoint.Client.DocumentManagement and Microsoft.SharePoint.Client.Runtime

Use version 16.0..0.0 for SharePoint Online.

Add References in SharePoint

3 – Define the parameters

string siteUrl = “enter your site url”;
string username = “SharePoint Online account with permission to set the default locations on the document library”;

string notSecurePassword = “password for SharePoint Online account”;

SecureString password = new SecureString();

foreach (char c in notSecurePassword.ToCharArray()) password.AppendChar(c);

string listName = “SharePoint library name”;

string fieldname = “Name of the SharePoint column that will store the default metadata value.”;

4 – Create the context

ClientContext context = new ClientContext(siteUrl);
context.Credentials = new SharePointOnlineCredentials(username, password);

 

5 – Load the library

List list = context.Web.Lists.GetByTitle(listName);

We only need the server relative URL of the root folder.

context.Load(list, l => l.RootFolder.ServerRelativeUrl);

context.ExecuteQuery();

 

6 – Load the root folder and root sub-folders

Folder folder = context.Web.GetFolderByServerRelativeUrl(list.RootFolder.ServerRelativeUrl);

We only need the names of the sub-folders.

context.Load(folder, f => f.Folders.Include(sf => sf.Name)); context.ExecuteQuery();

 

7 – Create the metadata object and set the default value

MetadataDefaults metadata = new MetadataDefaults(context, list);

foreach (Folder subFolder in folder.Folders)

{

metadata.SetFieldDefault(subFolder, fieldName, subFolder.Name);

metadata.Update();

}

context.ExecuteQuery();

 

8 – Update Default Location Metadata method

So we have the following method which we can call from Main.

Update default location Metadata in SharePoint

Note: This method will only update the top-level folders.

 

9 – After running this method, we go to the Column Default Value page

All the folders have a green star next to them which indicates that the default value has been set.

SharePoint files

And voila! You just made it incredibly easy to assign tags and metadata to SharePoint folders for easier tagging and searching.


About the Author:
Elizabeth Brown

 

Elizabeth Brown SharePoint Consultant

Elizabeth Brown is one of CRGroup’s senior SharePoint Specialists and works with clients to setup, configure, upgrade, and customize their SharePoint systems. Elizabeth has over 8 years of experience working in IT, focusing on system analysis and application development and support.  In these roles, she worked directly with end users to develop and customize applications to meet long term needs. In addition to SharePoint and Dynamics CRM, she has extensive experience with web based technologies and SQL databases.

 

Get More From SharePoint!

CRGroup’s team of SharePoint business and technical consultants is here to help setup, configure, integrate and support your SharePoint systems. If you need help with customizations, troubleshooting, or training, contact us for a free SharePoint consultation. sharepoint@crgroup.com