C#, CRM

CRM Plugins – Input Parameters and Early Binding

I recently came across this problem looking at a CRM instance that had been upgraded to use early-binding. They had done something like below:

var accountInputParameters = ((Entity)context.InputParameters["Target"]).ToEntity<Account>();

This takes the InputParameters (which is of type Entity) and then bound it to the Account early-bound type.

While this seems like a good idea on the surface, as it allows all the fields of the Account to be accessed using strongly typed properties, the customer reported some strange behaviour on their Accounts.

Continue reading

CRM, JavaScript

Some helpful CRM 2016 Bookmarklets

I’ve been working in CRM Online (2016) a little for the past few weeks and I found these little bookmarklets to be helpful.

For those that don’t know, a bookmarklet is a snippet of JavaScript that can be saved as a Bookmark in Chrome, IE or Firefox. By prefixing your JavaScript function with javascript: you’re telling the browser to execute JavaScript rather than go to a particular URL

So far I have bookmarklets to

  • Print the current entity’s schema name
  • Print the current entity’s ID
  • Reopen the current entity’s form

The Print ones are quite nice because I used CRM’s in-built notification system.

Continue reading

JavaScript, SharePoint, Web Services

Using SharePoint REST to create a folder

I was tasked with creating some folders in a Picture Library and I thought that this would be the perfect opportunity to use the SharePoint 2013 REST API for the first time.
It was actually quite easy:

function CreateFolder (){
    var url_web;
    var pictureLibrary
    var folder_name;
    var folder_path;

    url_web = _spPageContextInfo.webAbsoluteUrl;
    pictureLibrary = 'Pictures';
    folder_name = 'jasonscript';
    folder_path = pictureLibrary + '/' + folder_name;

    jQuery.ajax({
        'url' : url_web + "/_api/Web/Folders/Add('"+ folder_path + "')",
        'type' : 'POST',
        'headers' : { 
            'accept' : 'application/json; odata=verbose', 
            'content-type' : 'application/json; odata=verbose',
            'X-RequestDigest' : $('#__REQUESTDIGEST').val()
        },
        'success' : function (evt){
            // folder has been created
        },
        'error' : function (jqXHR, textStatus, errorThrown){
            // handle the error
        } 
    }); 
 }

The biggest problem that I had was that I found that the Add method seems to only accept values wrapped in single quotes.
I typically use single quotes to define my strings

    var myString = 'wrapped in single quotes';

However, when I tried this

    url_web + '/_api/Web/Folders/Add("'+ folder_path + '")'

I kept getting ‘bad request’ errors. Switching the quotes around resolved this problem.

    url_web + "/_api/Web/Folders/Add('"+ folder_path + "')"

Weird

C#, JavaScript, Web Services

JSON and the JavaScriptSerializer

Today I was trying to write a web service method that would allow me to run some stored procedures on my table. Rather than having multiple web service calls for each stored procedure I wanted to come up with some generic functions that could call a range of stored procedures:

  1. Scalar
  2. NonQuery
  3. Retrieve DataTable

Each of these stored procedures require a Stored Procedure name and a collection of SQL Parameters. I decided to instead take a JSON string which would be easy to create from the client and easy to parse on the server.

Here’s the JSON snippet that I came up with

{
    "procName" : "sp_updateUser",
    "procVars" : 
       [
          {
              "paramName" : "firstname",
              "paramValue" : "jasonscript",
              "paramDirection": "Input"
          },
          {
              "paramName" : "planet",
              "paramValue" : "Earth",
              "paramDirection": "Input"
          },
       ]
 }

Pretty simple. Then in my web service I created two classes:

  1. for my container
    internal class Proc
    {
        public string procName;
        public List<ProcArg> procVars;
    
        public Proc()
        {
            this.procName = "";
            this.procVars = new List<ProcArg>();
        }
    }
  2. for my parameters:
    internal class ProcArg
    {
        public string paramName
        public string paramValue
        public string paramType;
    
        public ProcArg()
        {
            this.paramName = "";
            this.paramValue = "";
            this.paramType = "";
        }
    }

Next step was to parse the JSON string to these classes

var jsSerializer = new JavaScriptSerializer();
var spInfo = jsSerializer.Deserialize<Proc>(json);

Now I can access everything from my JSON string in my spInfo object

spInfo.procName               == "sp_updateUser"
spInfo.procVars.Count         == 2
spInfo.procVars[0].paramName  == "jasonscript"
spInfo.procVars[1].paramValue == "Earth"

The JavaScriptSerializer will attempt to match the keys of the JSON string to the public properties of each Class.

JSON.procName                 == Proc.procName

These mappings can be altered using DataMember attributes. For example, I could re-write the Proc class as follows:

internal class Proc
{
    [DataMember(Name = "procName")]      // specify the name to use for serializing
    public string spName;                // renamed so no longer matches JSON snippet
    public List<ProcArg> procVars;

    public Proc()
    {
        this.spName = "";
        this.procVars = new List<ProcArg>();
    }
}

Thanks to the DataMember property, the JavaScriptSerializer can still map the JSON string to the class property

JavaScript, SharePoint

Javascript and Working with the SharePoint 2013 People Picker

The People Picker in SharePoint 2013 has had a bit of a face-lift.

The ‘Verify Address’ and ‘Address Book’ icons are gone and what’s left is a cleaner, simpler interface.

But new controls means that some of the older approaches to working with the People Picker no longer work.

As usual I’m working with SharePoint 2013 on the client side (CSOM) working with JavaScript and jQuery.

Here are some of the results that I’ve found

Continue reading