Thursday, November 3, 2011

Renaming the attached file CRM 2011

    I had a requirement in crm 2011 to rename the file what ever we are attaching should be renamed to be a specific name i have achieved this through a plugin  here the code for that plugin

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.IO;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;

namespace FileRenaming
    public class filerename : IPlugin
        public void Execute(IServiceProvider serviceProvider)
            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            // The InputParameters collection contains all the data passed in the message request.
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                // Obtain the target entity from the input parmameters.
                Entity entity = (Entity)context.InputParameters["Target"];
                    IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                    IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
                    entity["filename"] = "Crm" + "test" + ".doc";                   
                catch (InvalidPluginExecutionException)
                    throw new InvalidPluginExecutionException("Error Occured");

register this plugin as post synchronous event and it sould be in create message for the primary entity as

No comments:

Post a Comment