Thursday, August 2, 2012

Get all user/team an object (account/contact/lead) is shared with in MS CRM 2011

Get all user/team an object (account/contact/lead) is shared with using OrganizationService service.

public void ProcessSharedUser()
        {
            PrincipalAccess[] sharedUsersOfAccount = getSharedUsersforAccount("account", accountId, service);
            if (sharedUsersOfAccount.Length > 0)
            {
                for (int i = 0; i < sharedUsersOfAccount.Length; i++)
                {
                    Guid user = sharedUsersOfAccount[i].Principal.Id;
                    // define your logic here
                }
            }
        }
        public PrincipalAccess[] getSharedUsersforAccount(string entityName, Guid accountId, IOrganizationService service)
        {
            RetrieveSharedPrincipalsAndAccessRequest retrieveSharedRequest = new RetrieveSharedPrincipalsAndAccessRequest()
            {
                Target = new EntityReference(entityName, accountId)
            };
            RetrieveSharedPrincipalsAndAccessResponse retrieveSharedResponse = (RetrieveSharedPrincipalsAndAccessResponse)service.Execute(retrieveSharedRequest);

            return retrieveSharedResponse.PrincipalAccesses;
        }

No comments: