Expand Minimize

Do not set properties from inside a ConditionalScope block.

SharePoint Client Side Object Model does not allow setting properties from the inside a ConditionalScope.

CheckId SPC019017
TypeName DoNotSetPropertiesInsideConditionalScope
Severity Error
Type Assembly

Bad Practice

    using (var context = new ClientContext("http://yoursite"))
    {
        Web web = context.Web;

        BasePermissions permissions = new BasePermissions();
        permissions.Set(PermissionKind.ManageWeb);

        ConditionalScope scope = new ConditionalScope(context, () => web.DoesUserHavePermissions(permissions).Value);

        using (scope.StartScope())
        {
            // You are not allowed to set properties in the ConditionalScope.

            // Example: This isn't allowed:
            web.Title = "New Title";
            web.Update();
        }
        context.ExecuteQuery();

        if (scope.TestResult.Value)
        {
            // TODO: Add your logic here instead.
        }
    }

Good Practice
    using (var context = new ClientContext("http://yoursite"))
    {
         Web web = context.Web;
        List list = web.Lists.GetByTitle("MyCustomerList");

        BasePermissions permissions = new BasePermissions();
        permissions.Set(PermissionKind.AddListItems);

        ConditionalScope scope = new ConditionalScope(context, () => web.DoesUserHavePermissions(permissions).Value);

        using (scope.StartScope())
        {
            // Start the scope.
        }
        context.ExecuteQuery();

        if (scope.TestResult.Value)
        {
            // Good Practice: After the scope has executed, verify that scope.TestResult.Value == true and perform any logic you need here.
            web.Title = "New Title";
            web.Update();
            context.ExecuteQuery();
        }
    }

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("SPCAF.Rules.ManagedCSOM.CSOMCorrectnessGroup", "SPC019017:DoNotSetPropertiesInsideConditionalScope", 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.