Wednesday, November 7, 2007

Files uploaded programmatically don't work for all users

Files uploaded programmatically don't work for all users

Today I encountered a problem when uploading images in a feature. Everything seemed to work fine, the images (used in my master page) appeared nicely where they should be, but then I logged in as another user and it just showed me the typical red x's. I noticed that the images were still checked out to the Administrator, so I checked them in programmatically and... still nothing. At first I thought they were not visible due to some kind of permissions problem, but the real solution was that you also need to publish a major version of the file. So if you ever upload files through code, do not forget these lines:

//Upload the new file.
SPFile addedFile = destinationFolder.Files.Add(fileName, fileToAdd.Open(FileMode.Open));

//Check in the newly created file and then publish a major version of it.
addedFile.CheckIn("File checked in by activating FodCustomLayout feature.");
addedFile.Publish("File published by activating FodCustomLayout feature.");

1 comment:

Anonymous said...

Hey Tom,

Thank you so much for this post.
I was facing the same issue.
After reading your post now I understood why it was not working.

Thanks again
Sughosh