Sometimes you may land up in this trouble when try to update an item in document library after editing the file attached to it.
this exception clearly says "the file has been locked".I have got 2 solutions For it
1)Impersonation
2)Release Lock
First Option(this will allow you to update but lock is not released)
-------------
1)try to impersonate to application identity
Dim systemAccountToken As SPUserToken = SPContext.Current.Site.SystemAccount.UserToken
Using site1 As New SPSite(SPContext.Current.Site.ID, systemAccountToken)
Using web1 As SPWeb = site1.OpenWeb(SPContext.Current.Web.ID)
2) Get a reference to list and then to item which you want to update.
3) Make changes
4) Finally item.SystemUpdate()
Second Option(permanently will release the lock)
--------------------------------------------------
1)Get the reference to the item you want to update(normal way,without impersonation)
2) Check if file is still locked
3) Release Lock
4) Then make changes whatever you want to do.
e,g
Dim lockedfile As SPFile = itm.File 'gets the file associated with the item
If lockedfile.LockType <> SPFile.SPLockType.None Then 'check if file is still locked
lockedfile.ReleaseLock(lockedfile.LockId) 'releases lock
End If
I hope this will help someone.Any suggestions,always welcome :)
No comments:
Post a Comment