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;
                }
            }
        }
    }
Christoffer
  • 9,801
  • 2
  • 36
  • 53

1 Answers1

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;
            }
Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79
Naim Murati
  • 3,553
  • 2
  • 13
  • 17
  • 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
  • Does the event receiver works properly with a standard view? If yes then check this about a hotfix (http://social.technet.microsoft.com/Forums/en-US/84928b6d-440b-4018-a140-4956f46720ec/list-datasheet-view-itemupdating-cancel-event-end-user-error-message-support) – Naim Murati Nov 13 '13 at 12:20
  • Indeed it does work and throw error if i try to edit/update the column. I can't believe i've missed about the datasheet hotfix! Case closed, thanks for your help! – Christoffer Nov 13 '13 at 12:29