Value was either too large or too small for an Int32 – SharePoint

Value was either too large or too small for an Int32

In one of my previous post I had demonstrated how to, Download Large Files from SharePoint Online. While the file download is working perfectly fine, but, I was constantly getting the following error while trying to load/access it’s properties,

Value was either too large or too small for an Int32

Issue

Unable to access the properties of large files in SharePoint Online using CSOM.

Solution

Following is the code snippet I was using to access the properties of any SharePoint file using CSOM,

ListItemCollection listItemsBatch = list.GetItems(camlQuery);

clientContext.Load(listItemsBatch,
			p => p.Include(
				l => l.FieldValuesAsText,
				l => l.FieldValuesAsHtml,
				l => l.DisplayName,
				l => l.HasUniqueRoleAssignments,
				l => l.FileSystemObjectType,
				l => l.Id,
				l => l.RoleAssignments.Include(
					r => r.Member,
					r => r.RoleDefinitionBindings.Include(
						rd => rd.Name))));

clientContext.ExecuteQuery();

Now, the above piece of code is still working with smaller documents. It’s only with large files, that I was facing this issue.

To solve this, I started commenting out the given properties one by one, to check if one of them is causing the issue with large files. And, as it turned out, I finally got the culprit, FieldValuesAsHtml. When I omitted the loading of FieldValuesAsHtml, the code ran just fine . Here’s the working sample,

ListItemCollection listItemsBatch = list.GetItems(camlQuery);

clientContext.Load(listItemsBatch,
			p => p.Include(
				l => l.FieldValuesAsText,
				//l => l.FieldValuesAsHtml,
				l => l.DisplayName,
				l => l.HasUniqueRoleAssignments,
				l => l.FileSystemObjectType,
				l => l.Id,
				l => l.RoleAssignments.Include(
					r => r.Member,
					r => r.RoleDefinitionBindings.Include(
						rd => rd.Name))));

clientContext.ExecuteQuery();

Key Takeaways

  • In my test cases, smaller files mean, file size between, 1KB-10MB and large files mean, file size between 1.5GB-9GB.
  • I omitted all the other properties and loaded only, FieldValuesAsHtml, still the issue remained! So the problem lies with this property only.
  • Previously, due to the 2GB limitation on the file size by SharePoint, I had only tested the above code with files of sizes less than 1GB. So this could be an old, untested issue.
  • Finally, though I got the code working for me, I still don’t have a proper explanation for it. If anybody has any idea as to why we cannot request this property for large files, then, please share. Would love to know the exact reason. 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s