Home Sign In

Steven Van de Craen's Blog [MVP]

Bloggings about SharePoint, .NET and more.

MVP SharePoint Server

RSS Feed
  • Contact Me

Archives

July 2007 (19)
August 2007 (5)
April 2008 (4)
September 2007 (3)
November 2007 (12)
October 2007 (4)
December 2007 (8)
January 2008 (6)
February 2008 (3)
March 2008 (4)
June 2008 (3)
July 2008 (1)
August 2008 (2)
September 2008 (1)

Categories

  • SharePoint 2003
  • SharePoint 2007
  • General
  • Office 2003
  • Office 2007
  • Excel Services
  • Dynamics NAV
  • SOAP
  • Search
  • Search Server 2008
  • .NET
  • CKS
  • IIS

Recent Posts

  • Filtering a list based on a multivalue column and filter
  • Backup and restore Site Collections between localized SharePoint installations
  • SharePoint: TODAY + 1 hour formula ?
  • So what's new ?
  • View Action Button

Other Blogs

//BLOG: naked programmer
JOPX
Jan Tielens' Bloggings [MVP]
Serge van den Oever [Macaw]
Carlos Segura Sanz
-->Patrick Tisseghem's Blog [MVP SharePoint] -->
Remco Ploeg's Blog
SharePoint Blogs
Romeo Pruno
[email protected]
Andrew Connell [MVP MOSS]
Mark Harrison
Pedro Serrano
Clemens Vasters and the Indigettes
Kevin Boske
SharePoint Team Blog
Brian Jones: Office XML Formats
Mart Muller's Sharepoint Weblog
Joel Oleson's SharePoint Land
Kris' blog
STSADM Custom Extensions
Microsoft Enterprise Search Blog
Michaël's coding thoughts
Chris O'Brien's blog
John Holliday's SharePoint Reflections
Koen's blog
PDT IT Services Blog Posts
.net palace (Benjamin Geens)

MOSS 2007: Exception when handling a document renaming event

Situation

I have written a small Event Handler to automatically copy the file name and version to custom text fields in order to be able to use them in Microsoft Word.

  • ItemAdded
  • ItemCheckedOut
  • ItemUpdated

Here's a small sample of the code I'm using:

public override void ItemUpdated(SPItemEventProperties properties)
{
    
SPListItem item = properties.ListItem;
     CopyMetadata(
ref item, "ItemVersion", item.Versions[0].VersionLabel);
     ...

     item.SystemUpdate(
false);
}

Problem

This code runs flawless as long as I don't change the file name. Changing the file name throws the following exception:

There is no file with URL 'Workinstructions/Test.doc' in this Web.

StackTrace:
   at Microsoft.SharePoint.Library.SPRequest.AddOrUpdateItem(String bstrUrl, String bstrListName, Boolean bAdd, Boolean bSystemUpdate, Boolean bPreserveItemVersion, Boolean bUpdateNoVersion, Int32& plID, String& pbstrGuid, Guid pbstrNewDocId, Boolean bHasNewDocId, String bstrVersion, Object& pvarAttachmentNames, Object& pvarAttachmentContents, Object& pvarProperties, Boolean bCheckOut, Boolean bCheckin, Boolean bMigration, Boolean bPublish)
   at Microsoft.SharePoint.SPListItem.AddOrUpdateItem(Boolean bAdd, Boolean bSystem, Boolean bPreserveItemVersion, Boolean bNoVersion, Boolean bMigration, Boolean bPublish, Boolean bCheckOut, Boolean bCheckin, Guid newGuidOnAdd, Int32& ulID, Object& objAttachmentNames, Object& objAttachmentContents, Boolean suppressAfterEvents)
   at Microsoft.SharePoint.SPListItem.UpdateInternal(Boolean bSystem, Boolean bPreserveItemVersion, Guid newGuidOnAdd, Boolean bMigration, Boolean bPublish, Boolean bNoVersion, Boolean bCheckOut, Boolean bCheckin, Boolean suppressAfterEvents)
   at Microsoft.SharePoint.SPListItem.SystemUpdate(Boolean incrementListItemVersion)

Cause

When using the SPListItem instance from properties.ListItem it seems that this instance is invalid at some times. Trying to work with this instance throws the exception.

I have only been able to reproduce this issue when Variations (MOSS Publishing Feature) are enabled on the Site Collection.

Solution

You will probably be frowning when looking upon this code but it does bypass the above issue. Basically I'm pausing the thread until I can get the item after the rename by checking the AfterUrl. To avoid an endless loop I limit the number of tries to 100.

///
///
Get the ListItem by opening a new SPWeb and using SPList.GetItemById.
/// Wait for the rename to have finished before returning the item.
///

///

///
Item
///

///
This issue occurs mostly when the following conditions are met:
/// - Variations are enabled and the SPList resides in a SPWeb in the Variation Hierarchy
/// - The item is renamed
/// - SPListItem.SystemUpdate(), SPListItem.UpdateOverwriteVersion() is called in the ItemUpdated Event Handler
///
private SPListItem GetRenamedListItem(ref SPItemEventProperties properties)
{
     SPListItem result =
null;
     for (int i = 0; i < 100; i++)
     {
          result = properties.OpenWeb().Lists[properties.ListId].GetItemById(properties.ListItemId);

         
if (result.Url.Equals(properties.AfterUrl))
               break;

         
Thread
.Sleep(100);
     }

     return
result;
}

In every event method I'm overriding I just use this method to retrieve the item reference.

Applies To

MOSS 2007 (I have only experienced this issue when Variations were enabled on the Site Collection)

 
Categories: SharePoint 2007

Trackback Url

 

Comments

Wednesday, 23 Jan 2008 11:09 by
shanks for your post.

Tuesday, 17 Jun 2008 09:53 by Labhesh Shrimali
I would like to change the version in the code? for example minor version to major version when user is checking in the document and my checkin event handler is executed.? i hope the code should be written before item.SystemUpdate(false)?

Leave a comment

Name

Url

Email

Comments

CAPTCHA Image Validation

© 2007 Steven Van de Craen — Powered by Community Kit for SharePoint: Enhanced Blog Edition.