Technical tips, suggestions and learnings on SharePoint.
I haven’t found any information on this but today I tried to add a custom event handler to a list automatically when a custom field type is added to a list using the OnSaveChange() method and I got the following error:
The specified program requires a newer version of Windows. (Exception from HRESULT: 0x8007047E) at Microsoft.SharePoint.Library.SPRequestInternalClass.AddField(String bstrUrl, String bstrListName, String bstrSchemaXml, Int32 grfAdd) at Microsoft.SharePoint.Library.SPRequest.AddField(String bstrUrl, String bstrListName, String bstrSchemaXml, Int32 grfAdd)
This message doesn’t seem to be much help but what I think is happening is that SharePoint isn’t allowing simultaneous changes to a list using different SPWeb/SpSite objects. Here is a break down of my problem:
The way I was able to fix this was that instead of creating my own SPSite/SPWeb and SPList objects use the SPField object that is passed to the OnSaveChange(SPField field, bool isNewField) method and accessing the list to be updated (with the event handler) by calling the field.ParentList object:
public void OnSaveChange(SPField field, bool isNewField){ try { SPSecurity.RunWithElevatedPrivileges( delegate() { #region Delete Existing deleting event handlers first SPEventReceiverDefinitionCollection evrs =
field.ParentList.EventReceivers; List<Guid> ersToDelete = new List<Guid>(); foreach (SPEventReceiverDefinition er in evrs) { if (er.Type == SPEventReceiverType.ItemDeleting) ersToDelete.Add(er.Id); } if (evrs.Count > 0) { foreach (Guid i in ersToDelete) { evrs[i].Delete(); field.ParentList.Update(); } } #endregion string assembly = Assembly.GetExecutingAssembly().FullName; string className = typeof(DeleteIconEventHandler).FullName; field.ParentList.EventReceivers.Add(
SPEventReceiverType.ItemDeleting, assembly, className); field.ParentList.Update(); }); } catch (Exception ex) { string message = string.Format("ERROR: Method {0} had an exception while
attempting RegisterDeleteItemEventHandler : {0}", ex.Message); throw new SPException(message); }}
[1] http://msdn.microsoft.com/en-us/library/bb802857.aspx
[2] http://www.sharepointblogs.com/aaronrh/archive/2007/05/25/the-solution-to-saving-properties-in-custom-field-types.aspx
[3] http://fredmorrison.wordpress.com/category/uncategorized/
Yes need a new instance of the web and the list for each update is what other google entries are saying