简体   繁体   中英

How to build a query to get a security role where user X does not exists in Dynamics CRM 2016 C#

I need to update the list of users for a security role. How can I avoid re-associating an user that already has the role assigned?

I'm trying to build a query to get the role where user x is not in the role's list of users and then use it for the association if the user was not found. Here is what I have so far:

LinkEntity userRoles = new LinkEntity
{
    LinkFromEntityName = "role",
    LinkToEntityName = "systemuserroles",
    LinkFromAttributeName = "roleid",
    LinkToAttributeName = "roleid",
    JoinOperator = JoinOperator.LeftOuter,
    Columns = new ColumnSet(true),
    EntityAlias = "userroles",
    LinkCriteria =
    {
        Conditions =
        {
            new ConditionExpression{
                //not sure how to build the "where user x does not exists"
           }
        }
    }
};

QueryExpression roleQuery = new QueryExpression
{
    EntityName = "role",
    ColumnSet = new ColumnSet(true),
    Criteria =
    {
        Conditions = { 
            new ConditionExpression { 
                AttributeName = "name", 
                Operator = ConditionOperator.Equal, 
                Values = { "RoleName"}
            } 
        }
    },
    LinkEntities = { userRoles }
};

I need to build something like this query:

select * from role 
where role.name = "RoleName"
and not exists
(select 1 from userRoles 
where userRoles.roleid = role.roleid
and userRoles.user = "xyz")

Based on this post that explained it using Fetch XML, I was able to build this query that gets the role as long as the user I specify is not there:

LinkEntity userRoles = new LinkEntity
{
    LinkFromEntityName = "role",
    LinkToEntityName = "systemuserroles",
    LinkFromAttributeName = "roleid",
    LinkToAttributeName = "roleid",
    JoinOperator = JoinOperator.LeftOuter,
    Columns = new ColumnSet(true),
    EntityAlias = "userroles",
    LinkCriteria =
    {
        Conditions =
         {                         
             new ConditionExpression {
                 AttributeName = "systemuserid",
                 Operator = ConditionOperator.Equal,
                 Values = { "xyz" }
             }
         }
    }
};

QueryExpression roleQuery = new QueryExpression
{
    EntityName = "role",
    ColumnSet = new ColumnSet(true),
    Criteria =
    {
        Filters = {
            new FilterExpression{
                FilterOperator = LogicalOperator.And,
                Conditions = {
                    new ConditionExpression{
                        EntityName = "systemuserroles",
                        AttributeName = "roleid",
                        Operator = ConditionOperator.Null
                    }
                }
            }                    
        },
        Conditions = { 
                    new ConditionExpression { 
                        AttributeName = "name", 
                        Operator = ConditionOperator.Equal, 
                        Values = { "RoleName"}
                    }
                }
    },
    LinkEntities = { userRoles }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM