Wednesday, May 22, 2013

Records.BulkDeclareItemsAsRecords input string format issues

Recently I was working on some code to bulk declare hundreds of thousands of files in a folder as record and came across an error when calling Microsoft.Office.Policy.Records.BulkDeclareItemsAsRecords() as noted here:

http://msdn.microsoft.com/en-us/library/microsoft.office.recordsmanagement.recordsrepository.records.bulkdeclareitemsasrecords(v=office.14).aspx

This technet article has very limited documentation and no examples of how to use the class. It states that the first input parameter should be a "A comma delimited string of item IDs." If you do that, you'll get an exception: "input string in incorrect format". This is because you actually need to delimit the string with a | (pipe character).

Here's a snippet of my code that loops through the library and process batches of x number of items at a time:


 SPWeb thisWeb = SPContext.Current.Web;
                            SPFolder thisFolder = thisWeb.GetFolder(gFolderID);
                            SPDocumentLibrary thisLibrary = (SPDocumentLibrary)thisWeb.Lists[thisFolder.ParentListId];

                             // Query to get the unprocessed items in folder.
                              SPQuery query = new SPQuery();
                              query.Folder = thisFolder;
                              SPListItemCollection unprocessedItems = thisLibrary.GetItems(query);
                              string itemStr = "";


                                  // Build the csv string for the bulkdeclare method
                              for (int i = 0; i < unprocessedItems.Count; i++)
                                  {
                                      int itemID = unprocessedItems[i].ID;
                                      itemStr += itemID.ToString() + "|";
                                      iProcessedSoFar++;
                          

                                  //if we are at the batch size, then run the job and reset processed counter and csv string
                                      if (iProcessedSoFar >= iBatchSize)
                                      {
                                          //snip off that last comma
                                          itemStr = itemStr.TrimEnd('|');

                                          Records.BulkDeclareItemsAsRecords(itemStr, thisLibrary);
                                          
                                          itemStr = "";
                                          iProcessedSoFar = 0;

                                      }

                                  }