Tuesday, 10 January 2012

Set the IFRAME height and width using javascript in CRM 2011

document.getElementById("IFRAME_name").style.width = 125;
document.getElementById("IFRAME_name").style.height = 125;


This code is done our senior Techincal Consultant Pradeep

Monday, 9 January 2012

AssginSecurity Roles for existing user in crm2011 by C#

 protected void AssignSecurityRole(Guid prmUserId, Guid prmSecurityRoleId, IOrganizationService prmCrmWebService)
        {
            // Create new Associate Request object for creating a N:N link between User and Security
            AssociateRequest wod_AssosiateRequest = new AssociateRequest();
           
            // Create related entity reference object for associating relationship
            // In our case we will pass (SystemUser) record reference  

            wod_AssosiateRequest.RelatedEntities = new EntityReferenceCollection();
            wod_AssosiateRequest.RelatedEntities.Add(new EntityReference("systemuser", prmUserId));


            // Create new Relationship object for System User & Security Role entity schema and assigning it 
            // to request relationship property

            wod_AssosiateRequest.Relationship = new Relationship("systemuserroles_association");


            // Create target entity reference object for associating relationship
            wod_AssosiateRequest.Target = new EntityReference("role", prmSecurityRoleId);


            // Passing AssosiateRequest object to Crm Service Execute method for assigning Security Role to User
            prmCrmWebService.Execute(wod_AssosiateRequest);
        }
This code available in MSDN Library.

Creating User in CRM-2011 using C#


protected void Create_CRM_User(string domainname, string firstname, string lastname, string emailid, string contactnumber, ITracingService tracingService, IOrganizationService service)
        {            
            tracingService.Trace("Inside Create_CRM_User");
            const string BusinessUnitEntityName = "businessunit";
            const string BusinessUnitIdColumnName = "businessunitid";
            const string ParentBusinessUnitIdColumnName = "parentbusinessunitid";
            const string SystemUserEntityName = "systemuser";
            const string FirstNameColumnName = "firstname";
            const string DomainNameColumnName = "domainname";
            const string LastNameColumnName = "lastname";
            const string InternalEmailAddress = "internalemailaddress";
            const string TelephoneNumber = "address1_telephone1";

            Guid userId = Guid.Empty;


            QueryExpression businessUnitQuery = new QueryExpression
            {
                EntityName = BusinessUnitEntityName,
                ColumnSet = new Microsoft.Xrm.Sdk.Query.ColumnSet(BusinessUnitIdColumnName),

                Criteria =
                {
                    Conditions =
                    {
                        new ConditionExpression(ParentBusinessUnitIdColumnName,
                            ConditionOperator.Null)
                    }
                }

            };

            // Get the business unit id from the returned entity
            EntityCollection entities = service.RetrieveMultiple(businessUnitQuery);

            Guid defaultBusinessUnitId = entities[0].Id;

            string new_domainname = createuser_domain + domainname;
            //Populate an entity with data for a new system user.
            Entity entity = new Entity(SystemUserEntityName);
            entity.Attributes.Add(DomainNameColumnName, new_domainname);
            entity.Attributes.Add(FirstNameColumnName, firstname);
            entity.Attributes.Add(LastNameColumnName, lastname);
            entity.Attributes.Add(InternalEmailAddress, emailid);
            entity.Attributes.Add(TelephoneNumber, contactnumber);
           
            entity.Attributes.Add(BusinessUnitIdColumnName, new EntityReference
            {
                Id = defaultBusinessUnitId,
                Name = BusinessUnitEntityName,
                LogicalName = BusinessUnitEntityName
            });

            userId = service.Create(entity);

            string crm_successvar = "Created CRM User with GUID " + userId.ToString();
            tracingService.Trace(crm_successvar);


          
        }
This code is done by our senior Techincal Consultant Palani

Creating User in Active Directory using C# code (CRM 2011)


public void Create_AD_User(string username, string password, string firstname, string lastname, string emailid, string contactnumber, ITracingService tracingService)
        {
            tracingService.Trace("Inside Create_AD_User");
            try
            {
                //  Step 1: Create a Directory Entry Object, starting at point in the AD Structure that we would like
                //          to add the user to.  This will come from the path that we selected from the Show AD Structure Tab
                //          and DataGrid.
                DirectoryEntry currentADObject = new DirectoryEntry(adrootpath);

                //  Step 2: Make sure that the AD Object that we are adding to is a container.  Meaning that it can
                //          hold other AD Objects (e.g., users, groups, etc.)
                if (currentADObject.SchemaEntry.Name == "container")
                {
                    //  Step 3: Create a User Object of type User, to be added to the Children colllllection of the
                    //          current AD Object that we created in Step 1
                    DirectoryEntry newUserObject = currentADObject.Children.Add("CN=" + username, "User");

                    //  Step 4: Check to see if the user already exists, and if so, we will overwrite it for Demo simplicity.
                    //          In the real world, you could prompt the user to overwrite or not and code accordingly.
                    if (DirectoryEntry.Exists(newUserObject.Path))
                    {
                        // Step 4a: Remove the user object first
                        currentADObject.Children.Remove(new DirectoryEntry(newUserObject.Path));
                    }

                    //  Step 5: Add the user optional and required properties (sAMAccountName is ALWAYS REQUIRED!!)
                    newUserObject.Properties["sAMAccountName"].Add(username);
                    newUserObject.Properties["givenName"].Add(firstname); //Dont change the givenName and sn, they are the actual active directory properties
                    newUserObject.Properties["sn"].Add(lastname);
                    newUserObject.Properties["mobile"].Add(contactnumber);
                    newUserObject.Properties["mail"].Add(emailid);
                   
            
                    //  Step 6: Commit the changes to the AD data store
                    newUserObject.CommitChanges();

                    //  Step 7: Set the password for the new account, which can only be done AFTER the account exists!
                    //          We are using the "Invoke" method on the newUserObject, which uses Native AD Object under the hood to set
                    //          the password.  I've only seen this done, using the Invoke method, which is why I've used it here
                    newUserObject.Invoke("setPassword", password);

                    //  Step 8: Enable the user, if the user wants to, by setting the userAccountControl property
                    //          to the magical value of 0x0200.  The disable-user value is 0x0002
                    newUserObject.Properties["userAccountControl"].Value = 0x0200;
                    newUserObject.CommitChanges();

                    string ad_successvar = "User: " + username + " successfully created in AD!";
                    tracingService.Trace(ad_successvar);
                    
                }
                else
                {
                    string ad_unsuccessvar = "You must select an AD Object that is a container, user creation in AD Failed";
                    tracingService.Trace(ad_unsuccessvar);
                }
            }

            catch (Exception ex)
            {
                string ad_exception = ex.Message + " some exception in creating the user in AD";
                tracingService.Trace(ad_exception);
            }

        }

This code  available  in MSDN Library .

Passing the value through Unsecure config to plugin CRM 2011


This is XML syntax code used in Unsecureconfig to pass the value to plugin.
<Settings>
        <setting name="adusername">
            <value>administrator</value>
        </setting>
       
<setting name="adpassword">
            <value>gangadhar_123</value>
        </setting>

<setting name="addomain">
            <value>locusit</value>
        </setting>
 
  </Settings>
 Below code is used to fetch the value from XML . paste this code in plugin(C#).

 private static string adusername = null;
       private static string adpassword  = null;
       private static string addomain = null;
   
public Class1(string unsecureConfig, string secureConfig)
       {
           XmlDocument doc = new XmlDocument();
           doc.LoadXml(unsecureConfig);

           adusername = GetValueNode(doc, "adusername");
           adpassword = GetValueNode(doc, "adpassword");
           addomain = GetValueNode(doc, "addomain");
 // adusername, password and addomain can make use of it anywhere in the solution or plugin .         
        }

        private static string GetValueNode(XmlDocument doc, string key)
        {
            XmlNode node = doc.SelectSingleNode(String.Format("Settings/setting[@name='{0}']", key));

            if (node != null)
            {
                return node.SelectSingleNode("value").InnerText;
            }
            return string.Empty;
        }

This is code done by our senior Technical Consultant Palani

Monday, 2 January 2012

Action which can be perform OnSave Event -Javascript -CRM 2011

This property lets you know for which reason the onSave() method has been called: 
Here is the list of event:
  •       None : 0
  •      Save : 1
  •    SaveAndClose : 2
  •    Delete : 3
  •     Load : 4
  •     Deactivate : 5
  •    Reactivate : 6
  •   Email Send : 7
  • Email Reply : 8
  •    Email Forward : 9
  •    Kb Submit : 10
  •    Kb Reject : 11
  •      Kb Publish : 12
  •      Kb UnPublish : 13
  •     Kb Rate : 14
  •     Lead Unqualify : 15
  •    Lead Qualify : 16
  •       Quote Accept : 17
  •      Quote CreateOrder : 18
  •      Order ProcessOrder : 19
  •       Opportunity AddRelatedOrder : 21
  •      Opportunity AddRelatedQuote : 22
  •      Opportunity AddRelatedInvoice : 23
  •       Quote CreateRevision : 24
  • ·         Quote CloseQuote : 25
  •       Order CancelOrder : 26
  •       Invoice Close : 27
  •      Quote GetProducts : 28
  •       Quote Activate : 29
  •     Email ReplyAll : 30
  •         Contract Hold : 31
  •    Contract ReleaseHold : 32
  •       Contract Cancel : 33
  •       Contract Renew : 34
  •       Product ConvertToKit : 35
  • ·         Product ConvertFromKit : 36
  •      ContractDetail Cancel : 37
  •     Contract Invoice : 38
  •        Contract Clone : 39
  •      Incident Cancel : 40
  •         Email Assign : 41
  •   Change SalesStage : 42
  •        SalesOrder GetProducts : 43
  •      InvoiceGetProducts : 44
  •       TemplateMakeOrgAvailable : 45
  •       TemplateMakeOrgUnavailable : 46
  •       Assign : 47
  •       IncidentAssignToUser : 49
  •       OrderLock : 50
  •       OrderUnlock : 51
  •        InvoiceLock : 52
  •      InvoiceUnlock : 53
  • ConvertResponse : 54
  •      ReportMakeOrgAvailable : 60
  •      ReportMakeOrgUnavailable : 61
  •         WorkflowAddCheckStep : 62
  •        WorkflowUpdateCondition : 63
  •       WorkflowCreateAction : 64
  •       SendInvite : 65
  •        WorkflowAddElseIfStep : 66
  •       WorkflowAddElseStep : 67
  •     WorkflowDeleteStep : 68
You can then use it like :
if(event.Mode == 16)
{
// Check whatever you want
}

So, just in case, if you don't want which event is triggered, just add the
following line
in the onSave event of the form:
alert(event.Mode);