locked
Get User Permission Set on an entity RRS feed

  • Question

  • Hi all,

    I am having issue trying to fetch CRM 2011 user permission set on a particular entity. Look at my code below.

    // 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();
    
                        if (userId != 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, new Entity(entityName).Id);
                            RetrievePrincipalAccessResponse resp = (RetrievePrincipalAccessResponse)context.Execute(req);

    The code keeps on giving error where I try to set Target for the request as entityName as it can not find the Guid for the entity "contact" (Check the bold statement above). What I don't get is how to get the existing Guid of the entity from CRM.

    Please help!

    Thursday, July 12, 2012 7:36 PM

Answers

  • RetrievePrincipalAccessRequest is used to determine a Principal's access to a specific entity instance, not an entity.  As such, you must pass a GUID for an existing entity instance in the EntityReference specified as the Target.  E.g.:

    Guid contactId =
    	(
    		from c in xrm.ContactSet
    		where c.FirstName.StartsWith("A")
    		select c.Id
    	).Take(1).First();
    
    if (userId != 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("contact", contactId);
    	RetrievePrincipalAccessResponse resp = (RetrievePrincipalAccessResponse)context.Execute(req);
    }
    


    --pogo (pat) @ pogo69.wordpress.com

    • Proposed as answer by MubasherSharif Sunday, July 15, 2012 11:33 PM
    • Marked as answer by Syed A. Ali Tuesday, September 24, 2013 12:15 PM
    Thursday, July 12, 2012 11:51 PM