0
votes

I am using below code to get the information about Sharepoint document:

using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp1
{
    class Class1
    {    
        static void Main(string[] args)    
        {    
          //make changes based on your site url
          HttpWebRequest endpointRequest =(HttpWebRequest)HttpWebRequest.Create("https://company.sharepoint.com/_api/search/query?querytext='docName");
          endpointRequest.Method = "GET";
          endpointRequest.Accept = "application/json;odata=verbose";
          endpointRequest.Headers.Add("Authorization","Bearer " + accessToken);
          HttpWebResponse endpointResponse =(HttpWebResponse)endpointRequest.GetResponse();     
        }
    }
}

But I am getting below errors:

Severity Code Description Project File Line Suppression State Error CS0234 The type or namespace name 'SharePoint' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) ConsoleAppSP C:\Program.cs 1 Active

How to add Microsoft Sharepoint library to Visual studio 2019 Preview?
(I am new to C#, Visual Studio and SharePoint).

1
Have you tried to check if the .NetFramework is set correctly? - Alessandra Amosso
Also, which version of Sharepoint are you using? - Alessandra Amosso
@AlessandraAmosso I used nupkg of Micrsoft Sharepoint 15.04. Now,sharepoint issue got resolved ,but please tell me how to populate accessToken with username password or domain name for sharepoint. - AllTech
Have you already checked this document? - Alessandra Amosso

1 Answers

1
votes

The following steps for your reference.

1.Create a console application using Visual Studio.

2.Install SharePoint Online Client library using Nuget below.

Install-Package Microsoft.SharePointOnline.CSOM -Version 16.1.19515.12000

3.Copy the code below to Program.cs.

using System;
using System.Security;
using Microsoft.SharePoint.Client;
using System.Net;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string userName = "[email protected]";
            string password = "xxx";

            string apiUrl = "https://tenant.sharepoint.com/_api/search/query?querytext='docName'";
            var securePassword = new SecureString();
            foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
            var credential = new SharePointOnlineCredentials(userName, securePassword);
            Uri uri = new Uri(apiUrl);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "GET";
            request.Credentials = credential;
            request.Headers[HttpRequestHeader.Cookie] = credential.GetAuthenticationCookie(new Uri(apiUrl), true);  // SPO requires cookie authentication
            request.Headers["X-FORMS_BASED_AUTH_ACCEPTED"] = "f";

            HttpWebResponse webResponse = (HttpWebResponse)request.GetResponse();
            Stream webStream = webResponse.GetResponseStream();
            StreamReader responseReader = new StreamReader(webStream);
            string response = responseReader.ReadToEnd();

        }
    }
}

4.Modify the tenant name, username and password to make it works.