Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am trying to connect to Office 365 using console application , please find my cod ebelow. But i am getting error
Cannot contact site at the specified URL http://mysite-my.sharepoint.com

What I have tried:

using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security;
using Microsoft.SharePoint.Client;
using System.Net;
using Microsoft.SharePoint;

namespace OneDrive
{
    class Program
    {
        
        static void Main(string[] args)
        {
            GetSiteLists();
            Console.ReadLine();
        }
        
        class configuration
        {
            public static string o365SiteUrl = "http://mysite-my.sharepoint.com";
            public static string o365UserName = "username";
            public static string o365Password = "pwd";
            public static string o365Domain = "domail";
           
        }
        static ClientContext GetUserContext()
        {
            var o365password = new SecureString();
            foreach (char c in configuration.o365Password)
            {
                o365password.AppendChar(c);
            }
            // var o365Credentials = new SharePointOnlineCredentials(configuration.o365UserName, o365password);
           NetworkCredential o365Credentials = new NetworkCredential(configuration.o365UserName, o365password, configuration.o365Domain );
           

            ClientContext o365Context = new ClientContext(configuration.o365SiteUrl);
            System.Net.ServicePointManager.ServerCertificateValidationCallback =
        ((sender, certificate, chain, sslPolicyErrors) => true);

            o365Context.Credentials  = o365Credentials;
            return o365Context;
        }
        static void GetSiteLists()
        {
            var clientContext = GetUserContext();
            //clientContext.ExecutingWebRequest += clientContext_ExecutingWebRequest;
            var results = clientContext.LoadQuery(clientContext.Web.Lists.Include(List => List.Title, List => List.Id ));
            clientContext.ExecuteQuery();
            results.ToList().ForEach(x =>
            {
                Console.WriteLine(x.Title);
            });
            clientContext.Dispose();



        }
    

    }
Posted
Updated 8-Mar-18 2:22am

Quote:
You can use your My Site to share information and connect with other people to build your professional network. This article describes some key features, and provides some steps to help you get started.


As is described above, you need to configure your sharepoint site. I'd strongly recommend to read below articles:
Getting started with your My Site - SharePoint[^]
Configure My Sites in SharePoint Server 2013[^]
 
Share this answer
 
Comments
Member 13690846 6-Mar-18 6:09am    
Hi Maciej Los,
Thanks for the reply. I can access the o365SiteUrl from the front end and its working fine. Any other configuration i need to do to connect through a console application?
Maciej Los 6-Mar-18 6:18am    
I have no idea. Seems, you need to go to login page to authorize your entry, then to your site.
Member 13690846 6-Mar-18 6:13am    
And the mentioned URL in the sample code a dummy one not the actual one.
I tried many things with no success. SO i have tried with Microsoft Graph and it worked fine.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900