Saturday, February 26, 2011

Display list views in sharepoint programmatically

In Share point each list has own view and through that the user can get the information as per his required view. We can add the views and delete the views in share point. Through program we can get what are the views available in the list.

By using the following code we can get the views available in the list
// Get the list the list and display the views using the SPViews object.
public static void GetListViews()
{
      string siteUrl = string.Empty;
      string listTitle=string.Empty;
      SPListCollection listItemCollection;
      SPList list;
      Console.WriteLine("Enter the url of the site");
      try
      {
          siteUrl = Console.ReadLine();
          if (string.IsNullOrEmpty(siteUrl))
          {
              Console.WriteLine("url not be empty or null");
          }
          else
          {
              using (SPSite currentSite = new SPSite(siteUrl))
              {
                   using (SPWeb currentWeb = currentSite.OpenWeb())
                   {
                         if (currentWeb.Exists)
                         {
                              listCollection=currentWeb.Lists;
                              if(listCollection!=null)
                              {
                                   Console.WriteLine("Enter the Title of the list");
                                   listTitle=Console.ReadLine();
                                   if (string.IsNullOrEmpty(listTitle))
                                   {
                                     Console.WriteLine("list title not empty or null");
                                   }
                                   else
                                   {
                                       if(listCollection[listTitle]!=null)
                                        {
                                           foreach (SPView view in listCollection[listTitle].Views)
                                           {
                                               Console.WriteLine(view.Title);
                                           }

                                        }
                                        else
                                        {
                                             Console.WriteLine("current wed not contained list with the title name you entered");
                                        }
                                     }
                                 }
                                 else
                                 {
                                     Console.WriteLine("list collection not exists in the current web");
                                 }
                            }
                        }
                    }
            }
      }
      catch (Exception e)
      {
          Console.WriteLine(e.Message());
      }
Console.ReadKey();
}

As shown above the views of the list will be displayed by using the SPView object.

Hope this will help.

Share this

0 Comment to "Display list views in sharepoint programmatically"

Post a Comment