Trimble Connect User Forum

 View Only

 Trimble Connect API (getting model object attachements)

Paulius Vasiliauskas's profile image
Paulius Vasiliauskas posted 03-19-2021 03:29
Hi ,

I have a bug in Trimble Connect web application, somehow when i attached pdf to objects they got attached multiple times.
And now when opening model in web browser it crashes the browser.

So i wonder maybe API is the way to go to fix this model.

As i know some C# i went to  DesktopAPI
but soon realized that this API doesnt have API calls to work with attachements.
Then i looked for aanother way: Web REST API , i saw some possible solutions:
https://app.connect.trimble.com/tc/static/apidoc.html#object-link-to-entity

Now i got stuck at launching some of the basic REST API commands:
I found a method that would maybe do the trick InvokeAPIAsyn(),
but as i lack of knowledge with REST API and all those HTTP requests, couldn't figure the way to do that.


Question is , maybe someone have done it before and maybe can share some code sheet how to launch some basic REST API command like

/projects/{projectId}


as far my code in c# looks like:
var clientCreds = new ClientCredential(Config.ConsumerKey, Config.ConsumerSecret)
                {
                    RedirectUri = new Uri(Config.RedirectURL),
                };

                var authCtx = new AuthenticationContext(clientCreds)
                {
                    AuthorityUri = new Uri(AuthorityUris.ProductionUri),
                };

                var provider = new AuthCodeCredentialsProvider(authCtx)
                {
                    AuthenticationRequest = new InteractiveAuthenticationRequest()
                };

                var config = new TrimbleConnectClientConfig { ServiceURI = new Uri(Config.ServiceUri) };

                config.RetryConfig = new RetryConfig { MaxErrorRetry = 1 };

                var client = new TrimbleConnectClient(config, provider);

                await client.InitializeTrimbleConnectUserAsync();


                client.InvokeApiAsync(......);​
Dominik-Sergio Martilotti's profile image
Dominik-Sergio Martilotti

Hi Paulius,

currently I'm trying to implement a REST API call for Trimble Connect Projects but still running into a lot of errors. I found your post here and I was wondering whether you solved your problem

and maybe you can share the code sheet for your solution?

Regards,

Dominik

Paulius Vasiliauskas's profile image
Paulius Vasiliauskas

Hi Dominik,

yes i solved my problem, with not rest api calls but with c# wrapper of it

the code is not refactored as i was prototyping its a mess, but 
if i remember correctly i was looking for a specific link id cause from logs i saw that it was crashing the webbrowser, so when i found its id i looped all links and deleted it , that fixed the problem 
rest of the code is here:

the usings are:

using Trimble.Identity;
using Trimble.Identity.OAuth.AuthCode;
using MO = Trimble.Connect.Client.Models;



var projects = await client.GetProjectsAsync();

var myProject = projects.Where(a => a.Name == "YOUR-PROJECT-NAME").FirstOrDefault();
var projectClient = await client.GetProjectClientAsync(myProject);

                var linkParams = new Dictionary<string, string>() { { "id", "FYCWjs_SQ24" },
                                                                    { "detail", "true" } };

                IQueryResult<MO.EntityLink> links = await projectClient.Links.GetAllAsync(linkParams);

                // Source - model object , target - file
                var groupedLinks = links
                    .SelectMany(src => src.Source.Elements, (link, src) => new { link, src })
                    .GroupBy(e => ((MO.ModelElementSource)e.src).Identifier);


                ObjectLinks objects = new ObjectLinks();
                Dictionary<string, List<string>> duomenys = new Dictionary<string, List<string>>();

                foreach (var item in groupedLinks)
                {
                    var Listas = new List<string>();

                    foreach (var i in item)
                    {
                        Listas.Add(i.link.Identifier);
                    }

                    duomenys.Add(item.Key, Listas);
                }

                objects.Objects = duomenys;

                JsonSerializer serializer = new JsonSerializer();

                using (StreamWriter sw = new StreamWriter(@"C:\Users\USERNAMEHERE\Desktop\data.json"))
                using (JsonWriter writer = new JsonTextWriter(sw))
                {
                    serializer.Serialize(writer, objects);
                }

                string stop = string.Empty;

                // DELETE LINKS BY ID
                foreach (var item in links)
                {
                    //await projectClient.Links.DeleteAsync(item.Identifier);
                }

                // TODO : Probleminis ID
                //await projectClient.Links.DeleteAsync("9gxqPfgxXjo");
                //await projectClient.Links.DeleteAsync("hu1HHdHL6KA");