1

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;
                }
            }
        }
    }
0

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;
            }
  • 1
    Thanks for your answer, i will test it asap! As for the using statement when working with event receivers to prevent memory leaks, i am following MSDNs best practice for Instantiating an SPSite object, please see: link Thanks again – Christoffer Nov 13 '13 at 10:24
  • You are right, there is no memory leak in this case. – Naim Murati Nov 13 '13 at 10:31
  • Naim, i did replace my code, but it still throws error when trying to enter a value to the empty column. What is the difference between my old code and yours? – Christoffer Nov 13 '13 at 11:40
  • Your code checks for null only.My code checks for null and empty. Also there is no need to set properties.Cancel to FALSE so you don't need ELSE IF in your code. What type is the column you are checking? – Naim Murati Nov 13 '13 at 12:05
  • It is a lookup column that gets the name of employees from another list as single text. – Christoffer Nov 13 '13 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.