0

I am trying to upload a lot of files to SharePoint programmatically using a windows forms application. I use the SpFileCollection.Add() method to add the file and then attempt to set the properties by using that file's "Item" property. All the files are being loaded into the same folder of the same document library. My problem is, about 2600 files into this process (with 17,000 to go), it says "nope" because the Item property is NULL. Why is it null, and how can I correct the issue and move on?

Looking at the source file it seems OK - I was able to view its properties in the CMS it started in and the file itself (a PDF) with no problems.

Code snapshot:

Hashtable htProperties = new Hashtable();
file = folder.Files.Add(contentsFromUCM.SharPointFilename, filecontent, htProperties, EnsureUser(ci.CreatedBy, web), EnsureUser(ci.ModifiedBy, web), ci.CreatedDate, ci.ModifiedDate, true);
item = file.Item; // in debug mode the .Item is NULL

item[SPBuiltInFieldId.Modified] = SPUtility.CreateISO8601DateTimeFromSystemDateTime(ci.ModifiedDate.ToUniversalTime()); 
item[SPBuiltInFieldId.Editor] = EnsureUser(ci.ModifiedBy, web);

UPDATE: I learned to just ignore the problem. It only happens very rarely and with specific files so hopefully not enough of them come up where I can't just manually upload them later. I'll post a fuller answer if nobody has any better ideas.

1

You can use try - catch statement for continuing the process.

Inside the catch statement log the file which is not uploaded.

I believe those files might be

  1. Corrupted
  2. Some other processes has locked the files from reading

Another point you should be aware of is the list view resource throttling which is set to 5000 by default.

| improve this answer | |
  • Thanks for the answer, but at this point I don't even remember what my problem was, anymore. – Matt Jun 3 '16 at 17:33
0

You need to make sure you're not just added the file into the "ether".

In other words, your code doesn't indicate what folder is equal to. You can't just add a file to an SPFileCollection and expect to get back a reference to an item unless the folder your adding to belongs to an SPList.

private SPListItem UploadFile(string fileName, Stream uploadStream)
{
    SPList list = SPContext.Current.List; //Or just get a reference to the list you need.

    SPFile file = list.RootFolder.Files.Add(fileName, uploadStream, true);

    return file.Item;
}
| improve this answer | |
  • I mentioned in the OP that the document with the error happens after a couple thousand successful uploads. I guess I didn't mention they're all going to the same place, but I think it's safe to assume I was doing it right at least 2600 times before things went ploin-shaped. – Matt Jan 31 '14 at 16:06

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.