Thursday 3 October 2013

Remove Workflow Column from SharePoint List View

When you attach a workflow to a SharePoint list, the workflow column will be added automatically to the default view of the list. This column shows the status of the workflow and when you click on the status, you will be redirected to the Workflow Status page. Usually, this information is of no practical use for end users therefore you don't want this column to appear in the list view. So in order to achieve it, you need to write a custom code that will remove the workflow column programmatically.


Below is the sample code of a console application that removes workflow column from the default view of the SharePoint list:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace RemoveWorkFlowColumnFromSPView
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length != 4)
            {
                Console.WriteLine("Please input parameters.");
                Console.WriteLine("Format: <site url> <list name> <view name> <workflow template name>");
                return;
            }

            string workflowColName = string.Empty;
            string siteUrl = args[0]; // "http://www.vpc.com/sites/PFW"; //
            string listName = args[1]; // "Perimeter Firewall Request"; //
            string viewName = args[2]; // "All Requests"; //
            string workflowName = args[3]; // "FirewallRequestWorkFlow"; //


            using (SPSite site = new SPSite(siteUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    web.AllowUnsafeUpdates = true;

                    SPView objView = web.Lists[listName].Views[viewName];

                    //static name of the workflow column is equal to 8_character_name_of_workflow_column,
                    // without space and is case sensitive.
                    workflowColName = workflowName.Substring(0, 8);

                    if (objView.ViewFields.Exists(workflowColName))
                    {
                        objView.ViewFields.Delete(workflowColName);
                        objView.Update();

                        Console.WriteLine(string.Format("{0} column: {1} has been removed successfully from the {2} view",
                            workflowName,
                            workflowColName,
                            viewName));
                    }

                   

                    web.AllowUnsafeUpdates = false;

                   
                }
            }

           

        }
    }
}





No comments:

Post a Comment