I have a console app written in C#. I need to get some of the information from a SharePoint site. This instance of SharePoint is part of Office 365 (i.e. SharePoint Online).
My challenge is, I can't use the helper library. I have to use the REST-based APIs since I'm using .NET Core.
To begin, I registered my console app with Azure Active Directory. This console app is created in the same Azure Active Directory that my Office 365 environment is part of. I've also granted the "Read items in all site collections" delegated permission for the "Office 365 SharePoint Online" API to my console app.
In my situation, I have a console app sitting on a server. I've set up a test user with a username/password on my SharePoint tenant. I also have the Client ID for the console app I registered with Azure Active Directory and a Redirect URL.
At this time, I have some code that looks like this:
var accessToken = await GetToken(); // retrieves a token from Active Directory
using(var client = new HttpClient()) {
client
.DefaultRequestHeaders
.Clear();
client
.DefaultRequestHeaders
.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
client
.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var apiUrl = "https://[mySite].sharepoint.com/_api/web";
// how do I get the title of the site at the apiUrl variable?
}
I feel like I'm close since I'm getting an access token. I just can't seem to figure out how to get the title of the site. How do I get the title of a SharePoint Site from my C# code?
SharePoint endpoints follow the OData convention.
So, you can use $select
query options to specify the data that you want for a given web or list or fields etc.
So, in your case, you can simply modify your endpoint as below:
var apiUrl = "https://[mySite].sharepoint.com/_api/web?$select=Title";
In case, you want to get additional properties like description, logo, Webtemplate etc. you can append it as:
var apiUrl = "https://[mySite].sharepoint.com/_api/web?$select=Title,Description,
SiteLogoUrl,WebTemplate";
Reference - List of properties in SPO - Web object
Also, ensure that you have Have full control of all site collections
permissions checked in the Office 365 SharePoint Online
permission as below:
Fuller version of the code that I am using:
1) Create AuthenticationResponse.cs
class:
public class AuthenticationResponse
{
public string token_type { get; set; }
public string scope { get; set; }
public int expires_in { get; set; }
public int expires_on { get; set; }
public int not_before { get; set; }
public string resource { get; set; }
public string access_token { get; set; }
public string refresh_token { get; set; }
public string id_token { get; set; }
}
2) Use that in your code as below:
string userName = "[email protected]";
string password = "password";
List<KeyValuePair<string, string>> vals = new List<KeyValuePair<string, string>>();
string tenantName = "tenantName.OnMicrosoft.com";
string authString = "https://login.microsoftonline.com/" + tenantName;
string resource = "https://graph.microsoft.com";
AuthenticationContext authenticationContext = new AuthenticationContext(authString, false);
string clientId = "<client-id>";
string key = "<client-secret>";
vals.Add(new KeyValuePair<string, string>("client_id", clientId));
vals.Add(new KeyValuePair<string, string>("resource", resource));
vals.Add(new KeyValuePair<string, string>("username", userName));
vals.Add(new KeyValuePair<string, string>("password", password));
vals.Add(new KeyValuePair<string, string>("grant_type", "password"));
vals.Add(new KeyValuePair<string, string>("client_secret", key));
string url = string.Format("https://login.windows.net/{0}/oauth2/token", tenantName);
using (HttpClient httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Add("Cache-Control", "no-cache");
HttpContent content = new FormUrlEncodedContent(vals);
HttpResponseMessage hrm = httpClient.PostAsync(url, content).Result;
AuthenticationResponse authenticationResponse = null;
if (hrm.IsSuccessStatusCode)
{
Stream data = await hrm.Content.ReadAsStreamAsync();
DataContractJsonSerializer serializer = new
DataContractJsonSerializer(typeof(AuthenticationResponse));
authenticationResponse = (AuthenticationResponse)serializer.ReadObject(data);
var accessToken = authenticationResponse.access_token;
httpClient
.DefaultRequestHeaders
.Clear();
httpClient
.DefaultRequestHeaders
.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
httpClient
.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var apiUrl = "https://[mySite].sharepoint.com/_api/web?$select=Title";
}
}
The SharePoint REST API's web
resource includes a Title
property that represents the title for the Web site.
Calling:
GET http://<site url>/_api/web/title
Returns:
<d:Title xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"
xmlns:georss="http://www.georss.org/georss"
xmlns:gml="http://www.opengis.net/gml">Site Title Goes Here</d:Title>
or, assuming you've set the Accept
header to application/json
:
{
"odata.metadata":
"https://microsoft.sharepoint.com/sites/msw/_api/$metadata#Edm.String",
"value": "MSW"
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With