Let's say we have the following SharePoint working URL:
https://mywebsite.sharepoint.com/_layouts/15/Test.aspx
The following code works for determining if the URL works or not unless the user is authenticated with Azure AD access token
var request = NetHelper.CreateWebRequest(url,
allowAutoRedirect: true,
method: WebRequestMethods.Http.Head);
......
// Authentication
......
bool exists = false;
using (var response = request.GetResponseWithRetry())
{
if (response != null)
{
exists = response.StatusCode == HttpStatusCode.OK;
}
}
This method works when we have the CookieContainer
(username/password authentication) for authenticating. However, as per How to get cookies after obtaining Azure AD access token to SharePoint Online it's not possible to make WebRequest
work when Azure AD authentication is being used and we apply HttpRequestHeader.Authorization
header.
I tried to use CSOM library:
var csomFile = context.Web.GetFileByServerRelativeUrl(serverRelativeUrl);
context.Load(csomFile, f => f.Exists);
context.ExecuteQueryWithRetry();
bool exists = csomFile != null && csomFile.Exists;
However, this kind of code works only for actual files (I guess). It always returns False
for the URL https://mywebsite.sharepoint.com/_layouts/15/Test.aspx
.
So, my question is - Is there a way to determine if the URL exists using CSOM library, assuming we already have the authenticated ClientContext
object (which can use either CookieContainer
or HttpRequestHeader.Authorization
header).