Pergunta

I am currently developing an Event Handler for SharePoint 2010 which sets defaults for Document Libraries on creation (content types, version settings etc). I am having a problem with Save Conflicts when creating Document Libraries through the GUI, the event receiver doesn't always run after the GUI is done saving the list.

I assumed ListAdded would be called after the GUI has properly finished creating the list and saved it, but it doesn't seem to be the case. I have tried setting properties in the ListAdding function, but changes are not saved (as the list isn't created yet).

I receive the following message when debugging in Visual Studio:

Save Conflict.
Your changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes.

with the stack trace of:

Microsoft.SharePoint.SPException was unhandled by user code ErrorCode=-2130575305 NativeErrorMessage=FAILED hr detected (hr = 0x81020037)

NativeStackTrace="" Message=Save Conflict.

Your changes conflict with those made concurrently by another user. If you want your changes to be applied, click Back in your Web browser, refresh the page, and resubmit your changes. Source=Microsoft.SharePoint StackTrace: at Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx) at Microsoft.SharePoint.Library.SPRequest.SetListProps(String bstrUrl, String bstrListName, Boolean bMigrate) at Microsoft.SharePoint.SPList.Update(Boolean bFromMigration) at Company.SharePoint.EventReceivers.ListCreate.CompanyListCreateEventReceiver.ListAdded(SPListEventProperties properties) at Microsoft.SharePoint.SPEventManager.RunListEventReceiver(SPListEventReceiver receiver, SPUserCodeInfo userCodeInfo, SPListEventProperties properties, String receiverData) at Microsoft.SharePoint.SPEventManager.RunListEventReceiverHelper(Object receiver, SPUserCodeInfo userCodeInfo, Object properties, SPEventContext context, String receiverData) at Microsoft.SharePoint.SPEventManager.<>c__DisplayClassc`1.b__6() at Microsoft.SharePoint.SPSecurity.RunAsUser(SPUserToken userToken, Boolean bResetContext, WaitCallback code, Object param) InnerException: System.Runtime.InteropServices.COMException

and the ULS logs of:

02/25/2011 11:12:52.06 w3wp.exe (0x363C) 0x219C SharePoint Foundation General 8e2s Medium Unknown SPRequest error occurred. More information: 0x8007047e f35857af-3d3a-462c-86a6-de24d3b3d8d3
02/25/2011 11:12:52.06 w3wp.exe (0x363C) 0x219C SharePoint Foundation General 72k4 Medium 0x8007047e f35857af-3d3a-462c-86a6-de24d3b3d8d3
02/25/2011 11:12:52.06 w3wp.exe (0x363C) 0x219C SharePoint Foundation General 8kh7 High 0x8007047e f35857af-3d3a-462c-86a6-de24d3b3d8d3

Is there a way of ensuring the conflict doesn't happen, or a different way of setting or accessing the list properties which won't conflict. I have tired creating a seperate instance of SPSite/SPWeb, but that still has the same issue.

A simple example of the code which is failing is:

  public class CompanyListCreateEventReceiver : SPListEventReceiver
    {

        public override void ListAdded(SPListEventProperties properties)
        {
            base.ListAdded(properties);

            SPList l_list = properties.List;
            l_list.Title = "Changed title";
            l_list.Update();

        }
}

Any help would be appreciated. Thanks!

Foi útil?

Solução

You have to register the ItemAdded event as Synchronous event. SPEventReceiverSynchronization.Synchronous

You can define it programmatically: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.speventreceiverdefinition.synchronization.aspx

Or in the Receiver element of the feature definition: http://msdn.microsoft.com/en-us/library/ff512765.aspx

Outras dicas

I've been fighting with SharePoint event receivers for some time now. A lot of pitfalls and unexpected logic in this part of SharePoint imo.

I haven't seen this exact problem before, but I can give you one suggestion anyway. In my experience, changing the properties list can sometimes have issues. That's why I want to get the list from the web instead. If this fails (it worked for me), try instantiate a new SPWeb from Web.Url as well.

In this case, I suggest that you try the following approach:

 public class CompanyListCreateEventReceiver : SPListEventReceiver
{

    public override void ListAdded(SPListEventProperties properties)
    {
        base.ListAdded(properties);

        SPList l_list = properties.Web.Lists[properties.List.ID];
        l_list.Title = "Changed Title";
        l_list.Update();
    }
}

I guess, you may need to specify "Synchronous" in your element.xml file, refer http://sharepointconnoisseur.blogspot.com/2011/04/save-conflict-error-when-creating-list.html

Try to use disable event and enable event methods.

2) (Useful in from updating Item from event handler)

Item[FieldName] = FieldValue;
this.DisableEventFiriing();
item.SystemUpdate(false);
or
item.Update();
this.EnableEventFiring();
//must enable event firing if we are disable it
Licenciado em: CC-BY-SA com atribuição
Não afiliado a sharepoint.stackexchange
scroll top