0

I have install a fresh version of SharePoint 2019 which is on domain A,then i added a second computer to A which will be hosting websites on IIS.

I have created a console application just for testing connection to SharePoint on the second computer using the code below and am getting unauthorized access.Is there anything wrong with the code below?. Note the credentials are correct.

        System.Net.NetworkCredential cred = new System.Net.NetworkCredential(@"DoaminNAme\administrator","Password");

        using (ClientContext clientContext = new ClientContext("http://10.1.1.1/sharepoint2019/sites/test/"))
        {
            clientContext.AuthenticationMode = ClientAuthenticationMode.Default;

            clientContext.Credentials = cred;

            Microsoft.SharePoint.Client.ClientResult<System.IO.Stream> stream = null;

            KeywordQuery keywordQuery = new KeywordQuery(clientContext);

            keywordQuery.QueryText = "SharePoint";

            keywordQuery.EnablePhonetic = true;

            keywordQuery.EnableOrderingHitHighlightedProperty = true;
            //keywordQuery.SummaryLength = 500;


            SearchExecutor searchExecutor = new SearchExecutor(clientContext);

            ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);

            clientContext.ExecuteQuery();
4
  • The code looks fine. The url seems a bit strange and the NetworkCredentials, too. I have never used domain\\user when using NetworkCredential. I always use new NetworkCredential ("user", "password", "domain");
    – Nils
    Jun 17, 2019 at 7:55
  • @Nils i changed the NetworkCredentials as per your guideline but the error persist. Yes the URL is strange, i was not sure what to use,can you give me an example please Jun 17, 2019 at 7:58
  • You'll need the same url that is used when accessing SharePoint using a browser. Typically it should look somthing like https://sharepoint.server/sites/sitename
    – Nils
    Jun 17, 2019 at 8:01
  • @Nils If i used this links sharepoint2019/sites/sitename i get the error cannot resolve the name sharepoint2019 Jun 17, 2019 at 8:06

1 Answer 1

1

You need to make sure you have the correct URL for your SharePoint/Location.

I'd suggest using a simple browser (Chrome, FF, IE, whatever). If you can not access your SharePoint using the browser, the code will probably never work.

1
  • Ya the url was incorrect in my case. The servername was incorrect in my case Jun 17, 2019 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.