Last active
October 29, 2019 17:38
-
-
Save t3knoid/7a2475b12245ea36e1abc1c1c83dbbd9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using kCura.Relativity.Client.DTOs; | |
using DTOs = kCura.Relativity.Client.DTOs; | |
using Relativity.Services.ServiceProxy; | |
namespace RelativityConnectTest | |
{ | |
public class WinAuth | |
{ | |
public string Username { get; set; } | |
public string Password { get; set; } | |
public string RestURL { get; set; } | |
public string ServicesURL { get; set; } | |
public WinAuth() | |
{ } | |
public static Relativity.Services.ServiceProxy.ServiceFactory GetServiceFactory(WindowsCredentials cred) | |
{ | |
Uri RestURI = new Uri(cred.RestURL); | |
Uri servicesURI = new Uri(cred.ServicesURL); | |
ServiceFactorySettings settings; | |
if (String.IsNullOrEmpty(cred.Username.Trim())) | |
{ | |
// Use Windows integrated authentication | |
IntegratedAuthCredentials credentials = new IntegratedAuthCredentials(); | |
settings = new ServiceFactorySettings(servicesURI, RestURI, credentials); | |
} | |
else | |
{ | |
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(cred.Username, cred.Password); | |
settings = new ServiceFactorySettings(servicesURI, RestURI, credentials); | |
} | |
return new ServiceFactory(settings); | |
} | |
public kCura.Relativity.Client.IRSAPIClient GetRSAPIClient(WindowsCredentials cred) | |
{ | |
var serviceFactory = GetServiceFactory(cred); | |
kCura.Relativity.Client.IRSAPIClient proxy = serviceFactory.CreateProxy<kCura.Relativity.Client.IRSAPIClient>(); | |
return proxy; | |
} | |
public List<NameValueObject> RetrieveRelativityWorkspaces(string workspaceName = "") | |
{ | |
List<NameValueObject> workspaces = new List<NameValueObject>(); | |
try | |
{ | |
WindowsCredentials windowsCred = new WindowsCredentials | |
{ | |
Username = this.Username, | |
Password = this.Password, | |
RestURL = this.RestURL, | |
ServicesURL = this.ServicesURL | |
}; | |
using (var client = GetRSAPIClient(windowsCred)) | |
{ | |
NameValueObject currentWorkspaceInfo = new NameValueObject(); | |
//Query for all workspace and load them in the dropdown. | |
kCura.Relativity.Client.DTOs.Query<kCura.Relativity.Client.DTOs.Workspace> query = new kCura.Relativity.Client.DTOs.Query<kCura.Relativity.Client.DTOs.Workspace>(); | |
query.Fields.Add(new kCura.Relativity.Client.DTOs.FieldValue("Name")); | |
query.Fields.Add(new kCura.Relativity.Client.DTOs.FieldValue("Status")); | |
//Send the query and receive results. | |
kCura.Relativity.Client.DTOs.QueryResultSet<kCura.Relativity.Client.DTOs.Workspace> results = client.Repositories.Workspace.Query(query); | |
if (!results.Success) | |
{ | |
return workspaces; | |
} | |
else if (!results.Results.Any()) | |
{ | |
//No Workspaces present in the given Relativity Server | |
Console.WriteLine("No Workspace condition found"); | |
return workspaces; | |
} | |
else | |
{ | |
//Loop all found workspaces | |
foreach (kCura.Relativity.Client.DTOs.Result<kCura.Relativity.Client.DTOs.Workspace> result in results.Results) | |
{ | |
kCura.Relativity.Client.DTOs.Choice statusChoice = result.Artifact.Fields.Get("Status").Value as kCura.Relativity.Client.DTOs.Choice; | |
if (workspaceName.Length > 0) | |
{ | |
if (result.Artifact.Fields.Get("Name").Value.ToString() == workspaceName) | |
{ | |
currentWorkspaceInfo = new NameValueObject() { Name = result.Artifact.Fields.Get("Name").Value.ToString(), Value = result.Artifact.ArtifactID }; | |
return workspaces; | |
} | |
} | |
else | |
{ | |
//If the workspace is active and passes the set up filters in the Relativity admin settings, it is added to the list to show | |
if (statusChoice.Name.Equals("Active") && result.Artifact.Fields.Get("Name").Value.ToString().Contains("")) | |
workspaces.Add(new NameValueObject() { Name = result.Artifact.Fields.Get("Name").Value.ToString(), Value = result.Artifact.ArtifactID }); | |
} | |
} | |
workspaces.Sort(new CompareNameValueObjectByName()); | |
//Adds a first entry at position 0 to prompt the user to select a workspace | |
workspaces.Insert(0, new NameValueObject() { Name = "Select a Workspace", Value = 0 }); | |
return workspaces; | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
} | |
public class WindowsCredentials | |
{ | |
public string RestURL { get; set; } | |
public string ServicesURL { get; set; } | |
public string Username { get; set; } | |
public string Password { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment