Tech Off Thread

1 post

Retrieving user privileges on entity using CRM 2011 SDK

Back to Forum: Tech Off
  • User profile image
    asharsyed

    Hi all,

    I was using RetrievePrincipalAccessRequest and RetrievePrincipalAccessResponse objects to retreive user access rights on entity using entity instances. This worked until thus far when I tried to retreive access rights on custom entity.

    I get the following error "The 'RetrievePrincipalAccess' method does not support entities of type 'my_entity'.

    Below is the code that I am using to retreive user access rights on entities.

            protected internal static AccessRights? GetUserAccessRights(string entityName, string UserName, out Guid? UserId)
            {
                // Instantiate the service object
                OrganizationService service = new OrganizationService("MY_ORG");
    
                // Instantiate the context object
                PRMServiceContext context = new PRMServiceContext(service);
    
                if (entityName != null && !String.IsNullOrEmpty(entityName) && !String.IsNullOrEmpty(UserName))
                {
                    try
                    {
                        // Get the owner id of the user
                        var userId = (from su in context.CreateQuery<SystemUser>()
                                      join sur in context.CreateQuery<SystemUserRoles>()
                                      on su.SystemUserId equals sur.SystemUserId
                                      where su.DomainName == UserName
                                      select su.SystemUserId).FirstOrDefault();
    
                        Guid? instanceId = null;
                        switch (entityName)
                        {
                            case "contact":
                                instanceId =
                                (
                                    from c in context.CreateQuery<Contact>()
                                    where c.Id != null
                                    select c.Id
                                ).Take(1).First();
                                break;
                            case "phonecall":
                                instanceId =
                                (
                                    from c in context.CreateQuery<PhoneCall>()
                                    where c.Id != null
                                    select c.Id
                                ).Take(1).First();
                                break;
                            case "my_entity":
                                instanceId =
                                (
                                    from c in context.CreateQuery<my_entity>()
                                    where c.Id != null
                                    select c.Id
                                ).Take(1).First();
                                break;
                        }
    
    
                        if (userId != null && instanceId != null)
                        {
                            RetrievePrincipalAccessRequest req = new RetrievePrincipalAccessRequest();
                           
                            //specify team or systemuser you want to get privileges for
                            req.Principal = new EntityReference("systemuser", userId.Value);
    
                            //specify the CRM record you want to check principal permissions for (can be any entity type)
                            req.Target = new EntityReference(entityName, (Guid)instanceId);
    
                            RetrievePrincipalAccessResponse resp = (RetrievePrincipalAccessResponse)context.Execute(req);
                            context.ClearChanges();
    
                            ParameterCollection pc = resp.Results;
                            UserId = userId;
    
                            return resp.AccessRights;
                        }
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
                UserId = null;
                return null;
            }

     Please help. Thanks.

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.