GET THE APP
Download to iPhone Download to Android
AutoCAD 360
AutoCAD WS APIs – C# Client

Webdav Client

The purpose of this tutorial is to show how you can use the WebdavClient C# class to perform various operations on files and folders located in an AutoCAD WS account.

Using this C# class you will be able to –

  • Connect to an AutoCAD WS account
  • Query the account file and folder structure.
  • Perform different file operations such as copy, delete, rename, create folder.
  • Upload and download files from the account.
  • Query file metadata such as last modified, permission and thumbnails.
  • Launch a drawing in the WS web editor.

WebdavClient class uses the Webdav protocol to communicate with the Autocad WS service.

The WebdavClient C# class relies on the standard Webdav protocol to communicate with the Autocad WS service. This http based industry protocol is supported across multiple languages such as C++, C#, Java and JavaScript. Click here for a tutorial on accessing AutoCAD WS using JavaScript.

Click here to download the C# sample application source code.


Constructor

The constructor of the WebdavClient class expects three arguments:

  1. host: The Webdav server addres. For example, if you want to connect to Autocad WS server, use: ‘https://dav.autocadws.com’
  2. username: the username of your AutoCAD WS account.
  3. password: the password of your account.

The following code creates new WebdavClient class, connecting to Autocad WS Webdav server, with the username ‘someuser’ and the password ’123456′.

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

 

PROPFIND
The PROPFIND method is used to retrieve info about files and folders in an AutoCAD WS account.

The Propfind function receives two arguments:

  1. remoteFilePath: relative path to the folder/file for which we wish to retrieve information.
  2. depth: indicates 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.

The following code sends PROPFIND request to the server, requesting info for all files/folders residing directly in ‘/Site’ folder.

//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 subscriberd the function PropfindResponse to the PropfindComplete event. When Propfind is finished, WebdavClient will call the PropfindResponse. This function must have the following signature:

//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)

 

We will show an implementation of the PropfindResponse function that prints the names of all items retrieved to the console. We could get the names from the WebdavItemInfo objects, but we will get this info from the full response XML, to show how this XML can be parsed.
Before we look at the PropfindResponse implementation, we should take a look at the response from the Webdav server. The server return an XML which holds info about the files/folders. The following is an example of such xml:



  
    /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
      
    
  

As you can see, each files/folder returned in ‘d:response’ tag. In this case, the server returned information about two files, ‘filea.dwg’ and ‘fileb.dwg’. For each file, the server states the creation date and its name.

The following code shows the implementaiton of the PropfindResponse function:

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);
}

If the account using username ‘someuser’ has the folder ‘Site’ and the files ‘filea.dwg’ and ‘fileb.dwg’ in the folder, the user will see the names of the files printed to the console:

filea.dwg
fileb.dwg

 

COPY. MOVE, DELETE, UPLOAD
Copy, Move, Delete and Upload are called in the same way as the PROPFIND method, differed in the parameters passed.
You should remember to subscribe a callback function to the appropriate event(DeleteComplete, CopyComplete, MoveComplete, UploadComplete). There is no response to parse in those cases but using the callback, you will be notified when the operation finished and whether is was successfull.

The following code:

  1. copies ‘/Site/filea.dwg’ to /OtherSite/fileb.dwg’
  2. moves ‘/Site/filec.dwg’ to /OtherSite/filed.dwg’
  3. deletes ‘/Site/filee.dwg’
  4. uploads 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");

The following code 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 drawing in Autocad WS

To open a drawing simply invoke the OpenDrawing method of the client, passing the path of the drawing:

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

Download the C# sample application source code.