Hi,
I have created a custom form to upload files to a library. Users can provide comments that are stored by calling CheckOut and CheckIn methods on the file. The problem is when the file is checked-in an Access Denied exception is thrown. The user seems to have all required permissions (e.g. EditListItems, OverrideCheckOut, etc.)
Has anyone had a similar problem with the Access Denied exception during checking-in a file programmatically?
This is the C# code:
private void UploadFile(Guid listId, int listItemId, string currFolder) { String sharePointSite = SPContext.Current.Web.Url; using (SPSite site = new SPSite(sharePointSite)) { using (SPWeb web = site.OpenWeb()) { // Get the list. SPListCollection lists = web.Lists; lists.ListsForCurrentUser = true; SPList list = lists.GetList(listId, false);
// Get the selected list item. SPListItem item = list.GetItemById(listItemId); if (item.File == null) throw new ArgumentException("This list item cannot have file versioning."); string itemFilename = item.File.Name; // Get data from the form. // InputFile, VersionComment, and chkOverwriteProperties are web controls. String fileToUpload = InputFile.Value.Trim(); String comments = VersionComment.Text; bool overwriteProperties = chkOverwriteProperties.Checked;
// Validate the file extension. if (Path.GetExtension(fileToUpload) != Path.GetExtension(itemFilename)) throw new ArgumentException("The selected file has a different extension than the file to upload."); SPFolder folder = null; if (currFolder == "") folder = list.RootFolder; else folder = FindSubFolder(web.RootFolder, currFolder); SPFile spfile = null;
// Prepare for upload bool overwriteExistingFile = true; bool checkRequiredFields = true; Stream stream = InputFile.PostedFile.InputStream; byte[] contents = new byte[stream.Length]; stream.Read(contents, 0, (int)stream.Length); stream.Close();
// Add the file to the library.
if (overwriteProperties) spfile = folder.Files.Add(itemFilename, contents, overwriteExistingFile, comments, checkRequiredFields); else { spfile = folder.Files.Add(itemFilename, contents, item.Properties, overwriteExistingFile); spfile.CheckOut();
// Update the comment. // THIS LINE THROWS THE "ACCESS DENIED" EXCEPTION spfile.CheckIn(comments, SPCheckinType.OverwriteCheckIn); } if (spfile != null) { folder.Update(); } else { throw new ApplicationException("Uploading the file failed."); } } } }
and this is the part of the web form containing the web controls:
<input type="file" id="InputFile" runat="server" class="ms-fileinput" size="35" /><br /><asp:TextBox TextMode="MultiLine" Rows="5" Columns="45" id="VersionComment" MaxLength="1023" class="ms-long" runat="server" /><br /><asp:CheckBox ID="chkOverwriteProperties" Checked="false" runat="server" /><br />
Thanks,
Leszek
Wiki: wbswiki.com
Website: www.wisenheimerbrainstorm.com