In the context of a project, I need to retrieve information from a SharePoint calendar (times, events, etc...):
First image:
Second Image:
It's been 2 days that I've been trying to understand how SharePoint Online APIs work and I can't do it.
What I understand is that there are 3 different methods:
- SCOM ;
- SharePoint REST API v1
- SharePoint REST API v2
I've tested few implementation but it never works:
#1 implementation:
using Microsoft.SharePoint.Client;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
class Program
{
public static JToken GetList(Uri webUri, ICredentials credentials, string listTitle)
{
using (var client = new WebClient())
{
client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
client.Credentials = credentials;
client.Headers.Add(HttpRequestHeader.ContentType, "application/json;odata=verbose");
client.Headers.Add(HttpRequestHeader.Accept, "application/json;odata=verbose");
var endpointUri = new Uri(webUri, string.Format("/_api/web/lists/getbytitle('{0}')", listTitle));
var result = client.DownloadString(endpointUri);
var t = JToken.Parse(result);
return t["d"];
}
}
static void Main(string[] args)
{
var webUri = new Uri("https://*********.sharepoint.com");
const string userName = "**********";
const string password = "*********";
var securePassword = new SecureString();
foreach (var c in password)
{
securePassword.AppendChar(c);
}
var credentials = new SharePointOnlineCredentials(userName, securePassword);
var list = GetList(webUri, credentials, "Contacts");
//print List title
Console.WriteLine(list["Title"]);
}
}
}
#2 Implementation:
using System;
using Microsoft.SharePoint.Client;
using System.Configuration;
using System.Security;
using System.Xml;
using TestSharePointRetrieveList.com.sharepoint.*******;
using System.Net;
namespace TestSharePointRetrieveList
{
class Program
{
private static Web _web;
private static ClientContext _context;
public class VersionsHandler
{
private const string ListsServiceUrl = "/_vti_bin/Lists.asmx";
private Lists lists = null;
public string TenantUrl { get; set; }
public String User { get; set; }
public String Password { get; set; }
public string Domain { get; set; }
public string MySiteHost { get; set; }
private Lists _lists
{
get
{
if (lists == null)
{
if (!String.IsNullOrEmpty(TenantUrl))
{
this.lists = new Lists();
lists.Url = TenantUrl + ListsServiceUrl;
lists.UseDefaultCredentials = false;
lists.CookieContainer = new CookieContainer();
lists.CookieContainer.Add(GetFedAuthCookie(CreateSharePointOnlineCredentials()));
return lists;
}
else if (this.User.Length > 0 && this.Password.Length > 0 && this.Domain.Length > 0 && this.MySiteHost.Length > 0)
{
this.lists = new Lists();
lists.Url = this.MySiteHost + ListsServiceUrl;
NetworkCredential credential = new NetworkCredential(this.User, this.Password, this.Domain);
CredentialCache credentialCache = new CredentialCache();
credentialCache.Add(new Uri(this.MySiteHost), "NTLM", credential);
lists.Credentials = credentialCache;
return lists;
}
else
{
throw new Exception("Please specify an authentication provider or specify domain credentials");
}
}
else
{
return this.lists;
}
}
}
public XmlNode GetVersionCollection(string listId, string itemId, string fieldName)
{
return _lists.GetVersionCollection(listId, itemId, fieldName);
}
private SharePointOnlineCredentials CreateSharePointOnlineCredentials()
{
var spoPassword = new SecureString();
foreach (char c in Password)
{
spoPassword.AppendChar(c);
}
return new SharePointOnlineCredentials(User, spoPassword);
}
private Cookie GetFedAuthCookie(SharePointOnlineCredentials credentials)
{
string authCookie = credentials.GetAuthenticationCookie(new Uri(this.TenantUrl));
if (authCookie.Length > 0)
{
return new Cookie("SPOIDCRL", authCookie.TrimStart("SPOIDCRL=".ToCharArray()), String.Empty, new Uri(this.TenantUrl).Authority);
}
else
{
return null;
}
}
}
static void Main(string[] args)
{
_context = new ClientContext(ConfigurationManager.AppSettings["TenantUrl"]);
string userName = ConfigurationManager.AppSettings["AdminUser"];
var passWord = new SecureString();
foreach (char c in ConfigurationManager.AppSettings["Password"].ToCharArray())
{
passWord.AppendChar(c);
}
_context.Credentials = new SharePointOnlineCredentials(userName, passWord);
_web = _context.Web;
var list = _context.Web.Lists.GetByTitle("Retention Rules");
var query = new CamlQuery();
query.ViewXml =
@"<View Scope='RecursiveAll'>
<Query>
<Where><Eq><FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value></Eq></Where>
</Query>
<RowLimit>5000</RowLimit>
</View>";
var listItems = list.GetItems(query);
_context.Load(_web);
_context.Load(list);
_context.Load(listItems);
_context.ExecuteQuery();
var versionsHandler = new VersionsHandler();
versionsHandler.User = userName;
versionsHandler.Password = ConfigurationManager.AppSettings["Password"];
versionsHandler.TenantUrl = ConfigurationManager.AppSettings["TenantUrl"];
if (listItems.Count == 0) throw new ArgumentException("No list items");
var listId = list.Id.ToString();
var itemId = listItems[0].Id.ToString();
var versionNodes = versionsHandler.GetVersionCollection(listId, itemId, "Title");
foreach (XmlNode node in versionNodes.ChildNodes)
{
if (node.Attributes != null)
{
var title = node.Attributes["Title"].Value;
var modified = node.Attributes["Modified"].Value;
Console.WriteLine("Modified: {0}, Title: {1}", modified, title);
}
}
Console.WriteLine("Hit any key to end.");
Console.ReadKey();
}
}
}
I always got the same error, but I do not understand why:
An unhandled exception of type 'System.Net.WebException' occurred in System.dll Additional information: Unable to connect to the remote server
{"No connection could be made because the target machine actively refused it 127.0.0.1:8888"}
I don't understand what I'm doing bad, someone can help/advise me please? :)
Thank you in advance.

