Customer Engagement & Dynamics CRM Forum

Expand all | Collapse all

C# code for Web Api

  • 1.  C# code for Web Api

    GOLD CONTRIBUTOR
    Posted Feb 21, 2019 08:27 AM
    Dear all,

    We have run across a new requirement wherein we would like to perform a retrieve of Opportunity records created by a user using Web Api.
    We wanted to know what would be the best way to do so . We would want to retrieve records using Web API and code in c# .Are there any possible places I can look for some code samples for CRUD operations for the opportunity or any entity using WEB API.

    Kindly guide and advise through the same.

    Thank you all

    #CustomerEngagement ​​

    ------------------------------
    Bhartendu Pandey
    Willis Towers Watson
    London
    ------------------------------


  • 2.  RE: C# code for Web Api

    Posted Feb 22, 2019 02:41 AM
    Hi Bhartendu,

    have you had a look at the D365 SDK?
    Here you will find some overview of sample code in the SDK: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/sample-code-directory

    I expect that what you are looking for is here: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/developer/org-service/sample-create-retrieve-update-delete-late-bound

    Does this help?
    Best Regards
    Torsten


    ------------------------------
    Torsten Harden
    Solution Architect
    Sycor Group
    Goettingen / Germany
    ------------------------------



  • 3.  RE: C# code for Web Api

    GOLD CONTRIBUTOR
    Posted Feb 22, 2019 03:31 AM
    Hi @Torsten Harden

    Thank you for your reply.
    The link you provided is a good one. However our requirement currently dictates that the code be written​ using C# and we want to use the web api.

    We are loooking to build a custom app and perform crud operations and we want to use the web APO and not the Iorganization service.
    I will still be looking into the SDK



    ------------------------------
    Bhartendu Pandey
    Willis Towers Watson
    London
    ------------------------------



  • 4.  RE: C# code for Web Api

    GOLD CONTRIBUTOR
    Posted Feb 22, 2019 10:21 AM
    Are you attempting to write the logic in .net Core/Standard?  If so, great, you can't use the SDK, spend your time recreating these calls your self.  If you are using the standard .Net Framework, then use the SDK.  Microsoft has already begun the process of internally updating these calls from the 2011 SOAP endpoint to the new Web API.  Updating the SDK is much easier / efficient / cost-effective than rewriting all of that logic by hand.

    ------------------------------
    Daryl LaBar
    President, MVP
    Gap Integrity
    Fishers IN
    ------------------------------



  • 5.  RE: C# code for Web Api

    GOLD CONTRIBUTOR
    Posted Feb 22, 2019 12:57 PM
    The Web API is FUN!

    You can start to play around with the GET requests from right inside your browser:


    In the image above, the part of the URL I underlined in Blue is the Rest endpoint, in this case it's showing you Audit entities. The GREEN portion of the request is ODATA (See here: OData Version 4.0 Part 1: Protocol Plus Errata 02 ) that I'm using to filter the returned records to just a single object_id.

    The browser makes this request as a GET request and returns me the results so I can see them as JSON. (I'm using an extension called JSONViewer to make it easy to read.)

    With just a little bit of time you can start making your own Web API requests to figure out what your application needs to support.

    You won't be able to do POST requests or any of the actions, but this is good enough to get you started with understanding ODATA and actually see the data you'll get via Web API.

    Message me if you'd like more help, this is an area I'm very familiar with and one of the strongest tools in my toolbox. I've been writing web apps for nearly 20 years and this is so fun for me.

    Happy Hacking!

    Nick

    ------------------------------
    Nick Hance
    Principal Developer; President
    Reenhanced
    www.javascriptforcrm.com
    www.buildbettersoftware.com
    Green Lane PA
    ------------------------------



  • 6.  RE: C# code for Web Api

    GOLD CONTRIBUTOR
    Posted Mar 05, 2019 05:39 AM
    Hello Nick,

    Just wanted to connect with you and check if there is a way to list Opportunities records similar to the "Recent opportunities view" as in CRM?
    Please let me know

    Thank you again for your kind help

    ------------------------------
    Bhartendu Pandey
    Willis Towers Watson
    London
    ------------------------------



  • 7.  RE: C# code for Web Api

    GOLD CONTRIBUTOR
    Posted Mar 05, 2019 09:14 AM
    Hi Bhartendu:

    I assume that you're wanting to do this within your Javascript application via the WebAPI.

    What I've learned is that you can get the raw values from the entity, but being able to render a specific view (that is administered from the CRM side) is much more difficult.

    I've looked deep into the form entity, where I can get FormJSON and formxml. My goal has been to render a form, but this is a nightmare of custom xml. I then thought I might be able to lift the form rendering javascript from within CRM, but that's akin to heart surgery.. I'd never be able to extract just what I needed.

    For your purpose of rendering a view, as far as I know, the best option is going to be to build your view in code. A smart way is to build it in JSX or whatever templating system you have and use your javascript to pass all of the data to this template. Modifying the view would require a code update, but at least you could limit the impact to a single file for small changes.

    WebAPI is great for getting the data you want. But the reuse between the configuration in CRM and what you can do with the WebAPI.. that's not very easy at this point.

    So you'll have to build the view directly in your code.

    Nick

    ------------------------------
    Nick Hance
    Principal Developer; President
    Reenhanced
    www.javascriptforcrm.com
    www.buildbettersoftware.com
    Green Lane PA
    ------------------------------



  • 8.  RE: C# code for Web Api

    GOLD CONTRIBUTOR
    Posted Mar 05, 2019 09:21 AM
    @Nick Hance Thank  you for your reply. I just wanted to confirm if there was a direct way available.



    ------------------------------
    Bhartendu Pandey
    Willis Towers Watson
    London
    ------------------------------



  • 9.  RE: C# code for Web Api

    SILVER CONTRIBUTOR
    Posted Feb 25, 2019 07:12 AM
      |   view attached
    Bhartendu,

    Attached is what I think you are looking for.  This is what I am currently using but do keep changing it from time to time.  The nice thing with the Web API is how easy it is to use.  I do most of my updates with PowerShell at this point.

    Wayne.


    ------------------------------
    Wayne Wittman
    CRM Administrator
    MiTek Industries Inc.
    Clifton NJ
    ------------------------------

    Attachment(s)

    txt
    CrmWebApi.cs.txt   3K 1 version


  • 10.  RE: C# code for Web Api

    GOLD CONTRIBUTOR
    Posted Feb 25, 2019 08:58 AM
    Hello @Wayne Wittman,

    Thank you for sharing the code sample.
    Yes this would seem to possibly work.
    Just had a question around particular assemblies and references to be included to avoid errors regarding Newtonsoft and Json converter.

    Could you kindly share the assemblies you have included .

    Thank you

    ------------------------------
    Bhartendu Pandey
    Willis Towers Watson
    London
    ------------------------------



  • 11.  RE: C# code for Web Api

    GOLD CONTRIBUTOR
    Posted Feb 25, 2019 10:39 AM

    While this may not be helpful for you - I thought it's somewhat related to this thread.  The CRM Rest builder is an awesome tool and I use it to do a whole array of client-side actions.

    CRM REST Builder v2.6.0.0 - Microsoft Dynamics CRM Community

    After you install the solution you'll get to create custom requests



    ------------------------------
    Travis Judd
    CRM Developer
    Trek Bicycle Corporation
    WATERLOO WI
    ------------------------------



  • 12.  RE: C# code for Web Api

    SILVER CONTRIBUTOR
    Posted Feb 25, 2019 10:55 AM
    Newtonsoft provides good JSON support.  Microsoft is now including and recommending it's use.
    Json.NET - Newtonsoft
    Newtonsoft remove preview
    Json.NET - Newtonsoft
    Json.NET is a popular high-performance JSON framework for .NET
    View this on Newtonsoft >

    I have been using Log4Net for some time.  If you have another logging package you use you can just remove the reference to it.
    Apache log4net - Apache log4net: Home - Apache log4net
    Apache remove preview
    Apache log4net - Apache log4net: Home - Apache log4net
    The Apache log4net library is a tool to help the programmer output log statements to a variety of output targets. log4net is a port of the excellent Apache log4j™ framework to the Microsoft® .NET runtime. We have kept the framework similar in spirit to the original log4j while taking advantage of new features in the .NET runtime.
    View this on Apache >



    ------------------------------
    Wayne Wittman
    CRM Administrator
    MiTek Industries Inc.
    Clifton NJ
    ------------------------------



  • 13.  RE: C# code for Web Api

    TOP CONTRIBUTOR
    Posted Feb 26, 2019 07:15 AM

    Wayne

    Did you use those 2 libraries in sandbox mode? I always get the "Inheritance security rules violated" when I use NewtonSoft.

    Also, why would you use a 3rd party logging library over crm ITracingService?

    Cheers!



    ------------------------------
    Rex Kenley Tan, MCP
    Tallmadge OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------



  • 14.  RE: C# code for Web Api

    SILVER CONTRIBUTOR
    Posted Feb 26, 2019 07:29 AM
    Rex,

    The ITracingService interface is for plugins.  Most of the work I am doing is for integration via console applications. So, I have not run into this issue.  For plugins or workflows I use the provided assemblies.

    We are on version 8.2 on-premise so don't have to deal with authenticating with the on line services.

    Wayne.

    ------------------------------
    Wayne Wittman
    CRM Administrator
    MiTek Industries Inc.
    Clifton NJ
    ------------------------------



  • 15.  RE: C# code for Web Api

    TOP CONTRIBUTOR
    Posted Feb 26, 2019 07:52 AM
    Thanks Wayne.

    I utilize the ITracingService interface in my unit tests logging class so I don't have to change any of my main code when I switch between local and online environment.

    Cheers!

    ------------------------------
    Rex Kenley Tan, MCP
    Tallmadge OH
    https://www.youracclaim.com/users/rex-kenley-tan

    *Always be CURRENT with JavaScript & C#, NEVER be obsolete.

    DISCLAIMER: All views expressed on this site are my own and DO NOT represent the opinions of ANY entity whatsoever with which I have been, am now, or will be affiliated.
    ------------------------------



If you've found this thread useful, dive deeper into User Group community content by role