문제

I have a document library in a SP 2013 site. I have a document content type with a document template associated to it. Now, I have written an ItemUpdated event receiver which will update the document with some text (not the properties of the list item associated with the file for which I could use SystemUpdate method. So please keep this in mind, while providing the solution). Now, I go to the library, click on the files ribbon and then click New Document (not upload), this opens up my template and make some changes and save it (the document is open in my local machine). The event receiver fires, but when it tries to do SPFile.SaveBinary (or SPFiles.Add(with params, I tried both), it throws the error saying

The file 'fileUrl' is locked for shared use by "domainName\contributor1".

If, instead of saving the file, I close the file after making changes, this error doesn't occur. But I cannot control whether the user will save or close. I have not set the Require Checkout option also. Only versioning is turned on in the library. Also, I had turned on the DIP (Document Information Panel) display earlier, which I have now turned off. Even then, it is showing up.

Can anyone please help me with these issues?

도움이 되었습니까?

해결책 3

Finally I found the solution. If the file is open in any client machine in Edit mode, then there is a lock created on it. Though most of the Lock related properties of SPFile are of not much help, the LockId property helps. It carries a GUID as a string, which means that the file actually has a lock. So calling the ReleaseLock method on the SPFile by passing the SPFile.LockId as the argument after checking it is not empty, the file gets updated without throwing any error, even if it is open in any client machine. The following code helps. (file is the instance of SPFile)

if(!string.IsNullOrEmpty(file.LockId))
 {                   
   file.ReleaseLock(file.LockId);
 }

But still, I am not able to figure out how to hide the Document Information Panel (DIP). Can anyone please suggest? In the Document Information Panel settings, the checkbox to show/hide DIP is already unchecked.

다른 팁

Using C#

You must have wait for some time to check if the file is ready to use or not. Means you need to wait till lock status SPFile.SPLockType.None becomes true.

Please check below code which applies System.Threading.Thread.Sleep(2000) for file to unlock.

if (SPContext.Current == null)
        {
          //If the initial file length is 0, pause the thread for 2 seconds
          if (properties.ListItem.File.Length == 0)
             {

               System.Threading.Thread.Sleep(2000);

               //Since our item exists, run the GetItemById to instantiate a new  and updated SPListItem object 
               var spFile = properties.List.GetItemById(properties.ListItemId);

               //SharePoint places an Exclusive lock on the file while the data is being loaded into the file
               while (spFile.File.LockType != SPFile.SPLockType.None)
                     {
                       System.Threading.Thread.Sleep(2000);
                       spFile = properties.List.GetItemById(properties.ListItemId);

                       //We need to check if the file exists, otherwise it will loop forever if someone decides to cancel the upload
                       if (!spFile.File.Exists)
                        return;
                     }

                //If someone thought it was a good idea to actually load a 0 byte file, don't do anything else
                if (spFile.File.Length == 0)
                       return;

              }
    }

Using POWERSHELL

You can unlock it programmatically!

There is no UI to unlock the locked files - as we do have for Check-in Checked-out files. So, Here is my solution to unlock the locked files using PowerShell.

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

#Variables for Web and File URLs
$WebURL ="http://intranet.crescent.com/support/"
$FileURL ="http://intranet.crescent.com/support/T1Support/Reports/ServiceTickets.xlsx" 

#Get Web and File Objects
$web = Get-SPWeb $WebURL
$File = $web.GetFile($FileURL)

#Check if File is locked
 if ($File.LockId -ne $null)
 {
     Write-host "File is Loked out by:" $File.LockedByUser.LoginName
     Write-host "File Lock Type: "$file.LockType
     Write-host "File Locked On: "$file.LockedDate
     Write-host "File Lock Expires on: "$file.LockExpires

     #To Release the lock, Uncomment below lines:
     #$File.ReleaseLock($File.LockId)
     #Write-host "Released the lock!" 
 }

Usually Windows SharePoint Services puts a write lock on the document on the server when a document is opened. The write lock times out after 10 minutes. Users cannot modify the document during the time when the document is locked. Workaround:

  1. Check whether the file has been properly checked in or not. If not, refresh the page and try to check in the file again.
  2. Delete the local copy of the file present in the cache folder of the concerned user account (access to user’s PC required).
  3. Close all the instances of file type opened in the system. E.g. if the file type is excel, then close all the excel files currently opened.
  4. Also kill the file type service (excel in this case) using the task manager

Sometimes a lock seems to persist, which may be the result of browser caching problems or bugs in the way client applications handle the file locks. The PowerShell for removing a file lock is as follows:

$web = get-spweb "site Url"
$list = $web.Lists["My Library"]
$item = $list.Items.GetItemById(123)
$item.File.ReleaseLock($item.File.LockId)
$web.Dispose()

Reference: click here

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 sharepoint.stackexchange
scroll top