2

I am trying to download file from Autodesk BIM360 Doc (https://docs.b360.autodesk.com) with the Forge API so the files can be then afterward archieved to our local storage.

I have managed to download any files from "Project Files" folder using the data management API https://forge.autodesk.com/en/docs/data/v2/reference/http/projects-project_id-versions-version_id-GET/, with which i can get the storage id under data.relationships.storage.data.id.

however with the same API i cannot get the storage Id when querying files under "Plan" folder,

So is there any way with Forge API we can download a file from Plan folder? any help is appreciated.

Kevin Zhu
  • 35
  • 3

2 Answers2

1

The item listed in the Plan folder is a type of items:autodesk.bim360:Document, this type item won't have storage attribute shown in its responses of GET versions/:version_id and GET items/:item_id directly.

To obtain the physical file location, you should call GET versions/:version_id/relationships/refs instead, see here for the similar thread: Download a Document with Autodesk API

Update for copied item

While accessing the relationship data of version of the copied item via GET versions/:version_id/relationships/refs, you would see a data attribute telling the relationship between the copied item and the source item with my experience:

"data": [
    {
        "type": "versions",
        "id": "urn:adsk.wipprod:fs.file:vf.34Xvlw1jTcSQ_XkIVh07cg?version=2",
        "meta": {
            "refType": "derived",
            "fromId": "urn:adsk.wipprod:fs.file:vf.34Xvlw1jTcSQ_XkIVh07cg?version=2",
            "fromType": "versions",
            "toId": "urn:adsk.wipprod:fs.file:vf.y3L7YbfAQJWwumMgqjJUxg?version=1",
            "toType": "versions",
            "direction": "to",
            "extension": {
                "type": "derived:autodesk.bim360:CopyDocument",
                "version": "1.0",
                "schema": {
                    "href": "https://developer.api.autodesk.com/schema/v1/versions/derived:autodesk.bim360:CopyDocument-1.0"
                },
                "data": {}
            }
        }
    }
],  

Afterward, you have to access the version relationship dat of the fromId via calling GET versions/:version_id/relationships/refs.

In this case, it's {PROJ_ID}/versions/urn:adsk.wipprod:fs.file:vf.34Xvlw1jTcSQ_XkIVh07cg%3Fversion=2/relationships/refs, then you will see the storage attribute inside the response with my investigation.

Eason Kang
  • 6,155
  • 1
  • 7
  • 24
  • Thanks for the swift answer, API GET versions/:version_id/relationships/refs already tried, however no lucky. the type of the item is **derived:autodesk.bim360:CopyDocument**, and there's no storage node returned – Kevin Zhu Mar 25 '19 at 14:10
  • Could you share more details about the `derived:autodesk.bim360:CopyDocument`? It seems that I haven't seen this before. How did you create this type of item? – Eason Kang Mar 26 '19 at 02:13
  • When a document is moved or copied to a different folder under Plan library, w get a different item type, eg. **derived:autodesk.bim360:CopyDocument** or **derived:autodesk.bim360:FileToDocument**, in our case we used the approval workflow of the BIM360 Docs to copy approved files into an stage folder, and thus what we get data.meta.extension.type which equals **derived:autodesk.bim360:CopyDocument** – Kevin Zhu Mar 26 '19 at 03:11
  • I have updated the answer, please have a try. It works from my side as expected. Cheers – Eason Kang Mar 28 '19 at 02:31
  • Thanks for the update. Yes, this solves most of the case. – Kevin Zhu Mar 28 '19 at 05:50
0

Just in case anyone else run into the same issue, i post my code with which i finally managed to get the file storage information. However please feel free to suggest other approaches than iteration to the full relationship trees.

internal static ForgeFileInfo getItemVersion(string token, string projectID, string versionID)
    {
        ForgeFileInfo forgeFileInfo = new ForgeFileInfo();

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        versionApi.Configuration.AccessToken = token;
        var version = versionApi.GetVersion(projectID, versionID);
        string fileType = version.data.attributes.extension.type;
        switch (fileType) {
            case "versions:autodesk.bim360:File":
                //File from Project File library or is regual file
                forgeFileInfo.FileName = version.data.attributes.displayName;
                forgeFileInfo.FileLocation = version.data.relationships.storage.meta.link.href;
                forgeFileInfo.StorageId = version.data.relationships.storage.data.id;
                return forgeFileInfo;
            case "versions:autodesk.bim360:Document":
                //File from Plan Library
                var versionRelationship=versionApi.GetVersionRelationshipsRefs(projectID, versionID);

                // the GET Relationship has data node where we can get the related document
                var relationshipData = new DynamicDictionaryItems(versionRelationship.data);
                // let's start iterating the relationship DATA
                foreach (KeyValuePair<string, dynamic> relationshipItem in relationshipData)
                {
                    //Have to loop until we found "derived:autodesk.bim360:FileToDocument"
                    var relationType = relationshipItem.Value.meta.extension.type;
                    var relation = relationshipItem.Value.meta.direction;
                    if ("derived:autodesk.bim360:FileToDocument".Equals(relationType))
                    {
                        if ("to".Equals(relation))
                        {
                            //Go up stream
                            return getItemVersion(token, projectID, relationshipItem.Value.id);
                        }
                    }
                    else if ("derived:autodesk.bim360:CopyDocument".Equals(relationType))
                    {
                        if ("to".Equals(relation))
                        {
                            //Go up stream
                            return getItemVersion(token, projectID, relationshipItem.Value.id);
                        }
                        continue;
                    }               
                }
                break;
        }
        return forgeFileInfo;
    }
Kevin Zhu
  • 35
  • 3