/// <summary>
        /// Deletes the specified audience or all audience rules for the specified audience.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="audienceName">Name of the audience.</param>
        /// <param name="deleteRulesOnly">if set to <c>true</c> [delete rules only].</param>
        internal static void Delete(SPServiceContext context, string audienceName, bool deleteRulesOnly)
        {
            AudienceManager manager = new AudienceManager(context);

            if (!manager.Audiences.AudienceExist(audienceName))
            {
                throw new SPException("Audience name does not exist");
            }

            Audience audience = manager.Audiences[audienceName];

            if (audience.AudienceRules != null && deleteRulesOnly)
            {
                audience.AudienceRules = new ArrayList();
                if (audience.GroupOperation == AudienceGroupOperation.AUDIENCE_MIX_OPERATION)
                {
                    // You can't change from mixed mode using the property without setting some internal fields.
                    object audienceInfo = Utilities.GetFieldValue(audience, "m_AudienceInfo");
                    Utilities.SetPropertyValue(audienceInfo, "NewGroupOperation", AudienceGroupOperation.AUDIENCE_OR_OPERATION);
                    Utilities.SetFieldValue(audience, typeof(Audience), "m_AuidenceGroupOperationChanged", true);
                }
                audience.Commit();
                return;
            }
            if (!deleteRulesOnly)
            {
                manager.Audiences.Remove(audience.AudienceID);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Adds the rules.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <param name="audienceName">Name of the audience.</param>
        /// <param name="rules">The rules.</param>
        /// <param name="clearExistingRules">if set to <c>true</c> [clear existing rules].</param>
        /// <param name="compile">if set to <c>true</c> [compile].</param>
        /// <param name="groupExisting">if set to <c>true</c> [group existing].</param>
        /// <param name="appendOp">The append op.</param>
        /// <returns></returns>
        internal static ArrayList AddRules(SPServiceContext context, string audienceName, string rules, bool clearExistingRules, bool compile, bool groupExisting, AppendOp appendOp)
        {
            AudienceManager manager = new AudienceManager(context);

            if (!manager.Audiences.AudienceExist(audienceName))
            {
                throw new SPException("Audience name does not exist");
            }

            Audience audience = manager.Audiences[audienceName];

            /*
             * Operator        Need left and right operands (not a group operator)
             * =               Yes
             * >               Yes
             * >=              Yes
             * <               Yes
             * <=              Yes
             * Contains        Yes
             * Reports Under   Yes (Left operand must be 'Everyone')
             * <>              Yes
             * Not contains    Yes
             * AND             No
             * OR              No
             * (               No
             * )               No
             * Member Of       Yes (Left operand must be 'DL')
             */
            XmlDocument rulesDoc = new XmlDocument();

            rulesDoc.LoadXml(rules);

            ArrayList audienceRules    = audience.AudienceRules;
            bool      ruleListNotEmpty = false;

            if (audienceRules == null || clearExistingRules)
            {
                audienceRules = new ArrayList();
            }
            else
            {
                ruleListNotEmpty = true;
            }

            //if the rule is not emply, start with a group operator 'AND' to append
            if (ruleListNotEmpty)
            {
                if (groupExisting)
                {
                    audienceRules.Insert(0, new AudienceRuleComponent(null, "(", null));
                    audienceRules.Add(new AudienceRuleComponent(null, ")", null));
                }

                audienceRules.Add(new AudienceRuleComponent(null, appendOp.ToString(), null));
            }

            if (rulesDoc.SelectNodes("//rule") == null || rulesDoc.SelectNodes("//rule").Count == 0)
            {
                throw new ArgumentException("No rules were supplied.");
            }

            foreach (XmlElement rule in rulesDoc.SelectNodes("//rule"))
            {
                string op              = rule.GetAttribute("op").ToLowerInvariant();
                string field           = null;
                string val             = null;
                bool   valIsRequired   = true;
                bool   fieldIsRequired = false;

                switch (op)
                {
                case "=":
                case ">":
                case ">=":
                case "<":
                case "<=":
                case "contains":
                case "<>":
                case "not contains":
                    field           = rule.GetAttribute("field");
                    val             = rule.GetAttribute("value");
                    fieldIsRequired = true;
                    break;

                case "reports under":
                    field = "Everyone";
                    val   = rule.GetAttribute("value");
                    break;

                case "member of":
                    field = "DL";
                    val   = rule.GetAttribute("value");
                    break;

                case "and":
                case "or":
                case "(":
                case ")":
                    valIsRequired = false;
                    break;

                default:
                    throw new ArgumentException(string.Format("Rule operator is invalid: {0}", rule.GetAttribute("op")));
                }
                if (valIsRequired && string.IsNullOrEmpty(val))
                {
                    throw new ArgumentNullException(string.Format("Rule value attribute is missing or invalid: {0}", rule.GetAttribute("value")));
                }

                if (fieldIsRequired && string.IsNullOrEmpty(field))
                {
                    throw new ArgumentNullException(string.Format("Rule field attribute is missing or invalid: {0}", rule.GetAttribute("field")));
                }

                AudienceRuleComponent r0 = new AudienceRuleComponent(field, op, val);
                audienceRules.Add(r0);
            }

            audience.AudienceRules = audienceRules;
            audience.Commit();

            if (compile)
            {
                SPServiceApplication svcApp = Utilities.GetUserProfileServiceApplication(context);
                if (svcApp != null)
                {
                    CompileAudience(svcApp.Id, audience.AudienceName);
                }
            }

            return(audienceRules);
        }
Ejemplo n.º 3
0
 private void UpdateObject(Audience currentObject)
 {
     currentObject.Commit();
 }