Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 528743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:01:06+00:00 2026-05-13T09:01:06+00:00

I have a webdav function listed below: The behavior is completely unexpected…. When I

  • 0

I have a webdav function listed below:

The behavior is completely unexpected….

When I first run the function and pass a URL to a resource (folder in sharepoint) that does not exist, I get a 404 which is expected. I then use another function to create the resource using THE SAME credentials as in this method. No problems yet…

However on 2nd run, after the resource has been created – when I check if resource exists, now I get a 401.

Whats important to note here is that the same credentials are used to check for 401 and create folder, so clearly the credentials are fine…

So it must be something else…. All I want to do is check if a resource exists in SharePoint…. any ideas how to improve this function? Or any theory as to why its giving this 401…

 private bool MossResourceExists(string url)
        {
            var request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "HEAD";

            // Create a new CredentialCache object and fill it with the network
            // credentials required to access the server.
            var myCredentialCache = new CredentialCache();
            if (!string.IsNullOrEmpty(this.Domain ))
            {
                myCredentialCache.Add(new Uri(url),
               "NTLM",
               new NetworkCredential(this.Username , this.Password , this.Domain )
               );
            }
            else
            {
                myCredentialCache.Add(new Uri(url),
               "NTLM",
               new NetworkCredential(this.Username , this.Password )
               );
            }

            request.Credentials = myCredentialCache;

            try
            {
                request.GetResponse();
                return true;

            }
            catch (WebException ex)
            {
                var errorResponse = ex.Response as HttpWebResponse;

                if (errorResponse != null)
                    if (errorResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        return false;
                    }
                    else
                    {
                        throw new Exception("Error checking if URL exists:" + url + ";Status Code:" + errorResponse.StatusCode + ";Error Message:" + ex.Message ) ;
                    }
            }
            return true;
        }

The only clue I have is that when using http://mysite.com/mydoclib/mytoplevelfolder it works…. any sub folders automatically give 401’s….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T09:01:07+00:00Added an answer on May 13, 2026 at 9:01 am

    The thing is that you can’t pass the whole url that includes folders to the CredentialCache.Add() method.

    For example:
    http://MyHost/DocumentLibrary/folder1/folder2 will not work as an Uri to the Add() method, but
    http://MyHost/DocumentLibrary/ will work.

    I would guess that the lack of permissioning capabilities on folder level in SharePoint is the reason for this. Or the way that otherwise SharePoint handles folders.

    What you can do is to separate the parameters in your method to accept a base url (including document libraries / lists) and a folder name parameter.
    The CredentialCache gets the base url and the request object gets the full url.

    Another way is to use the

    request.Credentials = System.Net.CredentialCache.DefaultCredentials;
    

    credentials instead. And, if necessary, do an impersonation if you want to use another account than the executing one.

    A third variation is to try with authentication type set to Kerberos instead of NTLM.

    Here is my test code. I am able to reproduce the problem if I replace the problem with your code, and this code works for me.

    static void Main(string[] args)
    {
      bool result = MossResourceExists("http://intranet/subtest/content_documents/", "testfolder/testfolder2");
    }
    
    private static bool MossResourceExists(string baseUrl, string folder)
    {
      string completeUrl = baseUrl + folder;
    
      var request = (HttpWebRequest)WebRequest.Create(completeUrl);
      request.Method = "HEAD";
    
      // Create a new CredentialCache object and fill it with the network
      // credentials required to access the server.
      var myCredentialCache = new CredentialCache();
      if (!string.IsNullOrEmpty(Domain))
      {
        myCredentialCache.Add(new Uri(baseUrl),
       "NTLM",
       new NetworkCredential(Username, Password, Domain)
       );
      }
      else
      {
        myCredentialCache.Add(new Uri(baseUrl),
       "NTLM",
       new NetworkCredential(Username, Password)
       );
      }
    
      request.Credentials = myCredentialCache;
      //request.Credentials = System.Net.CredentialCache.DefaultCredentials;
    
      try
      {
        WebResponse response = request.GetResponse();
        return true;
      }
      catch (WebException ex)
      {
        var errorResponse = ex.Response as HttpWebResponse;
    
        if (errorResponse != null)
          if (errorResponse.StatusCode == HttpStatusCode.NotFound)
          {
            return false;
          }
          else
          {
            throw new Exception("Error checking if URL exists:" + completeUrl + ";Status Code:" + errorResponse.StatusCode + ";Error Message:" + ex.Message);
          }
      }
      return true;
    }
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 306k
  • Answers 306k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Presumably you have control over the page memberresults.php? Then you… May 13, 2026 at 9:17 pm
  • Editorial Team
    Editorial Team added an answer For a table that large you will be wanting to… May 13, 2026 at 9:17 pm
  • Editorial Team
    Editorial Team added an answer Use libRETS for your interface, then transcribe the RETS class… May 13, 2026 at 9:17 pm

Related Questions

I am using webDAV and .Net 2.0 to gather information on an email account
This is my first jQuery script, which works great in Firefox and Chrome, but
I'm writing a CMS application in PHP and one of the requirements is that
Good day folks, I'm in a need of a bit of guidance. Basically, I'm
I have a PHP application which connects to Microsoft Exchange server to retrieve 'Contacts'

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.