Creating Folders using Microsoft Word in a MOSS 2007 Document Library 

Tags:

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");

                }

            }

        }

 
Posted by Nigel Price on 18-Jun-09
2  Comments  |  Trackback Url  | 0  Link to this post | Bookmark this post with:        
 

Links to this post

Comments


katiercommented onMonday, 12-Apr-2010
ok im out


Guy Nabzdykcommented onSaturday, 15-May-2010
i had to write now

Name:
URL:
Email:
Comments: