I'm trying to connect to a Sharepoint Client (Sharepoint List) within the Azure Function. However, when trying to connect, no matter what I do, I receive the exception:
'An invalid request URI was provided. The request URI must either be an absolute >URI or BaseAddress must be set'
Here I paste the code, please let me know if anyone could detect anything
string sharepointList = "https://someplace.sharepoint.com/teams/SomePlace/SomePlaceAppDev/SomeTeam/SomeRegion";
string sourceListName = "Some Sites";
string accessToken = await getSharePointToken(log, sharepointList);
var collListItem = GetSharePointSiteList(sharepointList, sourceListName, accessToken);
public static ClientContext GetClientContext(string siteUrl, string accessToken)
{
var ctx = new ClientContext(siteUrl);
ctx.ExecutingWebRequest += (s, e) =>
{
e.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + accessToken;
};
return ctx;
}
public static async Task<string> getSharePointToken(TraceWriter log, string siteUrl)
{
string clientID = ConfigurationManager.AppSettings["clientID"];
string clientSecret = ConfigurationManager.AppSettings["clientSecret"];
string tenantURL = ConfigurationManager.AppSettings["tenantURL"];
string tenantID = ConfigurationManager.AppSettings["tenantID"];
string spPrinciple = ConfigurationManager.AppSettings["spPrinciple"];
string spAuthUrl = ConfigurationManager.AppSettings["spAuthUrl"];
//public string TenantURL { get => tenantURL; }
HttpClient client = new HttpClient();
KeyValuePair<string, string>[] body = new KeyValuePair<string, string>[]
{
new KeyValuePair<string, string>("grant_type", "client_credentials"),
new KeyValuePair<string, string>("client_id", $"{clientID}@{tenantID}"),
new KeyValuePair<string, string>("resource", $"{spPrinciple}/{siteUrl}@{tenantID}".Replace("https://", "")),
new KeyValuePair<string, string>("client_secret", clientSecret)
};
var content = new FormUrlEncodedContent(body);
var contentLength = content.ToString().Length;
string token = "";
using (HttpResponseMessage response = await client.PostAsync(spAuthUrl, content))
{
if (response.Content != null)
{
string responseString = await response.Content.ReadAsStringAsync();
JObject data = JObject.Parse(responseString);
token = data.Value<string>("access_token");
}
}
return token;
}
private static ListItemCollection GetSharePointSiteList(string sourceSiteURL, string sourceListName, string accessToken)
{
ClientContext clientContext = GetClientContext(System.Net.WebUtility.UrlEncode(sourceSiteURL), accessToken);
Microsoft.SharePoint.Client.List oList = clientContext.Web.Lists.GetByTitle(sourceListName);
ListItemCollection collListItem = oList.GetItems(CamlQuery.CreateAllItemsQuery());
clientContext.Load(collListItem);
clientContext.ExecuteQuery();
clientContext.Dispose();
return collListItem;
}
As it was pointed out by Mel Gerats, the reason for that error was the use of an unassigned spAuthUrl variable
Once that variable was assigned, the error no longer would come up
(I would face other issue afterwards but the aforementioned error was gone)