Trying to write some code to declare an item as a Record.
The overall objective is to ensure that we can declare all items entered into a list as Records. As the relevant Workflow parts have been removed from SP2013 (they were available in SP2010) we have to build a custom workflow and WCF Service application specifically to address this issue.
However, although Records can be declared manually without any problems, trying to do exactly the same thing in code does not work
The Exception and code follow. Any help gratefully received. What am I doing wrong? I cannot believe how difficult this is to get working ...Surely this is a basic feature of the Product.
System.InvalidOperationException occurred HResult=-2146233079 Message=Operation is not valid due to the current state of the object. Source=Microsoft.SharePoint StackTrace: at Microsoft.SharePoint.WebControls.SPControl.SPWebEnsureSPControl(HttpContext context) at Microsoft.SharePoint.WebControls.SPControl.GetContextWeb(HttpContext context) at Microsoft.SharePoint.Utilities.SPUtility.ValidateFormDigest() at Microsoft.Office.RecordsManagement.RecordsRepository.Records.DeclareItemAsRecord(SPListItem itemToDeclareNonElevated, SPUser currentUser, SPList itemParentList, SPWeb rootWeb, Boolean isAutoDeclare) at Microsoft.Office.RecordsManagement.RecordsRepository.Records.DeclareItemAsRecord(SPListItem itemToDeclare, Boolean isAutoDeclare) at Microsoft.Office.RecordsManagement.RecordsRepository.Records.DeclareItemAsRecord(SPListItem itemToDeclare) at arbsp15Client.DeclareRecordClass.<>c__DisplayClass2.<DeclareRecord>b__0() in c:\Users\spsetup\Documents\Visual Studio 2012\Projects\arbsp15Client\arbsp15Client\DeclareRecord.cs:line 41 InnerException:
try { SPSecurity.RunWithElevatedPrivileges(delegate() { SPSite oSite = new SPSite(siteUrl); SPWeb oWeb = oSite.OpenWeb(); //SPList oList = oWeb.GetList(listRelativeUrl); oWeb.Lists.ListsForCurrentUser = true; SPList oList = oWeb.Lists[listTitle]; SPListItem oListItem = oList.Items.GetItemById(listItemID); if (!Records.IsRecord(oListItem)) { //Exception occurs on the next line
Records.DeclareItemAsRecord(oListItem); r++; } }); }