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);
clientContext.Load(list);
clientContext.ExecuteQuery();
#endregion
#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;
documentLibrary.Views.Add(view);
clientContext.ExecuteQuery();
#endregion
}
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:
Second Library:
Not able to figure it out.
Update:
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.