0

In my situation, I have records being added to a record center that are immediately declared as a record. There are some fields that are associated with these list items that I need to be able to update from an event receiver and from a workflow.

Short of un-declaring & re-declaring the record, what alternatives do I have to updates these fields for each of the list items? Where else could I store these fields?

1

The SharePoint object model provides a supported way of updating a record without unlocking it. There's a guide here that describes some of the relevant information. I was able to get a working example (excuse some of the 'basic' naming) in an ItemUpdated event receiver.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(properties.SiteId))
    {
        using (SPWeb web2 = site.OpenWeb(properties.Web.ID))
        {
            SPListItem item = web2.Lists.GetList(properties.List.ID, false).GetItemById(properties.ListItemId);

            Records.BypassLocks(item, delegate(SPListItem newItem)
            {
                newItem["Title"] = "Hello from code";
                newItem.Audit.WriteAuditEvent(SPAuditEventType.Custom, SPAuditEventSource.ObjectModel.ToString(), "<Data>Audit text</Data>");
                newItem.SystemUpdate(false);
            });

        }
    }
});

As usual with SPSecurity.RunWithElevatedPrivileges, you must create new SPSite and SPWeb objects, as well as a new SPListItem object otherwise you end up with the error Attempted to perform an unauthorized operation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.