I had a problem recently where customers were complaining that they could not create folders within a MOSS 2007 document library and get them approved when the folder was created from Microsoft Word 2003 or Word 2007. However, when I created the folder using the MOSS 2007 document library GUI it created the folder and approved it using the “additem” event handler I had created.
Upon investigation the difference between the two scenarios was that with the MOSS 2007 Document Library GUI method the properties parameter passed into the event hander by MOSS 2007 had the SPItemEventProperties.ListItem object populated. Whereas when the folder was created using Word 2003 or Word 2007 then it was NULL ! (I am told this occurs when MOSS 2007 does not have a context to work with.)
So to fix the problem I changed the event handler to look like this :-
private void ItemHandleEvent(SPItemEventProperties properties)
{
try
{
if((properties.ListItem == null) || (properties.ListItem.ContentType.Name.ToLower() == "folder"))
{
// Its a folder
if (Trace.TraceLevel.TraceInfo)
{
Trace.Write(this, "ItemHandleEvent ContentTypeName = Folder ");
}
DisableEventFiring();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(properties.WebUrl.ToString()))
{
using (SPWeb web = site.OpenWeb())
{
SPList doclib = web.Lists[properties.ListTitle];
SPFolder thisfolder = web.GetFolder(properties.AfterUrl.ToString());
SPModerationInformation moderationInformation = thisfolder.Item.ModerationInformation;
if (Trace.TraceLevel.TraceInfo)
{
Trace.Write(this, "ItemHandleEvent modeationinformation = " + moderationInformation.Status.ToString());
}
if (!(moderationInformation.Status == SPModerationStatusType.Approved ))
{
moderationInformation.Comment = string.Format("This folder has been approved on the {0}", DateTime.Now);
moderationInformation.Status = SPModerationStatusType.Approved;
thisfolder.Item.Update();
Trace.Write(this, "ItemHandleEvent " + string.Format("This folder has been approved on the {0}", DateTime.Now));
}
}
}
});
}
}
catch (Exception ex)
{
Trace.Write(this, "We got an exception in Document Event Handler" + ex.Message);
}
finally
{
EnableEventFiring();
if (Trace.TraceLevel.TraceInfo)
{
Trace.Write(this, "ItemHandleEvent Finally");
}
}
}