Dailycode.info

Short solution for short problems

Checkin all items in a document library

When I had a deadlock on a document in a document library, I went looking for a solution. Some suggested that you write a webpart that checks all document in a document library, which are checked in shortterm. 

SPListItemCollection docLibItems = getDocLib().Items;
Page.Response.Write("No of items: " + docLibItems.Count.ToString() +"<br>");
foreach (SPListItem docLibItem in docLibItems)
{
Page.Response.Write("Item: " + docLibItem.Name + " status: " +
docLibItem.File.CheckOutStatus.ToString() + "<br>");
if (docLibItem.File.CheckOutStatus == SPFile.SPCheckOutStatus.LongTerm)
      {
            Page.Response.Write(docLibItem.File.Name.ToString());
            try
            {
                  docLibItem.File.CheckIn("");
            }
            catch (Exception error)
            {
                  Page.Response.Write(error.Message.ToString());
}
}
if (docLibItem.File.CheckOutStatus == SPFile.SPCheckOutStatus.ShortTerm)
      {
            Page.Response.Write("Checking in short term lock out!");
            try
            {
                  docLibItem.File.CheckIn("");
}
            catch (Exception error)
            {
                  Page.Response.Write(error.Message.ToString());
}
}


So I tried it, but it didn't helped me. So at the end I had to delete it and put it back again. Just download the document and "save as" a document on your local machine. That delete the original and upload the document again. The only disadvantage you have is that the creation date and the created by are changed. I had to remove this deadlock fast, but next time this occurs I will try to find a better solution to this problem. You can first try the code above, because in some cases it will work. I found people with the same problem and for them this code worked.