Expand Minimize

Use CreateView to create views.

PnP has an implementation to create views.

CheckId SPC029007
TypeName UseCreateView
Severity Warning
Type Assembly

Standard SharePoint CSOM

var list = List.GetList(SelectedWeb);
if (list != null)
    var listType = list.BaseType.ToString();
    var listViews = xmlDoc.SelectNodes("ListViews/List[@Type='" + listType + "']/View");

    foreach (XmlNode view in listViews)
        string viewName = view.Attributes["Name"].Value;
        ViewType viewType = (ViewType)Enum.Parse(typeof(ViewType), view.Attributes["ViewTypeKind"].Value);
        string[] viewFields = view.Attributes["ViewFields"].Value.Split(',');
        uint rowLimit = uint.Parse(view.Attributes["RowLimit"].Value);
        bool defaultView = bool.Parse(view.Attributes["DefaultView"].Value);
        string query = view.SelectSingleNode("./ViewQuery").InnerText;

        if (string.IsNullOrEmpty(viewName))
        throw new ArgumentNullException(nameof(viewName));

        var viewCreationInformation = new ViewCreationInformation
            Title = viewName,
            ViewTypeKind = viewType,
            RowLimit = rowLimit,
            ViewFields = viewFields,
            PersonalView = false,
            SetAsDefaultView = defaultView,
            Paged = false
        if (!string.IsNullOrEmpty(query))
            viewCreationInformation.Query = query;

        var view = list.Views.Add(viewCreationInformation);

Using PnP
var list = List.GetList(SelectedWeb);
if (list != null)
    var view = list.CreateView(Title, ViewType, Fields, RowLimit, SetAsDefault, Query, Personal, Paged);

To suppress this violation in managed code add the following attribute to the method which contains the instruction (available since SPCAF version v5.2). Learn more about SuppressMessage here.

// Important: Ensure to have #define CODE_ANALYSIS at the beginning of your .cs file
[SuppressMessage("Rencore.Analyzers.Assemblies.Rules.PnP.PnPBestPracticesGroup", "SPC029007:UseCreateView", Justification = "Provide reason for suppression here")]
Disclaimer: The views and opinions expressed in this documentation and in SPCAF do not necessarily reflect the opinions and recommendations of Microsoft or any member of Microsoft. SPCAF and RENCORE are registered trademarks of Rencore. All other trademarks, service marks, collective marks, copyrights, registered names, and marks used or cited by this documentation are the property of their respective owners.