Support Center

Working With C# WebDAV Client

Last Updated: Feb 19, 2014 06:27PM UTC

Use this tutorial to perform various operations on files and folders in AutoCAD 360 using the C# client.

 

After completing this tutorial, you would be able to:

  • Connect to an AutoCAD 360 account.
  • Query the file and folder directory for an account.
  • Perform various operations like copy, move, delete, and create folder.
  • Query file metadata.
  • Launch a drawing in the AutoCAD 360 editor.

​The C# Webdav client class relies on the standard WebDAV protocol to communicate with the AutoCAD 360 service.
 

Click here to download the C# sample application source code

 
Connecting to AutoCAD 360 Server

The constructor of the Webdav client class expects three arguments:

  • Host – specifies the WebDAV server address. For example, if you want to connect to AutoCAD 360 server, use: https://dav.autocadws.com.
  • Username – specifies your AutoCAD 360 account.
  • Password – specifies your AutoCAD 360 account password.
 

This sample code creates a new WebDAV client, connecting to the AutoCAD 360 server, with the username 'someuser' and password '123456':

 
WebDAVClient client = new WebdavClient("https://dav.autocadws.com", "someuser", "123456");
 
Querying Files and Folders

Use the PROPFIND method to retrieve information about files and folders in AutoCAD 360.

 

PROPFIND / <handle>

 

The PROPFIND method receives two arguments:

  • remoteFilePath – specifies the relative path to the folder/file for which we wish to retrieve information.
  • depth – specifies whether the method is to be applied only to the resource (Depth = "0″), to the resource and its internal members only (Depth = "1″), or the resource and all its members (Depth = "infinity").
 

In addition, remember to subscribe your callback function to the PropfindComplete event.

 

Use this sample code, which sends a request to the AutoCAD 360 server for retrieving information about all files and folders within the Site directory:

 
//subscribe to the PropfindComplete event
client.PropfindComplete += new PropfindCompleteDel(PropfindResponse);
 
// request properties of all files/folders residing directly under the 'Site' folder.
client.Propfind('/Site', 1);

 

In this example, we subscribe the function PropfindResponse to the PropfindComplete event. When Propfind is finished, Webdav client calls the PropfindResponse.

 
//responseStr is the full XML returned by the server.
//WebdavItemInfo holds partial information about each file/folder retrieved, such as its name,
//relative path and whether it is a folder or a file.
public void PropfindResponse(String folderHref, bool requestSuccessfull, int statusCode,
                                       String responseStr, List files)

 

The following example shows a sample XML that the WebDAV server returns containing all the information about the files and folders.

 
 /Site/filea.dwg
 
      HTTP/1.1 200 OK
 
        2011-12-01T17:42:21-08:00
        filea.dwg
 
    /Site/fileb.dwg
 
      HTTP/1.1 200 OK
 
        2011-12-01T17:42:21-08:00
        fileb.dwg

The files/folders are returned in d:response tag. In this example, the server returns information about two files, 'filea.dwg' and 'fileb.dwg'. For each file, the server states the creation date and its name.

The following code demonstrates an implementation of the PropFindResponse function that prints the names of all items retrieved to the console.

public void PropfindResponse(String folderHref, bool requestSuccessfull, int statusCode,
                                       String responseStr, List files)
{
    XmlDocument xml = new XmlDocument();
    xml.LoadXml(responseStr);
 
    XmlNamespaceManager xmlNsManager = new XmlNamespaceManager(xml.NameTable);
    xmlNsManager.AddNamespace("d", "DAV:");
    xmlNsManager.AddNamespace("WSNS", "WS");
 
    //each child node is a 'd:response' node
    foreach (XmlNode node in xml.DocumentElement.ChildNodes)
    {
        XmlNode propStatNode = node.SelectSingleNode("d:propstat", xmlNsManager);
        XmlNode propsNode = propStatNode.SelectSingleNode("d:prop", xmlNsManager);
 
        XmlNode nameNode =  propsNode.SelectSingleNode("d:displayname", xmlNsManager); 
 
        String name = HttpUtility.HtmlDecode(nameNode.InnerXml);
 
        Console.Out.WriteLine(name);
 

In this example, the user will see these file names printed to the console:

 
filea.dwg
fileb.dwg
 
Copying, Moving, Deleting, and Uploading

Use the PROPFIND method to copy, move, delete, and upload files.

 

Using this sample code, you can:

  • Copy /Site/filea.dwg to /OtherSite/fileb.dwg
  • Move /Site/filec.dwg to /OtherSite/filed.dwg
  • Delete /Site/filee.dwg
  • Upload a local file from c:\fileg.dwg to /Site/fileg.dwg
 
//subscribe to theDeleteComplete , CopyComplete  and MoveComplete events
client.DeleteComplete -= new DeleteCompleteDel(FSOpFinished);
client.CopyComplete -= new CopyCompleteDel(FSOpFinished);
client.MoveComplete -= new MoveCompleteDel(FSOpFinished);
client.UploadComplete -= new UploadCompleteDel(FSOpFinished);
 
var overwrite = true;
 
client.Copy("/Site/filea.dwg", "/OtherSite/fileb.dwg", overwrite);
client.Move( "/Site/filec.dwg", "/OtherSite/filed.dwg'" overwrite);
client.Delete("/Site/filee.dwg");
client.Upload("c:/fileg.dwg", "Site/fileg.dwg");
 

You should subscribe a callback function to the appropriate event (DeleteCompleteCopyCompleteMoveCompleteUploadComplete). In these cases, there is no response to parse, but using the callback you will be notified when the operation is finished and whether it is was successful.

 

The following example shows the FSOpFinished function that prints to console the message 'success' in case the request ended successfully or the message 'error' with the status code in case of an error:

 
public void FSOpFinished(bool requestSuccessfull, int statusCode, String responseStr)
{
    if(requestSuccessfull)
    {
        Console.Out.WriteLine("success");
    }
    else
    {
        Console.Out.WriteLine("error: " + statusCode);
    }
}
 
Opening a Drawing in AutoCAD 360
 

Use the OpenDrawing method of your WebDAV client by passing the path of the drawing for opening the drawing.

 
client.OpenDrawing("/Site/filea.dwg");

Contact Us

Recent Discussions

Ajax-loader-small
https://cdn.desk.com/
false
desk
Loading
seconds ago
a minute ago
minutes ago
an hour ago
hours ago
a day ago
days ago
about
true
Invalid characters found
/customer/en/portal/articles/autocomplete
There was an error contacting Get Satisfaction
View All
0
discussions
replies
Questions
Ideas
Problems
Praise