I am creating a custom view for a document library using following code:

using (ClientContext clientContext = new ClientContext(siteUrl))
    #region Create List

    ListCreationInformation listCreationInformation = new ListCreationInformation();
    listCreationInformation.Description = documentLibraryName;
    listCreationInformation.Title = documentLibraryName;
    listCreationInformation.TemplateType = 101;
    List list = clientContext.Web.Lists.Add(listCreationInformation);


    #region Creating View

    string[] viewFields = new string[5];
    viewFields[0] = "Type";
    viewFields[2] = "Title";

    List documentLibrary = clientContext.Web.Lists.GetByTitle(documentLibraryName);

    ViewCreationInformation view = new ViewCreationInformation();
    view.Title = "DocumentsView";
    view.ViewFields = viewFields;
    view.SetAsDefaultView = false;



Here view is successfully created and associated with document library.

Question: Is this view should available in all new document library I create later?

Because when I am creating new document library and create custom view again the same method then the document library shows 2 views with the same name.

First Library:

enter image description here

Second Library:

enter image description here

Not able to figure it out.


When I am trying to run the same code again for the new library name then in new library name there are two views with the same name.

I know there is a solution that get all views and if my view already exists then don't add the view to library. But I want to know what's the problem in this approach.

  • how are you creating second library?using template(.stp) of first library? Commented Sep 3, 2016 at 7:51
  • No, custom code CSOM C#. Commented Sep 3, 2016 at 7:59
  • Which code is used to create the new library?
    – Nils
    Commented Sep 3, 2016 at 8:27

1 Answer 1


Before adding the view, check if it exists first.

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

namespace COM
  class Program
    static void Main(string[] args)
      // siteURL is the string that contains the site URL 
      string siteUrl = "http://serverName:50000/sites/Testing";
      // ClientContext object is used to get the context for the SharePoint objects
      ClientContext clientContext = new ClientContext(siteUrl);
      Web web = clientContext.Web;
      List list = web.Lists.GetByTitle("CustomList");
      ViewCollection viewColl = list.Views;
         views => views.Include(
            view => view.Title,
            view => view.Id));            
      foreach (View view in viewColl)
        Console.WriteLine(view.Title + "--------" + view.Id);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.