locked
Plugin Help with Public/Protected Voids?? RRS feed

  • Question

  • Hi guys, I'm a bit at a loss here. I've never ventured into how these voids work and I need to use variables from one in the other. I want this plugin to only run when 1 of 3 specific people update a Task. The plugin fails if anyone else does it because they don't have permissions to the regarding entity. So I want to catch it and only run with specific people use it.

    I found some code that demonstrates how to get the current user id but I'm having issues getting it to work. I get an error message that says 'TaskUpdate.JobCostUpdate.PostTaskUpdate.Execute(System.IServiceProvider)' hides inherited member 'TaskUpdate.JobCostUpdate.Plugin.Execute(System.IServiceProvider)'. Use the new keyword if hiding was intended.'

    Code is below. Thanks for any help.

    namespace TaskUpdate.JobCostUpdate
    {
        using System;
        using System.Linq;
        using System.ServiceModel;
        using Microsoft.Xrm.Sdk;
        using Microsoft.Xrm.Sdk.Client;
        using Microsoft.Xrm.Sdk.Query;
    
        public class PostTaskUpdate : Plugin
        {
            Guid jocelyn = new Guid("ABD80D9F-F3B8-E211-AC09-12A32B1E376A");
            Guid trevor = new Guid("E200F73B-57CC-E111-B35F-12A32B1E376A");
            Guid Lois = new Guid("2B13D49B-BD82-E311-990D-12A32B1E376A");
            Guid userId;
            public PostTaskUpdate()
                : base(typeof(PostTaskUpdate))
            {
                base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "task", new Action<LocalPluginContext>(ExecutePostTaskUpdate)));
    
            }
    
            public void Execute(IServiceProvider serviceProvider)
            {
                IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                userId = context.InitiatingUserId;
            }
    
            protected void ExecutePostTaskUpdate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
     
                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService service = localContext.OrganizationService;
    
                var ServiceContext = new OrganizationServiceContext(service);
                ITracingService tracingService = localContext.TracingService;
    
                if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
                {
                    if (userId == trevor || userId == Lois || userId == jocelyn)
                    {
                        Entity entity = (Entity)context.InputParameters["Target"];
                        Entity pEntity = service.Retrieve("task", entity.Id, new ColumnSet("new_billingtaskid"));
                        string postImage = "";
    
                        if (pEntity.Attributes.Contains("new_billingtaskid"))
                        {
                            postImage = (string)pEntity["new_billingtaskid"];
                        }
    
                        if (entity.Attributes.Contains("new_billingtaskid"))
                        {
                            postImage = (string)entity["new_billingtaskid"];
                        }
    
                        var res = from q in ServiceContext.CreateQuery("new_salescommissionslineitems")
                                  where q["new_billingtask"].Equals(entity.Id)
                                  select q;
    
                        foreach (var q in res)
                        {
                            q["new_jobcostid"] = postImage;
                            ServiceContext.UpdateObject(q);
                        }
                    }
                    ServiceContext.SaveChanges();
                }
            }
        }
    }
            
    
                
            
     
        
    
    
    
    

    Tuesday, January 21, 2014 9:10 PM

Answers

  • The problem is you're inheriting from a class called "Plugin" that already defines and calls the Execute method. Simply remove that execute method completely from the above class. The reason is this, you are already telling the base class (Plugin) to call ExecutePostTaskUpdate in this line:

    base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "task", new Action<LocalPluginContext>(ExecutePostTaskUpdate)));

    So when the plugin fires -> the Execute will fire in the base class -> and then call that method for you.

    Next up, move this line into the ExecutePostTaskUpdate method so that you can store off the userId:

    userId = context.InitiatingUserId;

    There is no need to store it as a class member variable, just declare it in the method.

    Once you do all of that you should be fine.






    Tuesday, January 21, 2014 11:45 PM
  • Yes, you're nearly there. Firstly, you've found a small bug in Microsoft's plugin template that somehow has never got fixed! And that's the ServiceProvider property never get's set.

    Anyway, you don't need the following 3 lines at all:

                IServiceProvider serviceProvider = localContext.ServiceProvider;
                IPluginExecutionContext context2 = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    

    You already have a plugin execution context so replace the following line to this as well:

    userId = context.InitiatingUserId;

    Conor



    Wednesday, January 22, 2014 9:15 PM

All replies

  • The problem is you're inheriting from a class called "Plugin" that already defines and calls the Execute method. Simply remove that execute method completely from the above class. The reason is this, you are already telling the base class (Plugin) to call ExecutePostTaskUpdate in this line:

    base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "task", new Action<LocalPluginContext>(ExecutePostTaskUpdate)));

    So when the plugin fires -> the Execute will fire in the base class -> and then call that method for you.

    Next up, move this line into the ExecutePostTaskUpdate method so that you can store off the userId:

    userId = context.InitiatingUserId;

    There is no need to store it as a class member variable, just declare it in the method.

    Once you do all of that you should be fine.






    Tuesday, January 21, 2014 11:45 PM
  • Hello Conor, thanks a lot for you help. I think I am close but I am still getting an issue with unassigned local variable "serviceProvider", if I remove that I get "the name serviceProvider does not exist in the current context". I also tried "IServiceProvider serviceProvider = localContext.ServiceProvider;" which compiled successfully. But then I get a business process error when trying to run the plugin that says "Object reference not set to an instance of an object".

    Here is how I adjusted the code. Thanks again!

    namespace TaskUpdate.JobCostUpdate
    {
        using System;
        using System.Linq;
        using System.ServiceModel;
        using Microsoft.Xrm.Sdk;
        using Microsoft.Xrm.Sdk.Client;
        using Microsoft.Xrm.Sdk.Query;
    
        public class PostTaskUpdate : Plugin
        {
            
            public PostTaskUpdate()
                : base(typeof(PostTaskUpdate))
            {
                base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(40, "Update", "task", new Action<LocalPluginContext>(ExecutePostTaskUpdate)));
    
            }
            
    
            protected void ExecutePostTaskUpdate(LocalPluginContext localContext)
            {
                if (localContext == null)
                {
                    throw new ArgumentNullException("localContext");
                }
     
                IPluginExecutionContext context = localContext.PluginExecutionContext;
                IOrganizationService service = localContext.OrganizationService;
    
                IServiceProvider serviceProvider = localContext.ServiceProvider;
                IPluginExecutionContext context2 = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    
                Guid jocelyn = new Guid("ABD80D9F-F3B8-E211-AC09-12A32B1E376A");
                Guid trevor = new Guid("E200F73B-57CC-E111-B35F-12A32B1E376A");
                Guid lois = new Guid("2B13D49B-BD82-E311-990D-12A32B1E376A");
                Guid userId;
                userId = context2.InitiatingUserId;
    
                var ServiceContext = new OrganizationServiceContext(service);
                ITracingService tracingService = localContext.TracingService;
    
                if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
                {
                    if (userId == trevor || userId == lois || userId == jocelyn)
                    {
                        Entity entity = (Entity)context.InputParameters["Target"];
                        Entity pEntity = service.Retrieve("task", entity.Id, new ColumnSet("new_billingtaskid"));
                        string postImage = "";
    
                        if (pEntity.Attributes.Contains("new_billingtaskid"))
                        {
                            postImage = (string)pEntity["new_billingtaskid"];
    
                            var res = from q in ServiceContext.CreateQuery("new_salescommissionslineitems")
                                      where q["new_billingtask"].Equals(entity.Id)
                                      select q;
    
                            foreach (var q in res)
                            {
                                q["new_jobcostid"] = postImage;
                                ServiceContext.UpdateObject(q);
                            }
                        }
    
                        if (entity.Attributes.Contains("new_billingtaskid"))
                        {
                            postImage = (string)entity["new_billingtaskid"];
    
                            var res = from q in ServiceContext.CreateQuery("new_salescommissionslineitems")
                                      where q["new_billingtask"].Equals(entity.Id)
                                      select q;
    
                            foreach (var q in res)
                            {
                                q["new_jobcostid"] = postImage;
                                ServiceContext.UpdateObject(q);
                            }
                        }
    
                        
                    }
                    ServiceContext.SaveChanges();
                }
            }
        }
    }
            
    
                
            
     
        
    
    
    
    

    Wednesday, January 22, 2014 3:42 PM
  • Yes, you're nearly there. Firstly, you've found a small bug in Microsoft's plugin template that somehow has never got fixed! And that's the ServiceProvider property never get's set.

    Anyway, you don't need the following 3 lines at all:

                IServiceProvider serviceProvider = localContext.ServiceProvider;
                IPluginExecutionContext context2 = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
                IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
    

    You already have a plugin execution context so replace the following line to this as well:

    userId = context.InitiatingUserId;

    Conor



    Wednesday, January 22, 2014 9:15 PM
  • Awesome, thank you so much for your help!
    Thursday, January 23, 2014 3:01 PM