Unable to connect to sharepoint 2017 site from C# code.

Copper Contributor

Hi everyone,

I need help in order to develop CSharp console module to migrate documents and its properties from Sharepoint 2017 site. 

Org share point site uses SSO and its on prem. 

When I am trying to to connect using sharepoint.dll, It failed with null pointer exception for connection object. 

I am able to browse documents from web browser and also confirmed with admin that I have read only access to the site. 

 

I tried both ways e.g. Credential manager or hard coding credentials of mine. 

Not sure if I am missing something here.

Can someone please guide me?
code snippets given as below:

 

 

 

using CredentialManagement;
using Microsoft.BusinessData.MetadataModel;
using Microsoft.SharePoint.Client;
using System;
using System.Diagnostics;
using System.Net;
using System.Runtime.InteropServices;
using System.Security;

namespace DetectOSCredentialManagement
{
    class Program
    {
        internal static string _siteURL = string.Empty;
        static void Main(string[] args)
        {
             var _siteURL = "http://ecm.***.**********.com/sites/*****";
	     var _login = "xyz123@*****.com";
             var _password = "*************";
            try
            {
                //approach001(); // No luck
                //approach002(); //No luck
                approach003(_siteURL,_login,_password); //No luck
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR! \t" + ex.Message);
            }		
		
//APPROACH 3
	private static void approach003(string url,var password, var login)
        {
          try
            {
                var targetSite = new Uri(url);                
                var securePassword = new SecureString();
                foreach (char c in password)
                {
                    securePassword.AppendChar(c);
                }
                NetworkCredential _myCredentials = new NetworkCredential
                {
                    UserName = login,                    
                    SecurePassword = securePassword
                };
                ClientContext ctx = new ClientContext(_siteURL)
                {
                    Credentials = _myCredentials
                };
                Web _MyWebSite = ctx.Web;
                Entity entity = ctx.Web.GetEntity(_siteURL, "My%20Policy%20Records");
                ctx.Load(entity);
                ctx.ExecuteQuery();
                Console.WriteLine("CTX \t" + ctx.Site);
            }
            catch (Exception)
            {
              throw;
            }   
        }		
//APPROACH 2
		
	private static void approach002(string url,var password, var login)
        {
            try
            {
                var targetSite = new Uri(url);                
                var securePassword = new SecureString();
                foreach (char c in password)
                {
                    securePassword.AppendChar(c);
                }

                var onlineCredentials = new SharePointOnlineCredentials(login,  
securePassword);
                using (ClientContext clientContext = new ClientContext(targetSite))
                {
                    clientContext.Credentials = onlineCredentials;
                    Web web = clientContext.Web;
                    clientContext.Load(web,
                    webSite => webSite.Title);
                    clientContext.ExecuteQuery();
                    Console.WriteLine(web.Title);
                    Console.Read();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
		
//APPROACH 1
		
	private static void approach001(string _siteURL)
        {
           try{
	        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                  {
                     Console.WriteLine("--Try Get Credential");
                     Program.SetCredentials("****@***lab.com", "******@**lab.com", "********", PersistanceType.LocalComputer);
                     var userpass = Program.GetCredential("*****.*****lab.com");
                     var securePassword = new SecureString();
                     foreach (char c in userpass.Password)
                     {
                        securePassword.AppendChar(c);
                     }
                NetworkCredential _myCredentials = new NetworkCredential
                {
                    UserName = userpass.Username,
                    SecurePassword = securePassword
                };
                
                Console.WriteLine($"User: {userpass.Username} Password: {userpass.Password}");                               
                ClientContext ctx = new ClientContext(_siteURL);
                ctx.Credentials = new SharePointOnlineCredentials(userpass.Username, securePassword);
                Web _MyWebSite = ctx.Web;
                Entity entity = ctx.Web.GetEntity(_siteURL, "My%20Policy%20Records");
                ctx.Load(entity);
                ctx.ExecuteQuery();
                Console.WriteLine("CTX \t" + ctx.Site);
                System.Console.ReadLine();
            }
	}
        catch(Exception)
        {
	 throw;
        }            
     }			
    }		
   }

 

 

 

 

 

 

 

 

0 Replies