The other day I developed a custom SharePoint command in Visual Studio 2010.
I was to specify a String parameter for the command and it should have returned a simple .NET object having a few properties. Something like that:
To execute the command that returns this kind of data I had to use the following overload of the ExecuteCommand method:
TResult ExecuteCommand<T, TResult>(string commandId, T arg)
It seemed that the command was executed successfully, but I experienced that instead of the expected result, an exotic runtime exception was generated:
Microsoft.VisualStudio.SharePoint.SharePointConnectionException. Message: Type ‘MyNameSpace.MyClass’ with data contract name ‘MyClass:http://schemas.datacontract.org/2004/07/SPVSCommands’ is not expected. Add any types not known statically to the list of known types – for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.
I used this type of ExecuteMethod successfully for simple object types, like String.
The following posts gave me some info on the possible background of the error:
Finally, I found a rather simple and logic solution for the issue:
It seems the object must be serializable to be transmitted over the wire. It is interesting that if I use a similar “complex” object type as the input parameter of the ExecuteMethod, it runs without error, even if the class is not serializable.