Wednesday, October 02, 2013

Code snippet to copy saved outlook filters from one user to all users


Use the code template from here

Code:
QueryExpression ownerQe = new QueryExpression("systemuser");
                ownerQe.Criteria.AddCondition("domainname", ConditionOperator.Equal, "domain\\c260xxx");
                ownerQe.ColumnSet.AddColumn("systemuserid");
                EntityCollection ownerColl =  _service.RetrieveMultiple(ownerQe);
                if (ownerColl.Entities.Count == 1)
                {
                    Entity owner = ownerColl.Entities[0];
                    Guid _OwnerId = owner.Id;//new Guid("79934B9F-065B-E211-AA02-0050568E0699");//Ganesh Sivakumar
                    ConditionExpression query_ce1 = new ConditionExpression("querytype", ConditionOperator.Equal, 256);
                    ConditionExpression query_ce2 = new ConditionExpression("ownerid", ConditionOperator.Equal, _OwnerId);
                    FilterExpression query_fe = new FilterExpression(LogicalOperator.And);
                    query_fe.AddCondition(query_ce1);
                    query_fe.AddCondition(query_ce2);
                    QueryExpression query_qe = new QueryExpression();
                    query_qe.EntityName = "userquery";
                    query_qe.Criteria = query_fe;
                    query_qe.ColumnSet = new ColumnSet("userqueryid", "fetchxml", "returnedtypecode");
                    query_qe.PageInfo.ReturnTotalRecordCount = true;
                    EntityCollection query_coll = _service.RetrieveMultiple(query_qe);

                    string _appointmentFetchXml = "", _recurringFetchXml = "";

                    foreach (Entity query in query_coll.Entities)
                    {
                        if ((string)query["returnedtypecode"] == "appointment")
                            _appointmentFetchXml = (string)query["fetchxml"];
                        if ((string)query["returnedtypecode"] == "recurringappointmentmaster")
                            _recurringFetchXml = (string)query["fetchxml"];
                    }

                    ConditionExpression con_ce1 = new ConditionExpression("domainname", ConditionOperator.NotNull);
                    FilterExpression con_fe = new FilterExpression(LogicalOperator.And);
                    con_fe.AddCondition(con_ce1);
                    QueryExpression con_qe = new QueryExpression();
                    con_qe.EntityName = "systemuser";
                    con_qe.Criteria = con_fe;
                    con_qe.ColumnSet = new ColumnSet("systemuserid", "ssb_delete_flag", "isdisabled");
                    con_qe.PageInfo.ReturnTotalRecordCount = true;
                    EntityCollection con_coll = _service.RetrieveMultiple(con_qe);

                    foreach (Entity user in con_coll.Entities)
                    {
                        Guid _userId = user.Id;
                        ConditionExpression ent_ce1 = new ConditionExpression("querytype", ConditionOperator.Equal, 256);
                        ConditionExpression ent_ce2 = new ConditionExpression("ownerid", ConditionOperator.Equal, _userId);
                        //ConditionExpression ent_ce3 = new ConditionExpression("returnedtypecode", ConditionOperator.Equal, 2);
                        FilterExpression ent_fe = new FilterExpression(LogicalOperator.And);
                        ent_fe.AddCondition(ent_ce1);
                        ent_fe.AddCondition(ent_ce2);
                        //ent_fe.AddCondition(ent_ce3);
                        QueryExpression ent_qe = new QueryExpression();
                        ent_qe.EntityName = "userquery";
                        ent_qe.Criteria = ent_fe;
                        ent_qe.ColumnSet = new ColumnSet("userqueryid", "returnedtypecode", "fetchxml", "statecode");
                        ent_qe.PageInfo.ReturnTotalRecordCount = true;
                        EntityCollection ent_coll = _service.RetrieveMultiple(ent_qe);

                        foreach (Entity ent in ent_coll.Entities)
                        {
                            if ((string)ent["returnedtypecode"] == "contact")
                            {
                                OptionSetValue stateCode = (OptionSetValue)ent["statecode"];
                                if (stateCode.Value == 0)
                                {
                                    Backup.SetState(ent.LogicalName, ent.Id, 1, 2, _service);
                                    Helper.Log("QueryId " + ent.Id.ToString() + " is disabled", w);
                                }
                            }

                            if ((string)ent["returnedtypecode"] == "recurringappointmentmaster")
                            {
                                if ((string)ent["fetchxml"] != _recurringFetchXml)
                                {
                                    ent["fetchxml"] = _recurringFetchXml;
                                    _service.Update(ent);
                                    Helper.Log("QueryId " + ent.Id.ToString() + " is updated", w);
                                }
                            }

                            if ((string)ent["returnedtypecode"] == "appointment")
                            {
                                if ((string)ent["fetchxml"] != _appointmentFetchXml)
                                {
                                    ent["fetchxml"] = _appointmentFetchXml;
                                    _service.Update(ent);
                                    Helper.Log("QueryId " + ent.Id.ToString() + " is updated", w);
                                }
                            }
                        }
                    }

No comments: