locked
Finding Checked-Out Documents / SPListItems ? RRS feed

  • Question

  • We have a script that finds all the documents associated with an SPWeb. It if finding all the documents and folders correctly, but isn't giving us anything for documents that are checked out. I am testing ...

    if (SPListItem.File != null)

    This is catching all the documents, except the ones that are checked out.

    Any tips or ideas about how to located the checkedout documents ?

    Friday, October 9, 2009 4:38 PM

Answers

  • This works for me:

    var targetLists = from l in web.Lists.OfType<SPList>()
                              where l.Title == "Documentation" 
                              select l; // get a targetLists of the lists in this 'web' 
    
    
            foreach (SPList l in targetLists) // cycle through all the lists in the targetLists
            {
    
                
                    SPDocumentLibrary library = (SPDocumentLibrary)l;
    
                    // ...print information about files uploaded but not checked in.
                    IList<SPCheckedOutFile> files = library.CheckedOutFiles;
                    foreach (SPCheckedOutFile file in files)
                    {
                        Console.WriteLine("{0,-20} {1,-25} {2}",
                            file.DirName, file.LeafName, file.CheckedOutBy);
                           
    // This is the code to check in the document //file.TakeOverCheckOut(); //SPListItem docItem = library.GetItemById(file.ListItemId); //docItem.File.CheckIn(string.Empty); //docItem.File.Update(); } } // end foreach (splist l in targetlists)
    • Marked as answer by Chengyi Wu Wednesday, October 14, 2009 1:23 AM
    Tuesday, October 13, 2009 7:37 PM

All replies

  • Check whether below peice of code helps you,

     

    foreach (SPListItem item in docLib.Items){
    
      if (item.FileSystemObjectType == SPFileSystemObjectType.File){
    
        SPFile file = item.File;
        if(file.CheckOutStatus == SPCheckOutStatus.None){
             Console.Out.WriteLine("Document is not checked out");
        }
    else
    {
    Console.Out.WriteLine(file.Name + "is checked out."); } }

    If you want to know more about check out status check SPCheckOutStatus enum values and SPFile class properties and methods.

    Friday, October 9, 2009 6:21 PM
  • Are you saing the docLib would be an SPList ? I tried with SPDocumentLibrary too.

    This is basically doing the same thing - finding all the standard documents, but it is never hitting the "else" clause, so I am assuming docLib never contains the documents that are checked out.

    Friday, October 9, 2009 7:40 PM
  • You know that there is a default option within Sharepoint to show all documents that are checkout.
    Look under site settings >view reports > checked out to me.

    Just wondering why you would develop it ?
    Is it because you want more functionality like finding every item regardless if it's checkoud by you or someone else ?
    You still have to remember that sharepoint uses security trimming, which means that you still not might get all documents checked out (if you don't have access to some part of the site.)


    Kind regards,
    Eric van Glabbeek
    Willing to learn everything about Sharepoint there is to learn. My blog
    Friday, October 9, 2009 10:48 PM
  • I tried but haven't been able to replicate the problem, but I think it might be an issue with the account you are using to run the script.  My suspicion is that the checked out items are new items that have yet to be checked in, and so the other users can't see them by default.  However, I haven't been able to replicate that with the code below - I see all the documents, and the one that is still checked out says so.

    SPWeb site = getSite();
    int numDocs = 0;
    foreach (SPList list in site.Lists)
    {
        if (list is SPDocumentLibrary)
        {
            SPDocumentLibrary docLib = (SPDocumentLibrary)list;
            foreach (SPListItem item in docLib.Items)
            {
                numDocs++;
                Console.WriteLine("Doc_" + numDocs.ToString() + ": " + item.File.Name +
                    " CheckOutStatus: " + item.File.CheckOutStatus.ToString());
            }
        }
    }
    Saturday, October 10, 2009 8:01 PM
  • This works for me:

    var targetLists = from l in web.Lists.OfType<SPList>()
                              where l.Title == "Documentation" 
                              select l; // get a targetLists of the lists in this 'web' 
    
    
            foreach (SPList l in targetLists) // cycle through all the lists in the targetLists
            {
    
                
                    SPDocumentLibrary library = (SPDocumentLibrary)l;
    
                    // ...print information about files uploaded but not checked in.
                    IList<SPCheckedOutFile> files = library.CheckedOutFiles;
                    foreach (SPCheckedOutFile file in files)
                    {
                        Console.WriteLine("{0,-20} {1,-25} {2}",
                            file.DirName, file.LeafName, file.CheckedOutBy);
                           
    // This is the code to check in the document //file.TakeOverCheckOut(); //SPListItem docItem = library.GetItemById(file.ListItemId); //docItem.File.CheckIn(string.Empty); //docItem.File.Update(); } } // end foreach (splist l in targetlists)
    • Marked as answer by Chengyi Wu Wednesday, October 14, 2009 1:23 AM
    Tuesday, October 13, 2009 7:37 PM