2

I have some very simple code using the client object model to retrieve a list and all items from that list from a SharePoint 2013 Online site.
When the ExecuteQuery() fires, I use Fiddler to view the received JSON results and that looks fine. The returned data/JSON contains the list and it's child items etc. But when the C# code starts looping the items in the line foreach (var listItem in items) and it wants to cast the first item to a ListItem, it always throws an InvalidCastException:

Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' to type 'Microsoft.SharePoint.Client.ListItem'.

So it never reaches the Console.WriteLine(""); line.

Here's the code:

var list = _web.Lists.GetByTitle(documentLibraryNamesToSearch[0]);
var camlQuery = new CamlQuery
{
    ViewXml = "<View />"
};

var items = list.GetItems(camlQuery);
_context.Load(list, l => l.Title);
_context.Load(items);
_context.ExecuteQuery();

foreach (var listItem in items)
{
    Console.WriteLine("");
}

I can't figure out what's causing this. Did I forget something?

2

Are you using the latest CSOM SharePoint 2013 client libraries? If not, download the 2013 libraries from here SharePoint Server 2013 Client Components SDK

  • Hi Welcome to SharePoint StackExchange please take time to check posting guidelines as this is more suited to a comment as apposed to an answer where you have typed it. – Hugh Wood Nov 29 '13 at 19:25
  • @HughWood Good point, will bear that in mind in future. – McGeeky Nov 29 '13 at 19:41
  • I was using the latest dll's, but referenced them double. See the answer I added. Thank you for your response @McGeeky. – Maarten Docter Dec 2 '13 at 23:15
2

I finally found out what was causing it.
I used an unit test project to test my DLL containing the above code.
I ilmerge'd the Microsoft.SharePoint.Client.dll and the Microsoft.SharePoint.Client.Runtime.dll into my DLL (because the DLL would be deployed to Dynamics CRM Online), but also referenced them from my unit test project. So they where referenced somewhat twice. This double availability was causing this strange behavior.
Hope this helps.

[edit June 2, 2015] I also found a post mentioning this same cast exception. The fix described there was to use the '/copyattrs' parameter with ILMerge. If you have installed ilMarge in the project with NuGet package:

  • Editing the file in solution:ILMerge.props
  • Change parameters:
<ILMergeAllowMultipleAssemblyLevelAttributes>true</ILMergeAllowMultipleAssemblyLevelAttributes>
<ILMergeCopyAttributes>true</ILMergeCopyAttributes>
  • Thanks a lot guys. By the way... I had exact same trouble... Which i could fix by Installing Framework.Net 4.5 while my server only had 4.0... in case off that helps Albo+++ – Albo Aug 7 '14 at 15:15
  • I had the same issue. fixed it by adding /copyattrs parameter to ILMerge query – Sergey Jun 6 '16 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.