0

I tried accessing SharePoint site using CSOM (C# with HttpClient and WebClient) but there also for authentication I need to use SharePoint Client SDK.

Is there any way so that I can perform SharePoint operations without using SharePoint.Client and SharePoint.Client.Runtime DLL on client machine (Non-SharePoint environment)?

1 Answer 1

2

SharePoint Client SDK is need for CSOM only. If you don't want to use SharePoint Client SDK, then you can use REST API. For accessing REST API, you do not need SharePoint Client SDK. Have a look on the following example using HttpWebRequest & REST API.

HttpWebRequest endpointRequest = (HttpWebRequest) HttpWebRequest.Create("Site URL/_api/web/lists/getByTitle(List Name')/items");

 endpointRequest.Method = "GET";
 endpointRequest.Accept = "application/json;odata=verbose";
 NetworkCredential cred = new System.Net.NetworkCredential("username", "password", "domain");
 endpointRequest.Credentials = cred;
 HttpWebResponse endpointResponse = (HttpWebResponse) endpointRequest.GetResponse();
 try {
     WebResponse webResponse = endpointRequest.GetResponse();
     Stream webStream = webResponse.GetResponseStream();
     StreamReader responseReader = new StreamReader(webStream);
     string response = responseReader.ReadToEnd();
     JObject jobj = JObject.Parse(response);
     JArray jarr = (JArray) jobj["d"]["results"];
     foreach(JObject j in jarr) {
         Console.WriteLine(j["Title"] + " " + j["Body"]);
     }

     responseReader.Close();
     Console.ReadLine();


 } catch (Exception e) {
     Console.Out.WriteLine(e.Message);
     Console.ReadLine();
 }

Above example has a dependency with Json.NET library for parsing response. You can use any library to parse your response but SharePoint Client SDK is not needed here.

All CSOM features are almost in REST API. For CRUD operation in list, you can have a look on my following article.

CRUD Operation to List Using SharePoint 2013 Rest API

5
  • I am trying the same code. But in line HttpWebResponse endpointResponse = (HttpWebResponse) endpointRequest.GetResponse(); giving me error for 403 forbidden. Passing the correct credential Commented Jan 2, 2017 at 5:37
  • Can you try without domain NetworkCredential cred = new System.Net.NetworkCredential("username", "password"); as your environment is SharePoint online. Commented Jan 2, 2017 at 5:39
  • Getting the same error. I guess problem with header? Do we need to pass any more header? Commented Jan 2, 2017 at 5:42
  • 1
    for GET request application/json;odata=verbose is enough. let me check. Commented Jan 2, 2017 at 5:43
  • Yes you need to pass more extra headers. Try it from fiddler at first. stackoverflow.com/q/7474077/1294606 Commented Jan 2, 2017 at 6:13

Your Answer

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

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