2

I need some support as I am struggling to understand what is wrong with my code. I try to retrieve the data of a specific view. The line

camlQuery.ViewXml = view.ViewQuery;

throws an exception The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested. Obviously there is something wrong the way I am handling the view, any suggestions?

// ClientContext object is used to get the context for the SharePoint object 
// Create a client connection
ClientContext clientContext = new ClientContext(siteUrl);
//user login via Form Authentication 
clientContext.AuthenticationMode = ClientAuthenticationMode.FormsAuthentication;
FormsAuthenticationLoginInfo fba = new FormsAuthenticationLoginInfo(siteUser, sitePassword);
clientContext.FormsAuthenticationLoginInfo = fba;

Web site = clientContext.Web;
List list = site.Lists.GetByTitle(ListName);
View view = list.Views.GetByTitle(ViewName);
ViewFieldCollection viewFields = view.ViewFields;

//Create the statement to retrieve the data
//pass  CamlQuery instance into a method on the List to retrieve the data.
//The return value from the GetItems method on the client OM’s List class is a ListItemCollection.  

//CamlQuery camlQuery = CamlQuery.CreateAllItemsQuery();

CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = view.ViewQuery;

//-----------------------
ListItemCollection ListColletion = list.GetItems(camlQuery);
clientContext.Load(list);
clientContext.Load(ListColletion);
clientContext.Load(viewFields);
clientContext.ExecuteQuery();
2

You are getting this error because you are using the property of the object which is not yet initialized. So to initialize this just write

  clientContext.Load(view);
  clientContext.ExecuteQuery();

after the line:

View view = list.Views.GetByTitle(ViewName);

  • Yes! You made me a very happy man, thank you for the letting me know.... – volker Jun 4 '14 at 11:14

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.