Monday, February 28, 2011

Change the default view of a list programmatically in share point

Every list in share point has a view to display the contents. We can change or we can create the new view through UI. we can also change the default list view through coding also.the below code shows how to change the default view of a list.



//Change the default list view of a string in share point
        private static void ChangeListDefaultView(SPList list)
        {
            string listViewName;
            Console.WriteLine("enter the name of the list view to set as default");
            listViewName = Console.ReadLine();
            if (!string.IsNullOrEmpty(listViewName))
            {
                if (list.Views[listViewName] != null)
                {
                    list.Views[listViewName].DefaultView = true;
                    list.Update();
                    Console.WriteLine("changed the list default view");
                }
                else
                {
                    Console.WriteLine("Given list view is not exists for the " + list.Title + " list");
                }
            }
            else
            {
                Console.WriteLine("list view name should not be null or empty");
            }
        }


In the above code snippet make the property of the list view default name as true. And update the list. Remain that web.AllowUnsafeUpdations must be TRUE before you updating any thing regarding the web.

Share this

0 Comment to "Change the default view of a list programmatically in share point"

Post a Comment