Showing posts with label uploaded documents publish programmatically. Show all posts
Showing posts with label uploaded documents publish programmatically. Show all posts

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.");