Question

I have the following code that returns me a list of checkout files in a document library.

 private static void IdentifyCheckedOutFilesForRequest(SPListItem listItem, SPDocumentLibrary lib, List<InvalidFile> invalidFiles)
        {

            string libraryName = lib.TitleResource.GetValueForUICulture(Thread.CurrentThread.CurrentUICulture);
            SPWeb web = lib.ParentWeb;

            // Identify lookupfield in doclib that points to the listitem
            string lufName = string.Empty;
            foreach (SPField f in lib.Fields)
            {
                if (f is SPFieldLookup)
                {
                    SPFieldLookup luf = f as SPFieldLookup;
                    if (new Guid(luf.LookupList) == listItem.ParentList.ID)
                    {
                        lufName = luf.InternalName;
                        break;
                    }
                }
            }

            if (string.IsNullOrEmpty(lufName))
            {
                Logger.LogDebug("HelperFunctions", "IdentifyCheckedOutFilesForRequest()", "No lookupfield found in library '" + libraryName + "' pointing to the current item.");
                return;
            }

            // Validate and identify checked out files that were 'once' checked in
            SPQuery query = new SPQuery();
            query.ViewAttributes = "Scope='Recursive'";
            query.Query = string.Format("<Where>" +
                "<And>" +
                    "<BeginsWith><FieldRef Name='ContentTypeId' /><Value Type='ContentTypeId'>0x0101</Value></BeginsWith>" +
                    "<Eq><FieldRef Name='{0}' LookupId='TRUE' /><Value Type='Lookup'>{1}</Value></Eq>" +
                "</and></Where>", lufName, listItem.ID);

            SPListItemCollection colItems = lib.GetItems(query);
            foreach (SPListItem item in colItems)
            {
                SPFile currentfile = item.File;
                //SPFolder currentFolder = currentfile.ParentFolder;

                bool isCheckedOut = currentfile.CheckedOutByUser != null;
                string fileName = currentfile.Name;
                //string checkedOutUserName = string.Empty;

                if (isCheckedOut)
                {
                    InvalidFile invalidFile = new InvalidFile();
                    invalidFile.LibraryName = libraryName;
                    invalidFile.FileName = fileName;
                    invalidFile.IsCheckedOut = isCheckedOut;
                    invalidFile.CheckedOutUserName = currentfile.CheckedOutByUser.Name;
                    invalidFile.FileLocation = "/" + SPUtility.GetUrlDirectory(currentfile.ServerRelativeUrl);
                    invalidFile.FileUrl = currentfile.ServerRelativeUrl;
                    invalidFiles.Add(invalidFile);
                }
            }

            // Validate and identify checked out files that were 'never' checked in in the library
            foreach (SPCheckedOutFile currentfile in lib.CheckedOutFiles)
            {
                //SPFolder currentFolder = web.GetFolder(string.Concat("", web.Site.WebApplication.GetResponseUri(web.Site.Zone)) + currentfile.DirName);
                //string serverRelativeUrlForFile =  "/" + currentfile.Url;

                InvalidFile invalidFile = new InvalidFile();
                invalidFile.LibraryName = libraryName;
                invalidFile.FileName = currentfile.LeafName;
                invalidFile.IsNeverCheckedIn = true;
                invalidFile.IsCheckedOut = true;
                invalidFile.CheckedOutUserName = currentfile.CheckedOutByName;
                invalidFile.FileLocation = "/" + currentfile.DirName;
                invalidFile.FileUrl = currentfile.Url;
                invalidFiles.Add(invalidFile);
            }

        }

this is returning me 2 files in a document library from a subweb, lets say

http://www.mydomian.com/spweb/doclibrary/file1.docx

When I copy and paste this in the browser the file does not exist. Its supposed to be checkout by someone, but why as a sitecollection admin I cant see it in the browser, but the API returns it to me?

I cant see in in the views either, I created a view of all checkout files, but its not there. I checked in and disregard checkoutfiles, and the api still returns me those 2 files.

Was it helpful?

Solution

One possible cause: If a file has never been checked in before, no accounts can access it except the one responsible for adding it. This usually happens when files are uploaded without all the required fields being filled out (most commonly this is the result of using Explorer View to drop files into the library). One easy solution is to run a recursive check-in program to check in all documents. Here's an example of one:

    public static void CallMassCheckIn()
    {
        using (SPSite oSite = new SPSite("http://server/site"))
        {
            using (SPWeb oWeb = oSite.OpenWeb())
            {
                SPDocumentLibrary oLib = (SPDocumentLibrary)oWeb.Lists["Library Name"];
                int iFolderCount = oLib.Folders.Count;
                // Root folder check-in
                MassCheckIn(oLib.RootFolder);
                // Sub-folder check-in
                for (int i = 0; i < iFolderCount; i++)
                {
                    MassCheckIn(oLib.Folders[i].Folder);
                }
            }
        }
    }
    public static void MassCheckIn(SPFolder oFolder)
    {
        foreach (SPFile oFile in oFolder.Files)
        {
            if (oFile.CheckOutType != SPFile.SPCheckOutType.None)
            {
                oFile.CheckIn("Programmatically checked in");
            }
        }
    }

That particular one is kicked off nightly by a Windows task on one of my servers. Users of that site primarily do all their transactions with Windows Explorer, and they tend to forget to check documents in after they are added that way, so this is a simple solution.

Hope that helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top