Question

I've got a list that is being displayed and edited in datasheet view and now I need to ensure that users cant edit a column if there already is a value, but if the column is empty they should be able to edit the column.

I have never worked with event receivers before so for the time I am stuck. Right now a user cant edit/delete a column value but neither add a value if the column is empty. What am I doing wrong?

public override void ItemUpdating(SPItemEventProperties properties)
    {
        base.ItemUpdating(properties);

        SPWeb currentWeb = properties.OpenWeb();
        {
            if (currentWeb.CurrentUser.IsSiteAdmin == false)
            {
                if (properties.ListItem["Column"] != null)
                {
                    properties.Status = SPEventReceiverStatus.CancelWithError;
                    properties.ErrorMessage = "blabla";
                    properties.Cancel = true;
                }
                else if(properties.ListItem["Column"] == null
                {
                properties.Cancel = false;
                properties.Status = SPEventReceiverStatus.Continue;
                }
            }
        }
    }
Was it helpful?

Solution

Replace

            if (properties.ListItem["Column"] != null)
            {
                properties.Status = SPEventReceiverStatus.CancelWithError;
                properties.ErrorMessage = "blabla";
                properties.Cancel = true;
            }
            else if(properties.ListItem["Column"] == null
            {
            properties.Cancel = false;
            properties.Status = SPEventReceiverStatus.Continue;
            }

with this

            string columnValue = properties.ListItem["Column"] == null ? null : properties.ListItem["Column"].ToString()
            if (!string.IsNullOrEmpty(columnValue))
            {
                properties.Status = SPEventReceiverStatus.CancelWithError;
                properties.ErrorMessage = "blabla";
                properties.Cancel = true;
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top