Blogs

Blogs

Creating Customised Views Programmatically in SharePoint

8th September 2014

In Sharepoint, I had trouble creating views as they can be created in different ways. Here are some generic examples which I arrived at through trial and error. Note that the field internal name and field title name are used to set up these views.

Default View

Although Sharepoint creates a default view when a list is created, it only adds the LinkTitle field.

The default view can have fields added to it as shown below. We can also delete fields as well as add them. A CAML query can be attached to the view for sorting purposes.

Note the use of internal and display names.

SPWeb web = site.OpenWeb();
SPList reportsList = web.Lists.TryGetList("Reports");

if (reportsList != null)
{
   SPView view = reportsList.DefaultView;
   if (view.ViewFields.Exists("LinkTitle"))
   {
      view.ViewFields.Delete("LinkTitle");
   }

   if (!view.ViewFields.Exists("Field Internal Name 1"))
   {
      view.ViewFields.Add("Field Title Name 1");
   }

   if (!view.ViewFields.Exists("Field Internal Name 2"))
   {
      view.ViewFields.Add("Field Title Name 2");
   }

   string queryCAML = string.Format(
      @"<OrderBy>
      <FieldRef Name='{0}' Ascending='{1}' />
      </OrderBy>",
      "Field Internal Name 1",
      "FALSE");

   view.Query = queryCAML;
   view.Update();
}

New View

Views can be created from scratch. In the code below, the view is first checked to see if it exists. Note the use of the SpViewCollection Add method:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spviewcollection.add.aspx

We also found it good practice to ensure that if a view already exists, then the view fields also exist.

SPWeb web = site.OpenWeb();
SPList newsList = web.Lists.TryGetList("News");

if (newsList != null)
{
   bool viewExists = false;
   foreach (SPView vw in newsList.Views)
   {
      if (vw.Title == "Latest News")
      {
         viewExists = true;
      }
   }

   if (!viewExists)
   {
      StringCollection viewFields = new StringCollection();
      viewFields.Add("Field Internal Name 1");
      viewFields.Add("Field Internal Name 2");
      viewFields.Add("Field Internal Name 3");

      string queryCAML = string.Format(
         @"<GroupBy Collapse=""TRUE"">
         <FieldRef Name='{0}' />
         </GroupBy>
         <OrderBy>
         <FieldRef Name='{1}' Ascending='{2}' />
         </OrderBy>",
         "Internal Name 1",                        
         "Internal Name 2"
         "FALSE");

         newsList.Views.Add("Latest News", viewFields, queryCAML, int.MaxValue, true, false);
   }
   else
   {
      SPView view = newsList.Views["Latest News"];
                   
      if (view.ViewFields.Exists("LinkTitle"))
      {
         view.ViewFields.Delete("LinkTitle");
      }

      if (!view.ViewFields.Exists("Field Title Internal Name 1"))
      {
         view.ViewFields.Add("Field Display Name 1");
      }

      if (!view.ViewFields.Exists("Field Title Internal Name 2"))
      {
         view.ViewFields.Add("Field Display Name 2");
      }

      if (!view.ViewFields.Exists("Field Title Internal Name 3"))
      {
         view.ViewFields.Add("Field Display Name 3");
      }

       string queryCAML = string.Format(
          @"<GroupBy Collapse=""TRUE"">
          <FieldRef Name='{0}' />
          </GroupBy>
          <OrderBy>
          <FieldRef Name='{1}' Ascending='{2}' />
          </OrderBy>",
          "Internal Name 1",                        
          "Internal Name 2"
          "FALSE");

      view.Query = queryCAML;
      view.Update();
}
This website uses cookies to provide the best possible experience. By continuing to use our website you are agreeing to our use of cookies.