Tuesday, September 8, 2009

Check Outs Gone Crazy

Technorati Tags: ,,

 

It seems to be easy to have a lot of documents checked out in SharePoint 2007, yet nobody knows about them except the person who uploaded the document. SharePoint 2007 lets you upload documents first and then index them later. As you know indexing a document is pretty important if you want to find it. So many times columns in content types will be required. However, SharePoint 2007 gives you so many ways of putting documents into it without setting these required columns. You can upload a document through the UI and then cancel when presented with the EditForm.aspx. Second, you can drag documents from a folder to a webfolder using Windows Explorer without setting the required columns. Finally, you can send documents to SharePoint 2007 using the copy.asmx webs service or use front page extensions rpc calls without setting the required columns.

So what happens to these documents?  The required columns have not been populated and the documents probably should not be in SharePoint right? Well they are all checked out to the user that uploaded them. The best part is that user is the only one that can see them, not even a site administrator can see them. You cannot even see them in the object model where you would expect. The documents are in limbo. As a farm or site administrator you might want to know about these documents so you can rectify their state. The SharePoint object model provides a way for you to obtain these documents and do something with them. You can use the SPDocumentLibrary class' CheckeOutDocuments property to get a collection of SPCheckedOutFile objects. SPCheckedOutFile object gives you the ListItemId property which enables you to get the SPListItem object so you can create a report or send an email notification notifying users that these documents need there required columns populated.  Alternatively, if you have non-responsive users you can override the checkout and do what you want even check them in without setting the required columns. I don't recommend this since the required properties are there for a reason. The code below gives you an example of how you can use this collection and the SPCheckedOutFile class to accomplish some management of this type of document in your SharePoint repository.

 

 

          SPListItem docItem = null;

          using (SPSite site = new SPSite("http://basesmcdev/sitedirectory/tester1"))
          {
              using (SPWeb web = site.OpenWeb())
              {
                  SPDocumentLibrary docLib = (SPDocumentLibrary)web.Lists["mosstestsearch"];

                  foreach (SPCheckedOutFile scf in docLib.CheckedOutFiles)
                  {
                      scf.TakeOverCheckOut();
                      docItem = docLib.GetItemById(scf.ListItemId);
                      docItem.File.CheckIn(string.Empty);
                      docItem.File.Update();
                  }

          }

1 comment:

Anonymous said...

hi, i used the above code and was able to do checkin, But now the problem is I have 34000 documents and n numbers of user. if i want to do checkin of say person "a" who has 500 document checked ot and there are total 10000 document checked out, then in the above case it will loop 10000 times. i want it to do only for that user is it possible.
Kindly help

Post a Comment