1

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 ?

1

You're using the same variable url that you used to instantiate the context. So of course the web is going to exist.

If you set the url variable to a junk value, you won't get a valid context. It'll either be null, or it'll throw an exception, depending on the implementation of GetSharePointOnlineAuthenticatedContextTenant()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.