I am trying to get the parent list of a file on Sharepoint online with .NET CSOM in c#. The code below works fine when I test it from a standalone c# app.
But when I call the exact same method via a com interop in c++, with the same parameters, I get an exception, "the specified object does not belong to a list". Exception is thrown after the ExecuteQuery()
But I KNOW it does, If I call the method with the exact same URL /sites/develop_apps/Shared Documents/subway/heynow(000640-9-9-2016 9-53-31 AM).PDF
in a standalone c# app, I get no exception. And the "GetFileByServerRelativeUrl" seems to be working fine. It doesn't throw an exception anyway.
I've read articles about similar errors and they seem to have something to do with the url possibly. But I can't understand why the same params work in one context but not another.
Microsoft.SharePoint.Client.File thisFile =m_clientContext.Web.GetFileByServerRelativeUrl(fileRelativeUrl);
ListItem item = thisFile.ListItemAllFields;
m_clientContext.Load(item.ParentList);
m_clientContext.ExecuteQuery();
Fiddler trace of the error shows.
{ "SchemaVersion":"15.0.0.0","LibraryVersion":"16.0.5701.1202","ErrorInfo":{ "ErrorMessage":"The object specified does not belong to a list.","ErrorValue":null,"TraceCorrelationId":"c616a29d-90ab-3000-b1de-17538058f3e4","ErrorCode":-2146232832,"ErrorTypeName":"Microsoft.SharePoint.SPException" },"TraceCorrelationId":"c616a29d-90ab-3000-b1de-17538058f3e4"
By comparing the ClientContext.Url fields of the working app and failing production test, I noticed that the urls were different.
My test that was working had a url of "https://mysite.sharepoint.com/sites/develop_apps/" .
My production test that's failing its "https://mysite.sharepoint.com"
and I was doing all my list manipulation in a library off of "https://mysite.sharepoint.com/sites/develop_apps/"
I guess when doing work against lists, you need to be more specific when creating your clientContext in regards to the url.