Extending SharePoint 2013’s REST _api endpoints – handling ComplexType input parameters with POST


If you haven’t read my adventures with the extension of the REST API endpoints, you can go read my previous blog posts on the topic.
For one of the projects I’m working on, I need to create some resources so to expose functions that can only be called using HTTP POST and that get complex types as input parameters.For that to happen, you still need to implement a custom TypeConverter as I did in my previous posts but you need to override at least one more method. So, say that I need to have the following complex type as an input parameter of a function

[ClientCallableType(Name="MyClass", ServerTypeId="{E1BB82E8-0D1E-4e52-B90C-684802AB4EF7}")]
public class MyClass
   [ClientCallableProperty(Name = "prop1", ClientLibraryTargets = ClientLibraryTargets.RESTful)]
    public string prop1 { get; set; }

   [ClientCallableProperty(Name = "prop2", ClientLibraryTargets = ClientLibraryTargets.RESTful)]
    public string prop2 { get; set; }

You’ll have to override the CustomCreateFrom method in my converter as follows:

protected override object CustomCreateFrom(NamedClientValueCollection values, ProxyContext context)
    ClientValue clientValue = null;
    MyClass myClass = new MyClass();
    foreach (string key in values.Keys)
        if (values.TryGetValue(key, out clientValue))
            string strvalue = clientValue.ConvertTo<string>();
            switch (key)
                case "prop1":
                    myClass.prop1 = strvalue;
                case "AssetName":
                    myClass.prop2 = strvalue;
    return myClass;

Of course here, I have only two string properties so I can afford to convert everything to string. Things get more complex if your properties are collections…

Happy coding!

About Stephane Eyskens

Office 365, Azure PaaS and SharePoint platform expert
This entry was posted in SharePoint and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s