spfile.delete gives file is checked out or locked for editing exception. RRS feed

  • Question

  • Dear Friends,

    I have created a web service for performing usual add/delete/update functions on sharepoint document library. But when i call this webservice function from one of the word plugins i get the following exception.

    System.Runtime.InteropServices.COMException (0x8107090E): The file is a user http://tsnt4-test/Lists/TSAkJKDocuments/154545445-342423/abc.doc DMTSIT \ xyz checked out or locked for editing.

    My observations

    1. spfile.moveTo method doesnt work in SPSecurity.RunWithElevatedPrivileges

    2. I have used two approaches two move the word document from source to destination.the usual spfile.moveTo() function didn't work so i tried with the following two approaches mentioned in bold letters. Kindly let me know the reason of this error and the alternative solution for the same.


    the web service code is as below:

        public void FileTransfer(string FileName, string strslova, string strPoznámka, string FileOldPath, string FileNewPath, bool result)
                    SPListItem newFileItem = null;
                    SPFileCollection collFiles = null;
                    TSAkJKDocuments objTSAkJkDocuments = null;
                    SPFile newFile = null;
                    SPSecurity.RunWithElevatedPrivileges(() =>
                        using (site = new SPSite(ConfigurationManager.AppSettings["SiteCollection"].ToString()))
                            site.AllowUnsafeUpdates = true;
                            using (web = Webs.Espis.GetWeb(site))
                                web.AllowUnsafeUpdates = true;
                                SPList documents = EspisLists.Documents.GetList(web);
                                SPFolder folder = web.GetFolder(FileOldPath);
                                 collFiles = folder.Files;

                                foreach (SPFile file in collFiles)
                                    if (file.Name.Equals(FileName, StringComparison.InvariantCultureIgnoreCase))
                                            web.AllowUnsafeUpdates = true;
                                           // file.MoveTo(FileNewPath, true);
                                            SPFolder foldernew = web.GetFolder(FileNewPath);
                                            newFile = foldernew.Files.Add(file.Name, file.OpenBinary(), true);
                                            newFileItem = newFile.Item;
                                            objTSAkJkDocuments = new TSAkJKDocuments(web, newFileItem);
                                            objTSAkJkDocuments.KlicovaSlova = strslova;
                                            objTSAkJkDocuments.Poznamka = strPoznámka;


                    if (objTSAkJkDocuments != null)
                        lock (_lock) //To prevent multiple queries from accessing the same objects simultaneously
                            web.AllowUnsafeUpdates = true;
                            if (collFiles[FileName] != null && !result)
                                //Approach 1
                                //web.AllowUnsafeUpdates = true;
                                //SPFile sptempfile = collFiles[FileName];
                                //byte[] tempbytearray = { 1 };

                                // Approach 2 if there is a lock.
                                web.AllowUnsafeUpdates = true;
                                SPFile sptempfile = collFiles[FileName];
                                byte[] tempbytearray = { 1 };
                                SPListItem splitem = sptempfile.Item;



                catch (Exception ex)
                    CustomThrow("Error in FileTransfer Method");



    Amit Dangre


    • Moved by Peter Jausovec Monday, November 8, 2010 11:29 PM (From:Sharepoint Development with Visual Studio)
    Monday, October 25, 2010 5:50 AM


  • Amit,

    A document library can be configured to automatically checkout a document when it is opened.  So if you are opening exisiting documents, then trying to delete a document that is automatically checked out that would explain the exception.  You would need to make sure the document is checked in before attempting a delete.  Additionally SharePoint already makes available web services for adding, updating, deleting, etc. documents.  You should consider using the existing Web Services.


    Dennis Bottjer | Follow Me: @dbottjer | Blog: Dennis Bottjer.com
    Monday, November 1, 2010 3:54 AM