Question

Prevent saving a record using EntityEventsBeforeArgs

Dear Community,

Based on attachment types, we are trying to prevent it from saving in the database.

We have written a code as below, but it doesnt seem to work.

Could you please help with this?

public override void OnInserting(object sender, EntityBeforeEventArgs e) 

        {

            base.OnInserting(sender, e);

            //var userConnection = entity.UserConnection;

            var entity = (Entity)sender;

      

            if (IsBlackListedFileType(entity))

            {

                

                e.IsCanceled =true;

            }

            else

            {

                if (IsPsuedoWhiteListedFileType(entity))

                {

                    

                    e.IsCanceled = true;

                }

                else

                {

                  

                    e.IsCanceled = false;

                }

                

            }

            

        }

Also could you please explain how to get the type (.png, jpeg etc ) and the data of the file?

 

Thanks

Like 0

Like

3 comments

From your description, you want to compare the list of file extensions to the field Type of the File. However, the Attachment Type lookup doesn't contain file extensions (http://prntscr.com/r758wk). To fix it, instead of comparing the extension to a type, you can parse the filename to determine the extension. You can get column values using the GetTypedColumnValue method. Here is an example: 

var entity = (Entity) sender;

 if (entity.GetTypedColumnValue<Guid>("ActivityCategoryId") == new Guid("2365ae4f-58e6-df11-971b-001d60e938c6"))

                e.IsCanceled =true; 

Dennis Hudson,

I was able to get the file extension by parsing the Name column. I was wondering if it is possible to get the data (blob type) of the attachment before inserting. 

Any insight would be appreciated. 

Thanks

Update

var result = entity.GetBytesValue("Data");

GetBytesValue is a public method to fetch data.

 

Thanks

Show all comments