Search code examples
c#sharepointsharepoint-2016

the object has been updated by another user since it was last fetched


I'm added somefield so my SharePoint solution but i keep getting the error the object has been updated by another user since it was last fetched.

foreach (var field in new PageAndFieldData().Fields)
{

    var spField = web.Fields.FirstOrDefault(i => i.InternalName == field.field_name); // if dont exist = create

    if (spField == null)
    {
        var fieldXml = field.Xml;
        var newspField = web.Fields.AddFieldAsXml(fieldXml, false, AddFieldOptions.DefaultValue);
        if (groups.Contains(field.group_name))
            newspField.Group = field.group_name;
        else
        {
            newspField.Group = field.group_name;
        }
        _Ctx.Load(newspField);
        _Ctx.ExecuteQuery(); <--- This line throws the error
    }
}

Can someone point out why it keeps throwing an error.


Solution

  • Make sure field schema (fieldXml variable in your case) does not contain Version attribute otherwise SharePoint considers the field is getting updated.

    For example, the error will occur while creating a field using the following field schema:

    <Field Version='1' Type='Geolocation' DisplayName='Location' /> 
    

    and will not with:

    <Field Type='Geolocation' DisplayName='Location' />