Share via


PropertyOrFieldNotInitializedException error using clientcontext

Question

Wednesday, June 27, 2012 8:26 AM

Am getting this error , not able to figure out why. My code seems to be fine.

It fails after the line ClientContext context = new ClientContext("http://win-6aokaqru2uv:19094/sites/Dem");

Error : ServerVersion 'context.ServerVersion' threw an exception of type 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException' System.Version {Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException}

ClientContext context = new ClientContext("http://win-6aokaqru2uv:19094/sites/Dem");
           
            context.Load(context.Web);
            List cusDetails = context.Web.Lists.GetByTitle("Customers");
            context.Load(cusDetails);

            //SelectedDateTextBox.Text = "Customers";            
           
            CamlQuery query = new Microsoft.SharePoint.Client.CamlQuery();
           string camlQueryXml = "<View>" +
                "<Query><Where><Eq><FieldRef Name='EventDate'/><Value Type='DateTime' IncludeTimeValue='false'>" + SelectedDateTextBox.Text.ToString() + "</Value></Eq></Where></Query>" +
                "<ViewFields>" +
                "<FieldRef Name=\Title\/><FieldRef Name=\Location\/><FieldRef Name=\Category\/><FieldRef Name=\EventDate\/>" +
                "</ViewFields></View>";

           /* string camlQueryXml = "<View>" +                
                "<ViewFields>" +
                "<FieldRef Name=\Title\/>" +
                "</ViewFields></View>";*/
           
            query.ViewXml = camlQueryXml;
            MessageBox.Show(camlQueryXml);
           
            _custDetails = cusDetails.GetItems(query);            
            context.Load(_custDetails);            
            context.ExecuteQueryAsync(new ClientRequestSucceededEventHandler(OnRequestSucceeded), new ClientRequestFailedEventHandler(OnRequestFailed));

All replies (7)

Wednesday, June 27, 2012 8:28 PM ✅Answered | 1 vote

Hi Cutloo,

Could you post your entire code here?.

I am posting my code as an example to see if this helps you solve your problem.

  using (ClientContext ctx = new ClientContext("http://vsiffsp.datalandmz.com:4761"))
            {
                Site oSite = ctx.Site;
                Web oWeb = ctx.Web;
                List oList = oWeb.Lists.GetByTitle("Test");
                              
                ctx.Load(oList);
                ctx.ExecuteQuery();
                if (oList != null)
                {
                    
                   
                    CamlQuery cmlQuery = new CamlQuery();
                    cmlQuery.ViewXml = @"<View>

                        <Query>

                                  <Where>

                                <Eq>

                                  <FieldRef Name='_Status'/>

                                  <Value Type='Choice'>Not Started</Value>

                                </Eq>

                                  </Where>
 <ViewFields>
                    <FieldRef Name='Source_x0020_Document_x0020_URL' />
                     
                     </ViewFields>

                        </Query>
                   

                              </View>";
                   

                    ListItemCollection oListItemCollection = oList.GetItems(cmlQuery);
                    ctx.Load(oList);
                    ctx.Load(oListItemCollection);
                    ctx.Load(oSite);
                    ctx.Load(oWeb);
                    ctx.ExecuteQuery();

}

I have a test list and I am getting all the items that have status as "Not Started". Make sure you load the List object before running the query and run execute query. I quickly wrote this on Managed Client object model but the concept remains the same for ECMA script.

VAravind Please click "Propose As Answer" if a post solves your problem or "Vote As Helpful" if a post has been useful to you.


Thursday, June 28, 2012 9:15 AM ✅Answered

Silverlight requires absolute url and ECMAScript requires relative url.

This is mentioned in the link, I have provided.

Thanks, Neha Navale


Wednesday, June 27, 2012 10:10 AM

Does it fail on ClientContext context = new ClientContext("http://win-6aokaqru2uv:19094/sites/Dem");?

or on a line after it?


Wednesday, June 27, 2012 11:01 AM

Yes the exception come on this line.


Wednesday, June 27, 2012 2:33 PM

For ECMAScript you have to provide a server-relative URL:

[JavaScript or ECMAScript]
var clientContext = new SP.ClientContext("/mySiteCollection/mySite");

Refer link

http://books.google.co.in/books?id=4lKnn9ZzG1AC&pg=PA723&lpg=PA723&dq=var+value+%3D+new+ClientContext(server+Absolute+Url);&source=bl&ots=LR3oPonQzu&sig=o6kek1FkQozWgXt_boS4Dxt2wJ8&hl=en&sa=X&ei=ARnrT7KbH8fwrQfB5KjFBQ&ved=0CFAQ6AEwAA#v=onepage&q=var%20value%20%3D%20new%20ClientContext(server%20Absolute%20Url)%3B&f=false

Thanks, Neha Navale


Wednesday, June 27, 2012 6:11 PM

Hi NehaNavale,

you don't need to provide a server-relative URL, you can do it by absolute.

What I understand , Cutloo did it in winform/silverlight, am I right?

Can you publish your  whole code?

By the way, look at this:

http://social.technet.microsoft.com/Forums/en-AU/sharepoint2010programming/thread/245bae70-0fd7-4d17-9f40-5489db537d65


Friday, February 8, 2013 11:43 AM | 1 vote

Hi Cutloo,

I faced the same problem in my console application.

ServerVersion = 'context.ServerVersion' threw an exception of type 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException'

After doing lot of searching on internet, I came to know that everybody is trying to change the code to make it work. But this didn't helped me.

I have mentioned the steps, following which I managed to resolve this issue -

Go to IIS --> <YourSPSite> --> _vti_bin --> Clent.svc --> Browse.
Observe whether this service is working.
If not working then follow the steps as mentioned below -
Go to IIs --> <YourSPSite> --> Bindings and remove all extra bindings. OR check AAM in SP Central Admin.
Again check the Clent.svc. Must be working now.
Run the code from Visual Studio, it should work.

Ashok Sabne