Monday 23 September 2013

Extracting the SSRS Report in PDF programmatically using Plugin or Wokflow in mscrm 2011

Extracting the Report in PDF programmatically.
Code snippet:
ReportGenerator rg = new ReportGenerator("http://crm/Reportserver/ReportExecution2005.asmx",
                new NetworkCredential("administrator", "Password", "contoso"));

            ParameterValue[] parameters = new ParameterValue[1];
            parameters[0] = new ParameterValue();
            parameters[0].Name = "P1";
            parameters[0].Value = string.Format("Select * From FilteredQuote Where QuoteId = '{0}'",
                context.PrimaryEntityId);

            byte[] reportresult = rg.Render("/contoso_mscrm/quote", FormatType.PDF, parameters);

            Entity attachment = new Entity("activitymimeattachment");
            attachment["objectid"] = Email.Get<EntityReference>(executionContext);
            attachment["objecttypecode"] = "email";
            attachment["filename"] =
            attachment["subject"] = "Quote.pdf";
            attachment["body"] = System.Convert.ToBase64String(reportresult);

Ref Links:




Tuesday 10 September 2013

Get DateTime formate from Retrieved stringDateAttribute Using Odata Java Script – MS CRM 2011


Get DateTime formate from Retrieved stringDateAttribute  Using Odata Java Script:
Some times you may get date time value as milli seconds in the below format as “/Date(1371723451000)/”
var RetrievedDate = ”/Date(1371723451000)/”;
to convert this into normal date you need to pass “RetrievedDate” string to the below highlited area.
var DateValue = new Date(parseInt(RetrievedDate.replace(“/Date(“, “”).replace(“)/”, “”), 10));
Now you will get DateValue in UTC date time format.
Cheers,

BY Vivek

MSCRM 2011 Plugin in user context to avail configured filed level security

var context = serviceProvider.GetService(typeof(IPluginExecutionContext)) as IPluginExecutionContext;
var target = new Entity("account");
target["name"] = "test 3";
target["new_secretcode"] = "1234";
var sf = serviceProvider.GetService(typeof(IOrganizationServiceFactory)) as IOrganizationServiceFactory;
var service = sf.CreateOrganizationService(Guid.Empty);
service.Create(target);

var service = sf.CreateOrganizationService(Guid.Empty);
To execute plugin in user context: The Create OrganizationService call that we pass Guid.Empty. Passing Guid.Empty instructs the method to give back an organization service configured to run in the context of the user


var service = sf.CreateOrganizationService(null);
To execute plugin in System context : we passed null to that method the organization service would be configured to run in the system context and the plugin would be able to update the secured field.


Thursday 5 September 2013

Retrieve OptionSet Text in CRM 2011 using C# in Plugin

Retrieve Normal(Local) Option Set Text


// Get Normal option set Text
string optionsetText = entity.FormattedValues["new_optionset"];

or

string optionsetText = entity.GetFormattedAttributeValue("new_optionset");

Retrieve Global Option Set Text

int OptionsetValue = ((Microsoft.Xrm.Sdk.OptionSetValue)entity["new_localoptionset"]).Value;
string GlobaloptionsetText= GetOptionsetText(entity, service, "new_globaloptionset", OptionsetValue );



   // Retrieves Global Option set Selected Text
        // Parameters: 1. Entity Name   2. Service  3. Global Option Set Name   4. optionset selected value
        public string GetOptionsetText(Entity entity, IOrganizationService service,string  optionsetName,int optionsetValue)
        {
            string optionsetSelectedText = string.Empty;
            try
            {


                RetrieveOptionSetRequest retrieveOptionSetRequest =
                    new RetrieveOptionSetRequest
                    {
                        Name = optionsetName
                    };

                // Execute the request.
                RetrieveOptionSetResponse retrieveOptionSetResponse =
                    (RetrieveOptionSetResponse)service.Execute(retrieveOptionSetRequest);

                // Access the retrieved OptionSetMetadata.
                OptionSetMetadata retrievedOptionSetMetadata = (OptionSetMetadata)retrieveOptionSetResponse.OptionSetMetadata;

                // Get the current options list for the retrieved attribute.
                OptionMetadata[] optionList = retrievedOptionSetMetadata.Options.ToArray();
                foreach (OptionMetadata optionMetadata in optionList)
                {
                    if (optionMetadata.Value == optionsetValue)
                    {
                        optionsetSelectedText = optionMetadata.Label.UserLocalizedLabel.Label.ToString();
break;
                    }
                }
            }
            catch (Exception)
            {


                throw;
            }
            return optionsetSelectedText;
        }

Monday 19 August 2013

How to hide the “New” or “Properties” button from the CRM 2011 Lookup Dialog

How to hide the “New” or “Properties” button from the CRM 2011 Lookup Dialog.

Here’s the new way to hide the “New” button:

var lookupControl = Sys.Application.findComponent("some_lookupid");
 
  if (lookupControl != null)
  {
       lookupControl._element._behaviors[0].AddParam("ShowNewButton", 0);

   }