Freigeben über


Simple application to modify wiki list field title

This post is based on a requirement that came up couple of weeks back. The objective was to modify the default fields title to something different for a wiki page as per the business needs. Interestingly, we had multiple fields to be modified for the wiki list in this scenario and I thought of achieving this through a simple console application which accept some user inputs.

Here is the code(C#) which will accept the wiki site URL, old field title and the new field title for the wiki list. Once the code is executed, the wiki list field title will be modified to the new field title you entered for the specific wiki site.

 

 using System;
 using System.Collections.Generic;
 using Microsoft.SharePoint;
  
 namespace Wiki
 {
     class Wiki
     {
         static void Main(string[] args)
         {
             Console.WriteLine("Key in your Wiki Site URL and Press Enter:");
             String URL = Console.ReadLine();
  
             Console.WriteLine("Key in the old field name which you want to modify:");
             String oldFieldTitle = Console.ReadLine().ToLowerInvariant();
  
             Console.WriteLine("Key in the new field name:");
             String newFieldTitle = Console.ReadLine();
  
             using (SPSite site = new SPSite(URL))
             {
                 using (SPWeb web = site.OpenWeb())
                 {
                     SPList myWikiList = web.Lists["Wiki Pages"];
                     SPFieldCollection myWikiFields = myWikiList.Fields;
  
                     foreach (SPField field in myWikiFields)
                     {
                         if (field.Title.ToLowerInvariant().Equals(oldFieldTitle))
                         {
                             field.Title = newFieldTitle;
                             field.Update();
                             Console.WriteLine("Updated the field title to" + newFieldTitle);
                             break;
                         }
                        
                     }
                 }
             }
  
         }
     }
 }

 

You can create a new console application and just copy the above code in the main class file. Execute the application on the server where you have MOSS installed. 

Hope that helps!

Sojesh

Comments