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;
    }
}
2
  • Can you post your code please?
    – sasfrog
    Commented Aug 25, 2018 at 20:25
  • @sasfrog -> removed sensitive data and posted code Commented Aug 25, 2018 at 23:50

1 Answer 1

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

1
  • 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. Commented Aug 31, 2018 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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