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.