Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Birthday plugin - restrict to certain ownerid

(0) ShareShare
ReportReport
Posted on by 4,926

Hi,

I have been searching for a way to filter birthdays in CRM and found a plugin from CRM MVP Darren Liu, which would perfectly fit to our needs:

http://blogs.msdn.com/b/crm/archive/2009/02/27/creating-a-birthday-contact-list.aspx

We'll I tried to build it in Visual Studio but suddenly recognized that the code is for CRM 4, so I need to rewrite parts of the plugin, because e.g. the data types changed from CRM 4 to CRM 2011 (to the .NET data types).

Then I registered the plugin for create and update, entity contact as pre-operation steps.

The plugin works fine. It writes the birthdate to the three new fields when creating a new contact, or when updating the birthdate for a contact.

BUT: I need to restrict the plugin that it doesn't run for contacts with a certain ownerid. Unfortunately I didn't manage to write that code, because I just started writing plugins (based on SDK samples) and I didn't study development.

 

The plugin shall not run, if the owner of the contact is = "04700952-321B-E111-B232-005056830021" (-> that's the GUID of the owner).

 

I tried it like that:

Entity current = service.Retrieve("entityname", context.PrimaryEntityId, new ColumnSet("ownerid"));

EntityReference OwnerId = (EntityReference)current["ownerid"];

if (entity.Attributes.Contains("birthdate") && OwnerId.ToString() != "04700952-321B-E111-B232-005056830021") 

--> but that didn't work out and threw an exception

 

It would be really great if somebody could help me out with that issue :)

 

thx a lot in advance

Thomas

 

 

 

That's my current source code (without the restriction):

using System;

using System.Diagnostics;

using System.Linq;

using System.ServiceModel;

using Microsoft.Xrm.Sdk;

using Microsoft.Xrm.Sdk.Query;

 

namespace BirthdayPlugin

{

    public class BirthdayField : IPlugin

    {

        public void Execute(IServiceProvider serviceProvider)

        {

        IPluginExecutionContext context = (IPluginExecutionContext)

        serviceProvider.GetService(typeof(IPluginExecutionContext));

 

        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

        ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

 

        try

            {

                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

                {

                    Entity entity = (Entity)context.InputParameters["Target"];

                    tracingService.Trace("EntityName:" + entity.LogicalName);

 

                     // Verify that the target entity represents a contact.

                     // If not, this plug-in was not registered correctly.

                   

                    if (entity.LogicalName == "contact")

                        {

                       

                        //Check if birthdate is filled in CRM and if new birthday fields are empty. If yes --> copy to new birthday/month/year fields

 

                           if (entity.Attributes.Contains("birthdate"))

                             {

                             //Neue Variable die den Geburtstag eines Contacts enthält

                             DateTime birthday = (DateTime) entity["birthdate"];

                             //Update bzw. Befüllung der 3 neuen Geburtstagsfelder am Contact

                             entity ["new_birthday"] = birthday.ToLocalTime().Day;

                             entity ["new_birthmonth"] = new OptionSetValue (birthday.ToLocalTime().Month);

                             entity["new_birthyear"] = birthday.ToLocalTime().Year;

                            }                                          

                        }

                }

            }

        catch (FaultException<OrganizationServiceFault> ex)

        {

            throw new InvalidPluginExecutionException("An error occurred in Birthday plugin", ex);

        }

        }

    }

}

*This post is locked for comments

  • tpeschat Profile Picture
    tpeschat 4,926 on at
    RE: Birthday plugin - restrict to certain ownerid

    Thx Guido Preite.

    Finally I figured out how to write that code.

    In the create event I do the service.Retrieve and in the update event I additionally added a pre-image.

    br Thomas

  • tpeschat Profile Picture
    tpeschat 4,926 on at
    RE: Birthday plugin - restrict to certain ownerid

    Hi,

    but it also doesn't work in the create event and what I read at your link there is no way to create a pre or post image for a pre-operation step for a create message.

    br Thomas

  • tpeschat Profile Picture
    tpeschat 4,926 on at
    RE: Birthday plugin - restrict to certain ownerid

    Hi,

    I now changed it to the suggestion of Mahendar Pal and now when rebuiliding the solution it doesn't throw an execption.

    The plugin still works, but it also runs for the user wit the GUID I want to ignore ...

    I assume there is an issue with comparing:

      Guid skipOwner = new Guid("04700952-321B-E111-B232-005056830021");

    with the value returned by:

     EntityReference owner = entity.GetAttributeValue<EntityReference>("ownerid");

    thx a lot for your support

    br Thomas

    That's my current source code:

    using System;

    using System.Diagnostics;

    using System.Linq;

    using System.ServiceModel;

    using Microsoft.Xrm.Sdk;

    using Microsoft.Xrm.Sdk.Query;

    namespace BirthdayPlugin

    {

       public class BirthdayField : IPlugin

       {

           public void Execute(IServiceProvider serviceProvider)

           {

           IPluginExecutionContext context = (IPluginExecutionContext)

           serviceProvider.GetService(typeof(IPluginExecutionContext));

           IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));

           IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

           ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

           try

               {

                   if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)

                   {

                       Entity entity = (Entity)context.InputParameters["Target"];

                       tracingService.Trace("EntityName:" + entity.LogicalName);

                        // Verify that the target entity represents a contact.

                        // If not, this plug-in was not registered correctly.

                       if (entity.LogicalName == "contact")

                           {

                           //Variable skipOwner = "CZ_OWNER" --> damit Plugin nicht für CZ läuft

                           Guid skipOwner = new Guid("04700952-321B-E111-B232-005056830021");

                           //Get OwnerId from Target Entity

                              //EntityReference owner = (EntityReference)entity.Attributes["ownerid"];

                           EntityReference owner = entity.GetAttributeValue<EntityReference>("ownerid");

                           //Check if owner isn't skipOwner

                           if (owner.Id != skipOwner)

                           {

                               //Check if the plugin context contains the attribute birthday (=create or update of birthdate in CRM). If yes --> copy to new birthday/month/year fields                          

                               if (entity.Attributes.Contains("birthdate"))

                               {

                                   //Neue Variable die den Geburtstag eines Contacts enthält

                                   DateTime birthday = (DateTime)entity["birthdate"];

                                   //Update bzw. Befüllung der 3 neuen Geburtstagsfelder am Contact

                                   entity["new_birthday"] = birthday.ToLocalTime().Day;

                                   entity["new_birthmonth"] = new OptionSetValue(birthday.ToLocalTime().Month);

                                   entity["new_birthyear"] = birthday.ToLocalTime().Year;

                               }

                           }              

                           }

                   }

               }

           catch (FaultException<OrganizationServiceFault> ex)

           {

               throw new InvalidPluginExecutionException("An error occurred in Birthday plugin", ex);

           }

           }

       }

    }

  • Mithilesh Kumar Profile Picture
    Mithilesh Kumar 10,047 on at
    RE: Birthday plugin - restrict to certain ownerid

    Hi Thomas,

    You will have to make use of Id properties of Entity Reference.

    Once you get the Id, make use of LINQ Query to get the value from SystemUser Entity with the ID you fetched.

    You can make use of EarlyBound Classes for LINQ Queries.

    Thanks

  • tpeschat Profile Picture
    tpeschat 4,926 on at
    RE: Birthday plugin - restrict to certain ownerid

    Hi Mithilesh,

    thx for your response.

    It throws the same error with a capital V.

    Actually the method "Value" isn't offered for entity.Attributes (see attached screenshot)

    br Thomas

  • Mithilesh Kumar Profile Picture
    Mithilesh Kumar 10,047 on at
    RE: Birthday plugin - restrict to certain ownerid

    Thomas,

    It should be

    EntityReference owner = ((EntityReference)(entity.Attributes[“ownerid”])).Value;

    "value" with capital "V".

    Thanks

  • Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Birthday plugin - restrict to certain ownerid

    Hello,

    Try this

    EntityReference owner = entity.GetAttributeValue<EntityReference>("ownerid");

  • tpeschat Profile Picture
    tpeschat 4,926 on at
    RE: Birthday plugin - restrict to certain ownerid

    Hi,

    the following part throws an error:

    //Get OwnerId from Target Entity

    EntityReference owner = (EntityReference)entity.Attributes["ownerid"].value;

    Error 1 'object' does not contain a definition for 'value' and no extension method 'value' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) \\p1at.s-group.cc\0188\users\s81na\daten\visual studio 2010\Projects\BirthdayPlugin\BirthdayPlugin\Class1.cs 38 95 BirthdayPlugin

    Please help

    thx Thomas

  • tpeschat Profile Picture
    tpeschat 4,926 on at
    RE: Birthday plugin - restrict to certain ownerid

    Thx a lot for your quick response. I'll try it out tomorrow morning and let you know if it solved my problem ;)

    ;) Thomas

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Daivat Vartak – Community Spotlight

We are honored to recognize Daivat Vartak as our March 2025 Community…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,407 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans