Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

[UWP] Cannot create multiple instance of Microsoft.SharePoint.Client.ClientContext or setting the Credentials again

$
0
0

Dear all,

Im trying to connect to a SharePointOnline 2013. In my C# Console Application everything works fine, but if I try (nearly) the same code in a Universal App, it is not possible to create multiple instances of a ClientContext (nor setting the credentials multiple times)...

Im using the Microsoft.SharepointOnline.CSOM Version: 16.1.4727.1204 (released today 11.12.2015)

Here the console application which works fine:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            UseContextMultipleTime();
            UseContextMultipleTime();
        }

        public static void UseContextMultipleTime()
        {
            using (var context = new ClientContext("https://something.sharepoint.com"))
            {
                var securePassword = new SecureString();

                foreach (char c in "password".ToCharArray()) securePassword.AppendChar(c);

                context.Credentials = new SharePointOnlineCredentials("username", securePassword);

                try
                {
                    context.ExecuteQuery();
                    string status = "OK";
                }
                catch (Exception ex)
                {
                    string test = ex.ToString();
                }
            }
        }
    }
}

and below the Universal App version, whichs crashs in the second method call:

namespace App2
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();

            UseContextMultipleTime();
            UseContextMultipleTime();
        }

        public void UseContextMultipleTime()
        {
            using (var context = new ClientContext("https://something.sharepoint.com"))
            {
                context.Credentials = new SharePointOnlineCredentials("username", "password");
                try
                {
                    context.ExecuteQueryAsync().Wait();
                    string status = "OK";
                }
                catch (Exception ex)
                {
                    string test = ex.ToString();
                }
            }
        }
    }
}

I already invested a lot of time and tried multiple variants like global variable context with disposing instead of using etc... interesting is that if you set the credentials only once, then it will work..

Does anybody of you know a solution for this problem? I will appreciate every kind of hints...

Thanks in advance,

Lorenz



Viewing all articles
Browse latest Browse all 7589

Trending Articles