Giter Club home page Giter Club logo

sugarrestsharp's Introduction

Being RESTful with SugarCRM/SuiteCRM in .NET C#

SugarRestSharp is a .NET C# SugarCRM CE 6.x/SuiteCRM 7.x API client. SugarCRM RestSharp is a RestSharp implementation. It is a Restful CRUD client that implements the SugarCRM module Create, Read, Update and Delete functionalities.

SugarRestSharp implements following SugarCRM REST API method calls: oauth_access, get_entry, get_entry_list, set_entry, set_entries.

SugarRestSharp

For more info/documentation, please check SugarRestSharp wiki.

For Java port, please check SugarOnRest.

Basic Sample Usages

string sugarCrmUrl = "http://191.101.224.189/sugar/service/v4_1/rest.php";
string sugarCrmUsername = "will";
string sugarCrmPassword = "will";

var client = new SugarRestClient(sugarCrmUrl, sugarCrmUsername, sugarCrmPassword);

// Option 1 - Read by known type typeof(Account).
var accountRequest = new SugarRestRequest(RequestType.ReadById);

// set the account id to read.
accountRequest.Parameter = "1f2d3240-0d8a-ca09-2e11-5777c29a4193";
SugarRestResponse accountResponse = client.Execute<Account>(accountRequest);
Account account = (Account)accountResponse.Data;


// Option 2 - Read by known SugarCRM module name - "Contacts".
var contactRequest = new SugarRestRequest("Contacts", RequestType.ReadById);
contactRequest.Parameter = contactid;
SugarRestResponse contactRresponse = client.Execute(contactRequest);
Contact contact = (Contact)contactRresponse.Data;


// Option 3 - Read async by known type typeof(Case).
var caseRequest = new SugarRestRequest(RequestType.ReadById);
caseRequest.Parameter = caseId;
SugarRestResponse caseResponse = await client.ExecuteAsync<Case>(caseRequest);
Case case = (Case)caseResponse.Data;


// Option 4 - Read async by known SugarCRM module name - "Leads".
var leadRequest = new SugarRestRequest("Leads", RequestType.ReadById);
leadRequest.Parameter = leadId;
SugarRestResponse leadResponse = await client.ExecuteAsync(leadRequest);
Lead lead = (Lead)leadResponse.Data;

### Advanced Sample Usage - Linked Module This sample usage shows how to read "Accounts" module entity data with linked modules (link "Contacts" module). For more request options make changes to the [Options parameter](https://github.com/mattkol/SugarRestSharp/wiki/Request%20Options).

This implements the get_entry SugarCRM REST API method setting the link_name_to_fields_array parameter.

using SugarRestSharp;
using CustomModels;
using Newtonsoft.Json;

string url = "http://191.101.224.189/sugar/service/v4_1/rest.php";
string username = "will";
string password = "will";

var client = new SugarRestClient(url, username, password);
string accountId = "54bf59bc-ec61-1860-a97e-5777b5e92066";

var request = new SugarRestRequest(RequestType.LinkedReadById);
request.Parameter = accountId;

List<string> selectedFields = new List<string>();
selectedFields.Add(nameof(Account.Id));
selectedFields.Add(nameof(Account.Name));
selectedFields.Add(nameof(Account.Industry));
selectedFields.Add(nameof(Account.Website));
selectedFields.Add(nameof(Account.ShippingAddressCity));

request.Options.SelectFields = selectedFields;

Dictionary<object, List<string>> linkedListInfo = new Dictionary<object, List<string>>();

List<string> selectContactFields = new List<string>();
selectContactFields.Add(nameof(Contact.FirstName));
selectContactFields.Add(nameof(Contact.LastName));
selectContactFields.Add(nameof(Contact.Title));
selectContactFields.Add(nameof(Contact.Description));
selectContactFields.Add(nameof(Contact.PrimaryAddressPostalcode));

linkedListInfo[typeof(Contact)] = selectContactFields;

request.Options.LinkedModules = linkedListInfo;

SugarRestResponse response = client.Execute<Account>(request)

Custom model

using Newtonsoft.Json;
using SugarRestSharp.Models;
using System.Collections.Generic;

public class CustomAcccount : Account
{
    [JsonProperty(PropertyName = "contacts")]
    public virtual List<Contact> ContactLink { get; set; }
}

Response (Data)

Data = null;

// Deserialize json data to custom object
CustomAcccount customAccount = JsonConvert.DeserializeObject<CustomAcccount>(response.JData);

Response (JData)

{
  "id": "54bf59bc-ec61-1860-a97e-5777b5e92066",
  "name": "Southern Realty",
  "industry": "Telecommunications",
  "website": "www.veganqa.tv",
  "shipping_address_city": "Santa Monica",
  "contacts": [
    {
      "first_name": "Cameron",
      "last_name": "Vanwingerden",
      "title": "IT Developer",
      "description": "",
      "primary_address_postalcode": "89219"
    },
    {
      "first_name": "Jessica",
      "last_name": "Mumma",
      "title": "VP Sales",
      "description": "",
      "primary_address_postalcode": "26988"
    },
    {
      "first_name": "Brianna",
      "last_name": "Gleeson",
      "title": "VP Operations",
      "description": "",
      "primary_address_postalcode": "70525"
    },
    {
      "first_name": "Miles",
      "last_name": "Gore",
      "title": "Director Sales",
      "description": "",
      "primary_address_postalcode": "82591"
    },
    {
      "first_name": "Georgia",
      "last_name": "Brendel",
      "title": "Director Sales",
      "description": "",
      "primary_address_postalcode": "63582"
    }
  ]
}

Response (JsonRawRequest)

{
  "resource": "",
  "parameters": [
    {
      "name": "method",
      "value": "get_entry",
      "type": "GetOrPost"
    },
    {
      "name": "input_type",
      "value": "json",
      "type": "GetOrPost"
    },
    {
      "name": "response_type",
      "value": "json",
      "type": "GetOrPost"
    },
    {
      "name": "rest_data",
      "value": "{\"session\":\"jc520u2ql973ec9m67n7935tu3\",\"module_name\":\"Accounts\",\"id\":\"54bf59bc-ec61-1860-a97e-5777b5e92066\",\"select_fields\":[\"id\",\"name\",\"industry\",\"website\",\"shipping_address_city\"],\"link_name_to_fields_array\":[{\"name\":\"contacts\",\"value\":[\"first_name\",\"last_name\",\"title\",\"description\",\"primary_address_postalcode\"]}],\"track_view\":false}",
      "type": "GetOrPost"
    },
    {
      "name": "Accept",
      "value": "application\/json, application\/xml, text\/json, text\/x-json, text\/javascript, text\/xml",
      "type": "HttpHeader"
    }
  ],
  "method": "POST",
  "uri": "http:\/\/191.101.224.189\/sugar\/service\/v4_1\/rest.php"
}

Response (JsonRawResponse)

{
  "statusCode": 200,
  "content": "{\"entry_list\":[{\"id\":\"54bf59bc-ec61-1860-a97e-5777b5e92066\",\"module_name\":\"Accounts\",\"name_value_list\":{\"id\":{\"name\":\"id\",\"value\":\"54bf59bc-ec61-1860-a97e-5777b5e92066\"},\"name\":{\"name\":\"name\",\"value\":\"Southern Realty\"},\"industry\":{\"name\":\"industry\",\"value\":\"Telecommunications\"},\"website\":{\"name\":\"website\",\"value\":\"www.veganqa.tv\"},\"shipping_address_city\":{\"name\":\"shipping_address_city\",\"value\":\"Santa Monica\"}}}],\"relationship_list\":[[{\"name\":\"contacts\",\"records\":[{\"first_name\":{\"name\":\"first_name\",\"value\":\"Cameron\"},\"last_name\":{\"name\":\"last_name\",\"value\":\"Vanwingerden\"},\"title\":{\"name\":\"title\",\"value\":\"IT Developer\"},\"description\":{\"name\":\"description\",\"value\":\"\"},\"primary_address_postalcode\":{\"name\":\"primary_address_postalcode\",\"value\":\"89219\"}},{\"first_name\":{\"name\":\"first_name\",\"value\":\"Jessica\"},\"last_name\":{\"name\":\"last_name\",\"value\":\"Mumma\"},\"title\":{\"name\":\"title\",\"value\":\"VP Sales\"},\"description\":{\"name\":\"description\",\"value\":\"\"},\"primary_address_postalcode\":{\"name\":\"primary_address_postalcode\",\"value\":\"26988\"}},{\"first_name\":{\"name\":\"first_name\",\"value\":\"Brianna\"},\"last_name\":{\"name\":\"last_name\",\"value\":\"Gleeson\"},\"title\":{\"name\":\"title\",\"value\":\"VP Operations\"},\"description\":{\"name\":\"description\",\"value\":\"\"},\"primary_address_postalcode\":{\"name\":\"primary_address_postalcode\",\"value\":\"70525\"}},{\"first_name\":{\"name\":\"first_name\",\"value\":\"Miles\"},\"last_name\":{\"name\":\"last_name\",\"value\":\"Gore\"},\"title\":{\"name\":\"title\",\"value\":\"Director Sales\"},\"description\":{\"name\":\"description\",\"value\":\"\"},\"primary_address_postalcode\":{\"name\":\"primary_address_postalcode\",\"value\":\"82591\"}},{\"first_name\":{\"name\":\"first_name\",\"value\":\"Georgia\"},\"last_name\":{\"name\":\"last_name\",\"value\":\"Brendel\"},\"title\":{\"name\":\"title\",\"value\":\"Director Sales\"},\"description\":{\"name\":\"description\",\"value\":\"\"},\"primary_address_postalcode\":{\"name\":\"primary_address_postalcode\",\"value\":\"63582\"}}]}]]}",
  "headers": [
    {
      "Name": "Pragma",
      "Value": "no-cache",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "Content-Length",
      "Value": "1896",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "Cache-Control",
      "Value": "no-store, no-cache, must-revalidate, post-check=0, pre-check=0",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "Content-Type",
      "Value": "application\/json; charset=UTF-8",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "Date",
      "Value": "Sun, 18 Dec 2016 03:47:57 GMT",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "Expires",
      "Value": "Thu, 19 Nov 1981 08:52:00 GMT",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "Set-Cookie",
      "Value": "PHPSESSID=jc520u2ql973ec9m67n7935tu3; path=\/",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "Server",
      "Value": "Apache\/2.4.7 (Ubuntu)",
      "Type": 3,
      "ContentType": null
    },
    {
      "Name": "X-Powered-By",
      "Value": "PHP\/5.5.9-1ubuntu4.17",
      "Type": 3,
      "ContentType": null
    }
  ],
  "responseUri": "http:\/\/191.101.224.189\/sugar\/service\/v4_1\/rest.php",
  "errorMessage": null
}

sugarrestsharp's People

Contributors

mattkol avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sugarrestsharp's Issues

Put a meeting on user's calendar

Hi @mattkol,
I am using SugarRestSharp to pragmatically create meetings on my SuiteCRM installation. Each meeting is assigned to a defined user, but I can't see them on the calendar. I know that it's needed to place a relationship between meeting and user, via meetings_users table. How can I archieve that with SugarRestSharp?

Thanks
chogori

Implementing this with SuiteCRM 7.11.18

Hi we tried running this code but it kept giving us an error "JsonProperty namespace not found". Isn't that supposed to be fixed with "using Newtonsoft.Json"? I'm not sure if the framework is just outdated but we can't figure out how to get this repo to work.

Response Data Limited To 100

Hi,

When i try to trigger the following code for bulk read all my existing case on debugging the result only show the 100 data of case.
How to get all the cases which are present in the SugarCRM?

var client = new SugarRestClient(url, username, password); var request = new SugarRestRequest(RequestType.BulkRead); SugarRestResponse response = client.Execute<Case>(request); List<Case> cases = (List<Case>)response.Data;

Thanks & Regards,
Manthan Patel

Using this library with SugarCRM 7

Hello @mattkol
I would like to know if this library is actively maintained and can be used to access V10 REST APIs for SugarCRM 7.x

We are in process of implementing a SugarCRM connector and I would like to get some help here. If you have come across any good documentation, examples or any other open source libraries which can help us doing that, Please share the information.

Thank you.

DateStart and DateEnd null!

Hi @mattkol,
I am having a first look to your SugarRestSharp. In particular, I wrote a command-line utility to bulk-create calls and meetings. I am trying to set DateStart and DateEnd but on the CRM I see those fields empty. Duration (hours and minutes) works fine. I am using SuiteCRM 7.8.7 LTS.

Thanks

Add an email address to a new Lead on creation

Hi,

thanks for the library firstly. My question is, how do I add an email address to a new Lead. I have the lead being created but I cant see where to add the email address(s).

TIA
Trevor

CastException Error

Hi,

I am getting the following error

System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'SugarRestSharp.Models.Case'.'

While the code behind is
` string moduleName = "Cases";

        var client = new SugarRestClient(url, username, password);
        var request = new SugarRestRequest(moduleName, RequestType.Create);

        Case c = new Case();
        c.AccountId = "75262eb1-2382-1d61-b3a4-5901ef67e373";
        c.Name = "API Testing";
        c.Description = "This is where i am going to write my tally problem";
        c.MobileNumber = "9999999999";
        c.Status = "New";
        c.SupportMode = "call";
        c.Priority = "P2";

        request.Parameter = c;

        // Select fields.
        List<string> selectFields = new List<string>();
        selectFields.Add(nameof(Case.AccountId));
        selectFields.Add(nameof(Case.Name));
        selectFields.Add(nameof(Case.Description));
        selectFields.Add(nameof(Case.MobileNumber));
        selectFields.Add(nameof(Case.Status));
        selectFields.Add(nameof(Case.TicketSource));
        selectFields.Add(nameof(Case.MobileNumber));
        selectFields.Add(nameof(Case.SupportMode));
        selectFields.Add(nameof(Case.Priority));

        request.Options.SelectFields = selectFields;
        SugarRestResponse response = client.Execute(request);          
     
        Case cres = (Case)response.Data;`

few weeks back it was working fine but from now i am getting the cast exception.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.