0

I am trying to rename the folder name when a document is open. How can I do that?

   using (SPSite site = new SPSite("http://sp2016/"))
            {
                using (SPWeb web = site.OpenWeb("site1"))
                {
                   SPList list = web.Lists["Documents"];
                    SPListItem item = list.GetItemById(1);
                   string folderName = "new folder Name";
                   item["FileLeafRef"] = folderName;
                   item["Title"] = folderName;  
                    item.Update();
                }
            }

As at the moment it is throwing the following errors: The specified file is encrypted and the user does not have the ability to decrypt it.

Unhandled Exception: Microsoft.SharePoint.SPException ---> System.Runtime.InteropServices.COMException: <nativehr>0x80071772</nativehr><nativestack></nativestack>

Unhandled Exception: Microsoft.SharePoint.SPException ---> System.Runtime.InteropServices.COMException: <nativehr>0x80071772</nativehr><nativestack></nativestack>
   at Microsoft.SharePoint.Library.SPRequestInternalClass.AddOrUpdateItem(String bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPreserveItemVersion, Boolean bPreserveItemUIVersion, Boolean bUpdateNoVersion, Int32& plID, String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion, Object& pvarAttachmentNames, Object& pvarAttachmentContents, Object& pvarProperties, Boolean bCheckOut, Boolean bCheckin, Boolean bUnRestrictedUpdateInProgress, Boolean bMigration, Boolean bPublish, String bstrFileName, ISP2DSafeArrayWriter pListDataValidationCallback, ISP2DSafeArrayWriter pRestrictInsertCallback, ISP2DSafeArrayWriter pUniqueFieldCallback)
0

Unfortunately, it is not possible. If the file is opened then you won’t be able to rename or delete the file, nor will you be able to rename the folder, even though you are site collection admin. To avoid this issue, before renaming the folder, close all files if opened by the code.

| improve this answer | |
  • isn't there some other solution to this problem? – Burre Ifort May 10 at 11:12
  • As per my knowledge, this is the default behavior both in Windows Explorer and SharePoint, there is no other go than closing the document. – Habibur Rahaman May 10 at 13:36
0

It can't be done as-is. If a document is open, then what you can do is copy the files to the new folder and delete the old folder.

The opened documents than have to be saved in the new folder as the old folder will not exist.

| improve this answer | |
  • how does sharepoint do it as I tested it and I was able to rename the folder while a document was open? – Burre Ifort May 10 at 11:26
  • If a document is open, we cannot even delete the source folder. – Habibur Rahaman May 10 at 13:38

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.