Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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