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:
- using System;
- namespace MyNameSpace
- {
- public class MyClass
- {
- public String Title { get; set; }
- public bool IsDeleted { get; set; }
- }
- }
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:
All About KnownTypes
About KnownTypeAttribute example
Finally, I found a rather simple and logic solution for the issue:
- using System;
- using System.Runtime.Serialization;
- namespace MyNameSpace
- {
- [Serializable()]
- public class MyClass
- {
- public String Title { get; set; }
- public bool IsDeleted { get; set; }
- }
- }
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.
May 20, 2010 at 01:04 |
[...] The class is decorated with the Serializable attribute. I discussed that issue in my former post. [...]