Expand Minimize

Use SetDefaultContentTypeToList to set the default content type.

PnP has an implementation to set the default content type.

CheckId SPC029010
TypeName UseSetDefaultContentType
Severity Warning
Type Assembly

Standard SharePoint CSOM

List list = List.GetList(SelectedWeb);
var ctCol = list.ContentTypes;
list.Context.Load(ctCol);
list.Context.ExecuteQueryRetry();

var ctIds = ctCol.AsEnumerable().Select(ct => ct.Id).ToList();

// remove the folder content type
var newOrder = ctIds.Except(ctIds.Where(id => id.StringValue.StartsWith("0x012000")))
                    .OrderBy(x => !x.StringValue.StartsWith(contentTypeId, StringComparison.OrdinalIgnoreCase))
                    .ToArray();

list.RootFolder.UniqueContentTypeOrder = newOrder;

list.RootFolder.Update();
list.Update();
list.Context.ExecuteQueryRetry();

Using PnP
    ContentType ct = null;
    List list = List.GetList(SelectedWeb);

    if (ContentType.ContentType == null)
    {
        if (ContentType.Id != null)
        {
            ct = SelectedWeb.GetContentTypeById(ContentType.Id, true);
        }
        else if (ContentType.Name != null)
        {
            ct = SelectedWeb.GetContentTypeByName(ContentType.Name, true);
        }
    }
    else
    {
        ct = ContentType.ContentType;
    }

    if (ct != null)
    {
        SelectedWeb.SetDefaultContentTypeToList(list, ct);
    }

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", "SPC029010:UseSetDefaultContentType", 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.