Title: Microsoft.SharePoint.Client.ServerException - User cannot be found
Tags: c#;sharepoint
Question: Get list files from sharepoint library:
using (ClientContext cxt = new ClientContext(&quot;Site/Library&quot;))
List doclib = cxt.Web.Lists.GetByTitle(listTitle);
cxt.Load(doclib, l =&gt; l.RootFolder, l =&gt; l.Fields);
cxt.ExecuteQuery();
do
CamlQuery query = new CamlQuery();
query.ListItemCollectionPosition = pos;
query.ViewXml = String.Format(@&quot;<View Scope='RecursiveAll'&gt;<RowLimit&gt;{0}</RowLimit&gt;</View&gt;&quot;, PartCount);
ListItemCollection listItems = doclib.GetItems(query);
cxt.Load(
listItems,
a =&gt; a.ListItemCollectionPosition,
a =&gt; a.Include(
b =&gt; b[&quot;FileRef&quot;],
b =&gt; b.File,
b =&gt; b.File.Author).Where(b =&gt; (string)b[&quot;FSObjType&quot;] != &quot;1&quot;));
cxt.ExecuteQuery();
pos = listItems.ListItemCollectionPosition;
while (pos != null);
without the expression &quot;b =&gt; b.File.Author&quot; works well, on another library everything works with this expression too. Exception:
Microsoft.SharePoint.Client.ServerException
Message:
User cannot be found.
Stack:
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponseStream(Stream responseStream)
at Microsoft.SharePoint.Client.ClientRequest.ProcessResponse()
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQueryToServer(ChunkStringBuilder sb)
at Microsoft.SharePoint.Client.ClientRequest.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientRuntimeContext.ExecuteQuery()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
Comment: Is it like possible some user does not exist anymore on your farm? Maybe you could try to first get all columns as plain text? Also what happens if you try to do it this way b["Author"]?

Comment: To get all values as text you could include b=>b.FieldValuesAsText . Try maybe this and check if you get the same error?

Comment: I am not sure.. I thought that viewing as text would rather present the login not the name :-) I will need to check this kind of scenario myself and will let you know if I have any progress on getting the login for author column of a user which was removed from tenant and ad

Comment: Thanks for the advice!
The user is no longer in AD. The b ["Author"] property contains the name, but not the login, no exception occurs, b.FieldValuesAsText also contains the username. Can't I get to the login?

Comment: And the file versions are even worse. For file versions, there is only a "CreatedBy" field of type "Microsoft.SharePoint.Client.User". How do I get the login or username of the user who created the version if the user is removed from the directory? How can we at least avoid an exception with add to code "`b => b.File.Versions.Include(fv => fv.CreatedBy)`"? Maybe this can be done somehow through the SharePoint settings?



{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 0, 'CommentCount': 5, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:08/17/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><sharepoint>', 'Title': 'Microsoft.SharePoint.Client.ServerException - User cannot be found', 'ViewCount': 486, 'id': 63451818, 'user_AboutMe': None, 'user_AccountId': 19314382.0, 'user_DisplayName': 'Andrey Kuznetsov', 'user_Reputation': 1.0, 'user_Views': 2.0, 'user_id': 14119473.0}}