/// <summary>
        /// Search for <c>Knowledge Article</c>.
        /// <para>
        /// For more information look at
        /// <c>By Fulltext</c> : https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.fulltextsearchknowledgearticlerequest.aspx
        /// </para>
        /// </summary>
        /// <param name="searchType"><see cref="SearchTypeCode"/></param>
        /// <param name="query">
        /// Query criteria to find <c>Knowledge Article</c> with specified keyword.
        /// This parameter supports <c>QueryExpression</c> and <c>FetchXml</c>.
        /// <para>
        /// Please note that <see cref="PagingInfo"/> data must be defined in this query.
        /// </para>
        /// </param>
        /// <param name="searchText">Text to search for in <c>Knowledge Article</c> data</param>
        /// <param name="useInflection">Indicates whether to use inflectional stem matching when searching for knowledge base articles with a specified body text</param>
        /// <param name="removeDuplicates">
        /// Indicates whether to remove multiple versions of the same knowledge article in search results.
        /// Default value is <c>true</c>.
        /// </param>
        /// <param name="stateCode">
        /// State of the knowledge articles to filter the search results.
        /// Default value is <c>-1</c> (for all data). For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.fulltextsearchknowledgearticlerequest.statecode.aspx
        /// </param>
        /// <returns>
        /// <see cref="EntityCollection"/> for <c>Article</c> data.
        /// </returns>
        public EntityCollection Search(SearchTypeCode searchType, QueryBase query, string searchText, bool useInflection, bool removeDuplicates = true, int stateCode = -1)
        {
            ExceptionThrow.IfNull(query, "query");
            ExceptionThrow.IfNullOrEmpty(searchText, "searchText");

            if (query is QueryExpression)
            {
                ExceptionThrow.IfNullOrEmpty(((QueryExpression)query).EntityName, "QueryExpression.EntityName");
                ExceptionThrow.IfNotExpectedValue(((QueryExpression)query).EntityName.ToLower(), "QueryExpression.EntityName", this.EntityName.ToLower());
            }

            EntityCollection result = new EntityCollection();

            OrganizationRequest  serviceRequest  = null;
            OrganizationResponse serviceResponse = null;

            switch (searchType)
            {
            case SearchTypeCode.FullText:
                serviceRequest = new FullTextSearchKnowledgeArticleRequest()
                {
                    QueryExpression  = query,
                    RemoveDuplicates = removeDuplicates,
                    SearchText       = searchText,
                    StateCode        = stateCode,
                    UseInflection    = useInflection
                };

                serviceResponse = this.OrganizationService.Execute(serviceRequest);
                result          = ((FullTextSearchKnowledgeArticleResponse)serviceResponse).EntityCollection;
                break;
            }

            return(result);
        }
        /// <summary>
        /// Merge the information from two entity records of the same type.
        /// <para>
        /// For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.mergerequest(v=crm.8).aspx
        /// </para>
        /// </summary>
        /// <param name="mergeEntityType">Entity type that merged</param>
        /// <param name="masterRecordId">Target (merged to) of the merge operation</param>
        /// <param name="subordinateId">Id of the entity record from which to merge data</param>
        /// <param name="updatedContent">Additional entity attributes to be set during the merge operation for accounts, contacts, or leads. This property is not applied when merging incidents</param>
        /// <param name="performParentingChecks">Indicates whether to check if the parent information is different for the two entity records.</param>
        /// <returns>
        /// <see cref="MergeResponse"/>
        /// </returns>
        public MergeResponse Merge(MergeEntity mergeEntityType, Guid masterRecordId, Guid subordinateId, Entity updatedContent, bool performParentingChecks = false)
        {
            /*
             * MergeRequest CRM SDK notları
             * https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.mergerequest.aspx
             *
             * 2015-10-01 : Entities that support MERGE operation
             *   - Lead
             *   - Account
             *   - Contact
             *   - Incident
             *
             * Incident(Case) için kısıtlamalar
             *   - UpdateContent property kullanılamaz.
             *   https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.mergerequest.updatecontent.aspx
             *   https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.mergerequest_members.aspx adresinde "MergeRequest.UpdateContent" optional görünmesine rağmen ,
             *   boş gönderilince hata veriyor. Bu nedenle "Incident" için kullanımda new Entity("incident") ataması yapıyoruz
             */

            ExceptionThrow.IfGuidEmpty(subordinateId, "mergedRecordId");
            ExceptionThrow.IfGuidEmpty(masterRecordId, "mergeToRecordId");
            ExceptionThrow.IfNull(updatedContent, "updatedContent");
            ExceptionThrow.IfNotExpectedValue(updatedContent.LogicalName, mergeEntityType.Description(), "UpdatedContent.LogicalName");

            MergeRequest request = new MergeRequest()
            {
                Target                 = new EntityReference(mergeEntityType.Description(), masterRecordId),
                SubordinateId          = subordinateId,
                PerformParentingChecks = performParentingChecks,
                UpdateContent          = updatedContent
            };

            return((MergeResponse)this.OrganizationService.Execute(request));
        }
        /// <summary>
        /// Validates the state transition.
        /// <para>
        /// For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.isvalidstatetransitionrequest(v=crm.8).aspx
        /// </para>
        /// </summary>
        /// <param name="recordId">Record Id</param>
        /// <param name="entityLogicalName">Record's entity logical name</param>
        /// <param name="newStateCode"></param>
        /// <param name="newStatusCode"></param>
        /// <returns>
        /// Returns <c>true</c> if is valid. (<see cref="IsValidStateTransitionResponse.IsValid"/>)
        /// </returns>
        public bool Validate(Guid recordId, string entityLogicalName, string newStateCode, int newStatusCode)
        {
            ExceptionThrow.IfGuidEmpty(recordId, "id");
            ExceptionThrow.IfNullOrEmpty(entityLogicalName, "entityLogicalName");

            List <string> supportedEntityList = new List <string>()
            {
                "incident",
                "msdyn_postalbum",
                "msdyn_postconfig",
                "msdyn_postruleconfig",
                "msdyn_wallsavedquery",
                "msdyn_wallsavedqueryusersettings",
                "opportunity"
            };

            if (!supportedEntityList.Contains(entityLogicalName.ToLower().Trim()))
            {
                ExceptionThrow.IfNotExpectedValue(entityLogicalName, "entityLogicalName", "", string.Format("{0} is not supported for this operation. For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.isvalidstatetransitionrequest(v=crm.8).aspx", entityLogicalName));
            }

            IsValidStateTransitionRequest request = new IsValidStateTransitionRequest()
            {
                Entity    = new EntityReference(entityLogicalName, recordId),
                NewState  = newStateCode.Trim(),
                NewStatus = newStatusCode
            };

            IsValidStateTransitionResponse response = (IsValidStateTransitionResponse)this.OrganizationService.Execute(request);

            return(response.IsValid);
        }
Beispiel #4
0
        /// <summary>
        /// Validate <c>Email</c> entity with required attributes.
        /// </summary>
        /// <param name="entity"></param>
        void ValidateEmailEntity(Entity entity)
        {
            ExceptionThrow.IfNull(entity, "entity");
            ExceptionThrow.IfNullOrEmpty(entity.LogicalName, "Entity.LogicalName");
            ExceptionThrow.IfNotExpectedValue(entity.LogicalName.Trim().ToLower(), "Entity.LogicalName", "email", "Entity.LogicalName must be 'email'");

            var sender = GetFrom(entity);

            ExceptionThrow.IfNull(sender, "email.from");
            ExceptionThrow.IfGuidEmpty(sender.Id, "email.from.id");
            ExceptionThrow.IfNullOrEmpty(sender.LogicalName, "email.from.LogicalName");

            ExceptionThrow.IfNullOrEmpty(((EntityCollection)entity["to"]).Entities, "email.to");
        }
        /// <summary>
        /// Add <c>recurrence</c> information to an existing <c>appointment</c>.
        /// Please note that when you convert an existing appointment to a recurring appointment, the data from the existing appointment is copied to a new recurring appointment master instance and the existing appointment record is deleted.
        /// <para>
        /// For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.addrecurrencerequest(v=crm.7).aspx
        /// </para>
        /// </summary>
        /// <param name="appointmentId"><c>Appointment</c> Id</param>
        /// <param name="recurringAppointment">
        /// Recurring Appointment entity (<see cref="Entity"/>)
        /// </param>
        /// <returns>
        /// Newly created <c>Recurring Appointment</c> Id (<see cref="Guid"/>)
        /// </returns>
        public Guid AddRecurrence(Guid appointmentId, Entity recurringAppointment)
        {
            ExceptionThrow.IfGuidEmpty(appointmentId, "appointmentId");
            ExceptionThrow.IfNull(recurringAppointment, "recurringAppointment");
            ExceptionThrow.IfNullOrEmpty(recurringAppointment.LogicalName, "Entity.LogicalName");
            ExceptionThrow.IfNotExpectedValue(recurringAppointment.LogicalName, "Entity.LogicalName", "recurringappointmentmaster");

            AddRecurrenceRequest request = new AddRecurrenceRequest()
            {
                AppointmentId = appointmentId,
                Target        = recurringAppointment
            };

            return(((AddRecurrenceResponse)this.OrganizationService.Execute(request)).id);
        }
        void ValidateEntity(Entity entity)
        {
            ExceptionThrow.IfNull(entity, "Entity");
            ExceptionThrow.IfNullOrEmpty(entity.LogicalName, "Entity.LogicalName");

            var n = entity.LogicalName.ToLower().Trim();

            if (n.Equals("phonecall") || n.Equals("appointment") || n.Equals("letter") || n.Equals("fax") || n.Equals("email"))
            {
                //Valid
            }
            else
            {
                ExceptionThrow.IfNotExpectedValue(entity.LogicalName, "Entity.LogicalName", "phonecall - appointment - letter - fax - email");
            }
        }
        void ValidateActivity(Guid id, string activityName)
        {
            var entity      = this.Get(id);
            var channelcode = entity.GetAttributeValue <OptionSetValue>("channeltypecode");

            ExceptionThrow.IfNull(channelcode, "channeltypecode", "CampaignActivity must has 'channeltypecode' (optionset) field.");

            if (!Enum.IsDefined(typeof(CampaignActivityChannelTypeCode), channelcode.Value))
            {
                ExceptionThrow.IfNotExpectedValue(channelcode.Value, "channelcode", 0, "'channeltypecode' value is not defined in 'CampaignActivityHelper.CampaignActivityChannelTypeCode' enum");
            }

            CampaignActivityChannelTypeCode channelEnum = (CampaignActivityChannelTypeCode)channelcode.Value;

            if (!activityName.ToLower().Trim().Equals(channelEnum.Description()))
            {
                ExceptionThrow.IfNotExpectedValue(activityName.ToLower().Trim(), "Entity.LogicalName", channelEnum.Description(), string.Format("Entity.LogicalName ('{0}') and CampaignActivity ChannelCode ('{1}') must be same value.", activityName.ToLower().Trim(), channelEnum.Description()));
            }
        }
        /// <summary>
        /// Retrieve all the entity records that are related to the specified record.
        /// <para>
        /// For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.rolluprequest(v=crm.8).aspx
        /// </para>
        /// </summary>
        /// <param name="recordId">Record Id</param>
        /// <param name="entityLogicalName">Record's entity logical name</param>
        /// <param name="query">
        /// <see cref="QueryExpression"/> query for related records.</param>
        /// <param name="rollupTypeCode">
        /// The <c>Rollup Type</c> (<see cref="RollupType"/> ) for the supported entities depends on the target entity type.
        /// <c>0 : None</c>, a rollup record is not requested. This member only retrieves the records that are directly related to a parent record.
        /// <c>1 : Related</c>, a rollup record is not requested. This member only retrieves the records that are directly related to a parent record
        /// <c>2 : Extended</c>, a rollup record that is directly related to a parent record and to any descendent record of a parent record, for example, children, grandchildren, and great-grandchildren records.
        /// <para>
        /// For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.rolluptype(v=crm.8).aspx
        /// </para>
        /// </param>.
        /// <returns>
        /// <seealso cref="EntityCollection"/> for related rollup data
        /// </returns>
        public EntityCollection Rollup(Guid recordId, string entityLogicalName, QueryExpression query, RollupType rollupTypeCode)
        {
            ExceptionThrow.IfGuidEmpty(recordId, "recordId");
            ExceptionThrow.IfNullOrEmpty(entityLogicalName, "entityLogicalName");
            ExceptionThrow.IfNull(query, "query");
            ExceptionThrow.IfNullOrEmpty(query.EntityName, "query.EntityName");

            string queryEntity = query.EntityName.ToLower().Trim();

            List <string> supportedTargetList = new List <string>()
            {
                "account",
                "contact",
                "opportunity"
            };

            if (!supportedTargetList.Contains(entityLogicalName.ToLower().Trim()))
            {
                ExceptionThrow.IfNotExpectedValue(entityLogicalName, "entityLogicalName", "", string.Format("'{0}' is not supported for this operation. For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.rolluprequest(v=crm.8).aspx", entityLogicalName));
            }

            if (!RollupRequestSupportedEntityList().Contains(queryEntity))
            {
                ExceptionThrow.IfNotExpectedValue(queryEntity, "Query.EntityName", "", string.Format("'{0}' is not supported for this operation. For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.rolluprequest(v=crm.8).aspx", queryEntity));
            }

            if (!RollupRequestSupportedRollupCombinationList().Contains(new Tuple <string, string, RollupType>(queryEntity, entityLogicalName, rollupTypeCode)))
            {
                ExceptionThrow.IfNotExpectedValue(entityLogicalName, "entityLogicalName", "", string.Format("Target entity ('{0}') - Supported Entity ('{1}') - Rollup Type ('{2}') combination is not supported for this operation. For more information look at https://msdn.microsoft.com/en-us/library/microsoft.crm.sdk.messages.rolluprequest(v=crm.8).aspx", entityLogicalName, queryEntity, rollupTypeCode.ToString()));
            }

            RollupRequest request = new RollupRequest()
            {
                Target     = new EntityReference(entityLogicalName, recordId),
                Query      = query,
                RollupType = rollupTypeCode
            };

            RollupResponse serviceResponse = (RollupResponse)this.OrganizationService.Execute(request);

            return(serviceResponse.EntityCollection);
        }
Beispiel #9
0
        /// <summary>
        /// Validate <see cref="XrmRecurringAppointment"/> required fields by patterns and throws exception if values not expected.
        /// </summary>
        public void Validate()
        {
            ExceptionThrow.IfEquals(_startTime, "StartTime", DateTime.MinValue);
            ExceptionThrow.IfEquals(_endTime, "EndTime", DateTime.MinValue);
            ExceptionThrow.IfEquals(_startRangeDate, "StartRange", DateTime.MinValue);

            #region | Validate Recurrence Pattern |

            switch (_recurrencePattern)
            {
            case RecurrencePatternType.Daily:

                if (_isEveryWeekday)
                {
                    ExceptionThrow.IfEquals(_dayOfWeeksValue, "Days Of Week", 0);
                    ExceptionThrow.IfNegative(_dayOfWeeksValue, "Days Of Week");
                    ExceptionThrow.IfNotExpectedValue(_dayOfWeeksValue, "Days Of Week", 62);
                }
                else
                {
                    ExceptionThrow.IfEquals(_interval, "Interval", 0);
                    ExceptionThrow.IfNegative(_interval, "Interval");
                }

                break;

            case RecurrencePatternType.Weekly:
                ExceptionThrow.IfEquals(_dayOfWeeksValue, "Days Of Week", 0);
                ExceptionThrow.IfNegative(_dayOfWeeksValue, "Days Of Week");
                ExceptionThrow.IfGreaterThan(_dayOfWeeksValue, "Days Of Week", 127);
                ExceptionThrow.IfEquals(_interval, "Interval", 0);
                ExceptionThrow.IfNegative(_interval, "Interval");

                break;

            case RecurrencePatternType.Monthly:
                ExceptionThrow.IfEquals(_interval, "Interval", 0);
                ExceptionThrow.IfNegative(_interval, "Interval");

                if (_isFirstOption)
                {
                    ExceptionThrow.IfEquals(_dayNumber, "Day", 0);
                    ExceptionThrow.IfNegative(_dayNumber, "Day");
                }
                else
                {
                    ExceptionThrow.IfEquals(_dayOfWeeksValue, "Days Of Week", 0);
                    ExceptionThrow.IfNegative(_dayOfWeeksValue, "Days Of Week");
                    ExceptionThrow.IfGreaterThan(_dayOfWeeksValue, "Days Of Week", 127);
                }

                break;

            case RecurrencePatternType.Yearly:

                ExceptionThrow.IfEquals(_interval, "Interval", 0);
                ExceptionThrow.IfNegative(_interval, "Interval");
                ExceptionThrow.IfEquals((int)_month, "Month", 0);
                ExceptionThrow.IfNegative((int)_month, "Month");

                if (_isFirstOption)
                {
                    ExceptionThrow.IfEquals(_dayNumber, "Day", 0);
                    ExceptionThrow.IfNegative(_dayNumber, "Day");
                }
                else
                {
                    ExceptionThrow.IfEquals(_dayOfWeeksValue, "Days Of Week", 0);
                    ExceptionThrow.IfNegative(_dayOfWeeksValue, "Days Of Week");
                    ExceptionThrow.IfGreaterThan(_dayOfWeeksValue, "Days Of Week", 127);
                }

                break;
            }

            #endregion

            #region | Validate Recurrence End Pattern |

            switch (_recurrenceEndPattern)
            {
            case RecurrenceEndPatternType.NoEndDate:
                break;

            case RecurrenceEndPatternType.Occurrences:
                ExceptionThrow.IfNegative(_occurence, "Occurence");
                ExceptionThrow.IfEquals(_occurence, "Occurence", 0);

                break;

            case RecurrenceEndPatternType.WithEnddate:
                ExceptionThrow.IfEquals(_endRangeDate, "EndRange", DateTime.MinValue);

                break;
            }

            #endregion
        }