2

I have an Event Receiver that is supposed to add meta data to documents in a SharePoint library. The Event Receiver fires on ItemUpdated and is supposed to add a URL to a field.

The following code works perfect except for one little problem, when replacing a document it remains checked out.

So, when I add a new document to the library the Event Receiver adds the meta data and the document is checked in. But, when I upload a new document with the same name, thus replacing it, the document does not have any meta data and is checked out. When I then manually check in the document the meta data is added.

Here's my code.

SPField projectNameDocField = Methods.GetField(web, sharedDocumentList, Field.projectURLInternal);

SPListItem projectSiteItem = GetProjectSiteItem(web);

SPField projectNameSiteField = Methods.GetField(web.ParentWeb, projectSiteItem.ParentList, Field.projectURLInternal);

if (listItem.File.CheckOutType == SPFile.SPCheckOutType.None)
{
    listItem.File.CheckOut();

    if (projectSiteItem[projectNameSiteField.Id] != null)
    {
        SPFieldUrlValue projectNameUrlField = new SPFieldUrlValue();
        projectNameUrlField.Description = web.Title;
        projectNameUrlField.Url = web.Url;

        listItem[projectNameDocField.Id] = projectNameUrlField;
        listItem.Update();

        SPListItem updatedListItem = sharedDocumentList.GetItemById(listItem.ID);

        if (updatedListItem.File.CheckOutType != SPFile.SPCheckOutType.None)
        {
            updatedListItem.File.CheckIn("Automatisk uppdatering av metataggar", SPCheckinType.MinorCheckIn);
        }
    }
}

Thanks for helping.

Eric Herlitz
  • 25,354
  • 27
  • 113
  • 157
carruw
  • 361
  • 5
  • 10
  • 21
  • 1
    So you're asking why your document isn't automatically checked in when you replace the document? Because your code is written to only run when the document isn't checked out. If you replace the file, and the file ends up in a checked-out state, then the logic to add the metadata and check the file back in is skipped. – CBono Sep 04 '12 at 20:26

1 Answers1

1

Your event receiver only fires after an item is updated. When you upload the document to replace the existing the event is not fired until the change is checked in.

You may have more luck with the ItemUpdating event though I have not tested it. This may fire before the check-in.

You can review the full list of events here for others to try as well.

Ryan Erickson
  • 731
  • 1
  • 15
  • 23