Question

I'm making a Winforms program that allows users to connect to their own SharePoint Online environments to download and upload files from their Document Libraries. I use the code below to get Site and List objects. My current problem with this method is, every time it is called, a login window pops up (from GetWebLoginClientContext method) and disappears because user would have been logged in from beginning. I need to use this method because it works with accounts that have Multi Factor Authentication enabled.

Is there any other way to login to SPO with MFA enabled without having to see the popup every time it needs to load Sites and Lists ?

I have read about app-only authentication with Client ID and Client Secret, however, it only works with the SP site that creates the ID and Secret.

    private void GetSitesAndLists(string siteUrl, out Dictionary<string, string> teamSites, out List<List> documentLibraries)
    {
        using (ClientContext clientContext = _authManager.GetWebLoginClientContext(siteUrl))
        {
            Web web = clientContext.Web;
            WebCollection site = web.GetSubwebsForCurrentUser(null);
            clientContext.Load(site, we => we.Include(w => w.Url, w => w.Title));
            clientContext.ExecuteQuery();
            teamSites = site.ToDictionary(w => w.Url, w => w.Title);
            teamSites = teamSites.OrderBy(kvp => kvp.Value).ToDictionary(k => k.Key, k => k.Value);
            ListCollection libraries = web.Lists;
            clientContext.Load(libraries, l => l.Include(li => li.DefaultViewUrl, li => li.BaseType, li => li.Title, li => li.BaseTemplate, li => li.Hidden));
            clientContext.ExecuteQuery();
            documentLibraries = libraries.Where(lib => lib.BaseType == BaseType.DocumentLibrary && lib.Hidden == false && lib.BaseTemplate == 101).ToList();                
        }
    }

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top