0

I am developing a Console application to programmatically download documents from a SP site among other things. I am trying to retrieve the fields of a content type of a SP list using CSOM :

using (var context = new ClientContext(webUrl))
{    
     context.Credentials = new NetworkCredential(userName, securePassword, domain);
     var web = context.Web;    
     var list = context.Web.Lists.GetByTitle(listTitle);               
     context.Load(list);
     ContentTypeCollection lstContentTypes = list.ContentTypes;
     context.Load(lstContentTypes, l => l.Include(
                         c => c.Name,
                         c => c.Fields));
     context.ExecuteQuery();
    
     var contentType = lstContentTypes.Where(ct => ct.Name == contentTypeName).FirstOrDefault();
}

I get the following error:

System.NotSupportedException Message=Invalid usage of query execution. The query should be executed by using ExecuteQuery method on the client context object.

Here is the line of code that is causing the problem :

var contentType = lstContentTypes.Where(ct => ct.Name == contentTypeName).FirstOrDefault();

This is driving me crazy. I am working on a local variable that has the content types of the said list. My Load and ExecuteQuery have been executed successfully before the exception is thrown. What does the client context object have to do with it?

NB : The exact same code works perfectly in a WinForms application. Environment : .NET 4.5.2, VS 2019, VS 2015

2
  • What kind of .Net Framework are you using form the console app? Mar 18 at 13:43
  • The target framework in both apps (Console and WinForms) is the .NET 4.5.2
    – Bellatrix
    Mar 18 at 14:16
0

No Issues found in your code running VS 2017 (as administrator) with .NET 4.5

private static void GetContentType(string webURL, string listName)

            {

            using (var context = new ClientContext(webURL))
                {    
                // context.Credentials = new NetworkCredential(userName, securePassword, domain);
                var web = context.Web;
                var list = context.Web.Lists.GetByTitle(listName);
                context.Load(list);
                ContentTypeCollection lstContentTypes = list.ContentTypes;
                context.Load(lstContentTypes, l => l.Include(
                            c => c.Name,
                         c => c.Fields));
                context.ExecuteQuery();

                var contentTypeName = "Item";
    
                var contentType = lstContentTypes.Where(ct => ct.Name == contentTypeName).FirstOrDefault();

                Console.WriteLine("Filter Content Type field count: " + contentType.Fields.Count.ToString());

                foreach(Field f in contentType.Fields)
                {
                    Console.WriteLine("Field Title: " + f.Title);

                }

                Console.ReadKey();

                }
            }


enter image description here

0

I ended up going with a workaround to get the fields of the said content type :

 foreach (var c in lstContentTypes)
 {
     if (c.Name == contentTypeName)
     {
         return c.Fields;
     }
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.