I'm am troubled that this simple tasks has proven so difficult. I've tried several different techniques to check if a URL exists but none are acceptable. Using HttpWebRequest, it returns 403 on SharePoint site since the request is not authenticated.
Using the PnP library, it returns true even when a site exists. It seems to only look at the domain and not site collections or webs.
public static bool DoesSPOSiteExist(string url, NetworkCredential nc)
{
OfficeDevPnP.Core.AuthenticationManager authMgr = new OfficeDevPnP.Core.AuthenticationManager();
try
{
// Get the client context
using (var ctx = authMgr.GetSharePointOnlineAuthenticatedContextTenant(url, nc.UserName, nc.Password))
{
// Check if a web site exists at the specified full URL
if (ctx.WebExistsFullUrl(url))
{
//gets here even when you pass in a bad URL
Console.WriteLine(url + " - site exists");
}
return true;
}
}
catch (Exception ex)
{
return false;
}
}
Is there an alternative to see if a site exists using C# CSOM ?