Forum Discussion

Will # _________ H's avatar
Will # _________ H
Brass Contributor
Jul 15, 2020

Access SharePoint Site through CSOM and OfficeDevPnP.Core

Hi there,

The SharePoint Farm: 2* SharePoint WFE + 2* SQL AlwaysOn.

  • one of SharePoint : http://K5JVF8C01:19832; another one : http://K5JVF8C02:19832
  •  Extend the SharePoint Site to another IIS virtual server for Internal User named (https://doc.litwareinc.local)

I have a program for accessing the SharePoint Site (https://doc.litwareinc.local).

I got the 401 and 500 Error. After that I access the SharePoint by program and

  • the URL is http://K5JVF8C01:19832, everything works properly. 
  • the URL is http://K5JVF8C02:19832, the same error comes out (401, 500), sometimes it works well.

Does anyone have any idea on it? Thanks

 

Spoiler
Unhandled Exception: System.Net.WebException: The remote server returned an error: (401) Unauthorized.
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate()
at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
at Core.SPHelper.Start.GetAuthenticatedContext(String siteUrl, String farmAdmin, String password, String domain) in C:\Users\Administrator\source\workspaces\ogcio.pilotsystem\Integration Class\Core.SPHelper\Core.SPHelper\Start.cs:line 84
at Core.Simulator.Program.Main(String[] args) in C:\Users\Administrator\source\workspaces\ogcio.pilotsystem\Integration Class\Core.SPHelper\Core.Simulator\Program.cs:line 53
Spoiler
Unhandled Exception: System.Net.WebException: The remote server returned an error: (500) Internal Server Error.
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
at Core.SPHelper.Start.GetAuthenticatedContext(String siteUrl, String farmAdmin, String password, String domain) in C:\Users\Administrator\source\workspaces\ogcio.pilotsystem\Integration Class\Core.SPHelper\Core.SPHelper\Start.cs:line 84
at Core.Simulator.Program.Main(String[] args) in C:\Users\Administrator\source\workspaces\ogcio.pilotsystem\Integration Class\Core.SPHelper\Core.Simulator\Program.cs:line 53
Spoiler
Under normal circumstances: Show the Site Title
SharePoint - Personal Site Host
/// OfficeDevPnP
public static ClientContext GetPnPAuthenticatedContext(string siteUrl, string farmAdmin, string password, string domain)
        {
            /// SharePointPnPCore 2019
            /// Sample
            try
            {
                SecureString secureStringPassword = GetSecureString(password);

                //Get instance of Authentication Manager  
                OfficeDevPnP.Core.AuthenticationManager authenticationManager = new OfficeDevPnP.Core.AuthenticationManager();

                var clientContext = authenticationManager.GetNetworkCredentialAuthenticatedContext(siteUrl, farmAdmin, secureStringPassword, domain);

                Uri uri = new Uri(siteUrl);

                if (uri.Scheme == "https")
                {
                    //ServicePointManager.ServerCertificateValidationCallback = delegate (object sender1, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                    //{
                    //    bool validationResult = true;
                    //    return validationResult;
                    //};

                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
                    ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(customXertificateValidation);
                }

                //clientContext.Load(clientContext.Web);
                //clientContext.Load(clientContext.Site);
                clientContext.Load(clientContext.Site.RootWeb);
                clientContext.ExecuteQuery();

                return clientContext;
            }
            catch (Exception eX)
            {
                throw eX;
            }                     
        }
/// CSOM
public static ClientContext GetAuthenticatedContext(string siteUrl, string farmAdmin, string password, string domain)
        {
            ClientContext clientContext = new ClientContext(siteUrl);

            clientContext.Credentials = new NetworkCredential(farmAdmin, password, domain);

            Uri uri = new Uri(siteUrl);

            if (uri.Scheme == "https")
            {
                //ServicePointManager.ServerCertificateValidationCallback = delegate (object sender1, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
                //{
                //    bool validationResult = true;
                //    return validationResult;
                //};

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
                ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(customXertificateValidation);
            }

            //clientContext.Load(clientContext.Web);
            //clientContext.Load(clientContext.Site);
            clientContext.Load(clientContext.Site.RootWeb);
            clientContext.ExecuteQuery();

            return clientContext;
        }

 

No RepliesBe the first to reply

Resources