SharePoint: Batch updating items in a list with content approval enabled

Details

I was recently looking for a way to batch update items in a list that had “Require Content approval” (see Figure 1 below) enabled.  My requirement was that, after updating the item, it would be in an approved and not be placed into a pending status.  After doing some digging I was able to find the required command (<SetVar Name=”Cmd”>Moderate</SetVar>) that allowed this to work.  I’ve included a very simple test program below that updates the title field in a list and sets it to approved status (_ModerationStatus==0).  You can also assign other status values based on the enum SPModerationStatusType (values listed below).

public enum SPModerationStatusType {
// Summary:
// The item is displayed in public views of the list or document library.
Approved = 0,
//
// Summary:
// The item is not displayed in public views of the list or document library.
Denied = 1,
//
// Summary:
// The decision about displaying the item in public views of the list or document
// library is pending.
Pending = 2,
Draft = 3,
Scheduled = 4,
}

image

Figure 1 – Content Approval Option

Code Sample (sans adequate error checking) that ‘batch-updates’ a single item

static void Main(string[] args)
{
StringBuilder methodBuilder = new StringBuilder();
string batch = string.Empty;
string batchFormat = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<ows:Batch OnError=\"Return\">{0}</ows:Batch>";

            string methodFormat = "<Method ID=\"{0},Update\">" +
"<SetList>{1}</SetList>" +

             "<SetVar Name=\"Cmd\">Moderate</SetVar>" +
"<SetVar Name=\"ID\">{2}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#Title\">{3}</SetVar>" +
"<SetVar Name=\"urn:schemas-microsoft-com:office:office#_ModerationStatus\">0</SetVar>" +
"</Method>";

            using (SPSite site = new SPSite(" https://moss.litwareinc.com/sites/aspnet"))
            {
using (SPWeb sourceWeb = site.OpenWeb())
{
try
{
int itemID = 1;
SPList list = sourceWeb.Lists["TestList"];
sourceWeb.AllowUnsafeUpdates = true;
string listGuid = list.ID.ToString();
methodFormat = string.Format(methodFormat, itemID, listGuid, itemID,"SomeValue1a1aa");
batch = string.Format(batchFormat, methodFormat.ToString());
string batchReturn = sourceWeb.ProcessBatchData(batch);
}
catch (Exception oEx)
{

                    } // End exception block
} // End using spweb
} // End using SPSite

} // End Main