0

How to get SharePoint online list data in my windows 8.1 Universal apps?

I want to do crud operations in SharePoint list data.

I have tried CSOM for that but no work. My CSOM code is:

string url = "Office 365 Url";

using (ClientContext clientContext = new ClientContext(url))
{
   clientContext.Credentials = new SharePointOnlineCredentials("administator id", "password");
   clientContext.AuthenticationMode = ClientAuthenticationMode.Default;

   Web web = clientContext.Web;

   clientContext.Load(clientContext.Web);
   clientContext.Load(web);
   clientContext.ExecuteQueryAsync();

   Title.Text = web.Title;

}

But this constantly gives me below error:

The property or the field "Title" has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.

I have referred this link: https://social.msdn.microsoft.com/Forums/office/en-US/efd12f11-cdb3-4b28-a9e0-32bfab71a419/windows-phone-81-sdk-for-sharepoint-csom?forum=sharepointdevelopment

I don't want to use REST api in my code.

Any help will be appreciated...

3

This error occurs since Title property is not retrieved when you try to access it. ClientContext.ExecuteQueryAsync method is asynchronous operation and you could utilize Task.ContinueWith to wait until Web is loaded.

The below example demonstrates how to invoke ClientContext.ExecuteQueryAsync method and wait until Web is loaded:

using (var ctx = new ClientContext(webUri))
{
    ctx.Credentials = new SharePointOnlineCredentials(userName, password);
    ctx.AuthenticationMode = ClientAuthenticationMode.Default;
    var web = ctx.Web;
    ctx.Load(web);
    var webTask = ctx.ExecuteQueryAsync();
    webTask.ContinueWith((t) =>
    {
        var webTitle = web.Title;
    });
}

References

Consuming the Task-based Asynchronous Pattern

  • Thanks for reply...But it throws same error. – Dikesh Gandhi Jan 19 '15 at 12:46
  • please help me more to assign web.Title to Title.Text. When trying to assign within ContinueWith((t) it throws exception The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)) – Dikesh Gandhi Jan 22 '15 at 3:57
2

Finally, got answer after long effort.

string url = "Office 365 Url"; 
using (ClientContext clientContext = new ClientContext(url))
{
   clientContext.Credentials = new SharePointOnlineCredentials("administator id", "password");
   clientContext.AuthenticationMode = ClientAuthenticationMode.Default;

   Web web = clientContext.Web;

   clientContext.Load(web);
   var webTask = clientContext.ExecuteQueryAsync();

   webTask.Wait();
   Title.Text = web.Title;

}

Just need to wait ExecuteQueryAsync() method.

1

Try to explicitly load the title property:

string url = "Office 365 Url";

using (ClientContext clientContext = new ClientContext(url))
{
   clientContext.Credentials = new SharePointOnlineCredentials("administator id", "password");
   clientContext.AuthenticationMode = ClientAuthenticationMode.Default;

   Web web = clientContext.Web;

   clientContext.Load(clientContext.Web);
   clientContext.Load(web, w => w.Title);
   clientContext.ExecuteQuery();

   Title.Text = web.Title;

}
  • Tried that buddy... Unfortunately, got same error... – Dikesh Gandhi Jan 19 '15 at 11:08
  • Updated to not make use of the Async method for executing queries. – Robert Lindgren Jan 19 '15 at 12:38
0

Your code seems incomplete or wrong. As per my understanding you need to give the parameter of success and fail method name in the ExecuteQueryAsync call.

I think once you change ExecuteQueryAsync to ExecuteQuery, you will get your result.

You can refer this link for basic CRUD operation in SharePoint 2013 using Client side library.

Hope this helps!

  • In windows 8.1,You have to use WINRT framework. You have to attach dlls from 16 hive. so in that case you can't get ExecuteQuery. You have to use ExecuteQueryAsync. – Dikesh Gandhi Jan 19 '15 at 10:36
  • Then you need to take a look at this method here: msdn.microsoft.com/en-us/library/office/… . In that you specify success callback and fail callback. – Pradip R. Jan 19 '15 at 10:54
  • codeproject.com/Articles/738008/… This might help you as well. – Pradip R. Jan 19 '15 at 10:57
  • Tried that but not worked... – Dikesh Gandhi Jan 19 '15 at 11:01
  • Did you try putting await keyword before ExecuteQueryAsync? – Pradip R. Jan 19 '15 at 11:27

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.