Expand Minimize
This documentation is part of the BETA release and subject to change.

Call ExecuteQuery() before accessing objects in ClientContext

Before accessing any objects in a ClientContext, you need to request them by calling ExecuteQuery().

CheckId SPC019004
TypeName CallExecuteQueryBeforeAccessingObjects
Severity Warning
Type Assembly

Bad Practice

// Get Client Context
using (ClientContext context = new ClientContext("http://yourcontexturl"))
{
    // Get the Web object
    Web web = context.Web;

    // Now you can access the web.Title object
    string title = web.Title;
}

Good Practice
// Get Client Context
using (ClientContext context = new ClientContext("http://yourcontexturl"))
{
    // Get the Web object
    Web web = context.Web;

    // Load the object and the properties you want to work with
    context.Load(web, w => w.Title);

    // ExecuteQuery before you do anything else
    context.ExecuteQuery();

    // Now you can access the web.Title object
    string title = web.Title;
}

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", "SPC019004:CallExecuteQueryBeforeAccessingObjects", 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.