0

I am attempting to query SharePoint Online using CSOM but I am getting this error -> what do I need to change in my project to remove this error so I can succesfully query?

System.InvalidCastException: '[A]Microsoft.SharePoint.Client.ListItem cannot be cast to [B]Microsoft.SharePoint.Client.ListItem. Type A originates from 'Microsoft.SharePoint.Client, Version=16.1.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' in the context 'LoadNeither' in a byte array. Type B originates from 'Microsoft.SharePoint.Client, Version=16.1.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' in the context 'LoadNeither' in a byte array.'

EDIT
Sometimes this will throw the above error and other times it executes exactly as I would expect. I would like to nail down what is causing the error so it can be removed :)

    static void Main(string[] args)
{
    string an = "Jose Mendez";
    var task1 = Task.Run(() => CaptureData1(an));
    var task2 = Task.Run(() => CaptureData2(an));

    var dbval = task1.Result;
    string b = task2.Result;
}

private static DBData CaptureData1(string manager)
{
    string a = null;
    string b = null;
    string c = null;
    string d = null;

    using (var ctx = new ClientContext(URL1))
    {
        var web = ctx.Web;
        List list = web.Lists.GetByTitle(Site1);
        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml = string.Format("");

        var listItemCollection = list.GetItems(camlQuery);

        ctx.Load(listItemCollection, eachItem => eachItem.Include(item => item));

        ctx.ExecuteQuery();

        foreach(ListItem listItem in listItemCollection)
        {

        }
        var dbval = new DBData
        {

        };
        return dbval;
    }
}
private static string CaptureData2(string manager)
{
    string transID = null;

    using (var ctx = new ClientContext(URL2))
    {
        var web = ctx.Web;
        List list = web.Lists.GetByTitle(Site2);
        CamlQuery camlQuery = new CamlQuery();
        camlQuery.ViewXml = string.Format("");

        var listItemCollection = list.GetItems(camlQuery);

        ctx.Load(listItemCollection, eachItem => eachItem.Include(item => item, item => item["X"]));

        ctx.ExecuteQuery();

        foreach (ListItem listItem in listItemCollection)
        {
            transID = listItem["X"].ToString();
        }
        return transID;
    }
}
  • Can you post your code please? – sasfrog Aug 25 '18 at 20:25
  • @sasfrog -> removed sensitive data and posted code – user2676140 Aug 25 '18 at 23:50
1

This seems cause by DLL conflict, try to install the dll by nuget and try.

Screenshot from VS2013 for your reference.

enter image description here

| improve this answer | |
  • I tried your solution and I still get the issue described in my OP. It is random and I can see no rhyme or reason as to why it is thrown on attempt 1 but not attempt 2 for example. – user2676140 Aug 31 '18 at 15:59

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.