tags:

views:

77

answers:

1

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  A: 

Hi.

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.

Magnus Johansson
+1 for the info on passing credentials, however I tried to reroute the credentials, and still the problem persists.
JL
JL, I was able to reproduce with your code. I have posted my modified code. Does it work for you?
Magnus Johansson
very interesting indeed, I decided to go with PROPFIND instead, however your answer did provide insight into the problem, so accepted answer! thanks again.
JL