0

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).

2
  • Would you accept answers using other frameworks? PnP.Core SDK can solve this issue using SiteCollectionManager extension and SiteExistsAsync method.
    – MagnusK
    Commented Nov 28 at 7:42
  • We can consider other frameworks, since CSOM does not seem to have a possibility of doing this. At this moment we've just disabled the feature that depends on this. Commented Dec 2 at 12:15

0

Your Answer

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