Random Musings

December 30, 2008

Sharing Records using CRM SDK API

Filed under: Dynamics CRM — haditeo @ 12:03 pm

Developers can programmatically grant access to certain records, either read, write or other access through GrantAccessRequest.

Here is the reference at MSDN : http://msdn.microsoft.com/en-us/library/bb956464.aspx

Here is the code snippet in how to utilize GrantAccessRequest

        private void ShareCase(ref ICrmService service, string incidentId, string userId)
        {
            try
            {
                // Create the SecurityPrincipal Object
                SecurityPrincipal principal = new SecurityPrincipal();
                principal.Type = SecurityPrincipalType.User;

                // PrincipalId is the Guid of the user to whom access is being granted
                principal.PrincipalId = new Guid(userId);

                // Create the PrincipalAccess Object
                PrincipalAccess principalAccess = new PrincipalAccess();

                // Set the PrincipalAccess Object's Properties
                principalAccess.Principal = principal;

                // Gives the principal access to read
                principalAccess.AccessMask = AccessRights.ReadAccess;

                // Create the Target Object for the Request
                TargetOwnedIncident target = new TargetOwnedIncident();

                // EntityId is the Guid of the account access is being granted to
                target.EntityId = new Guid(incidentId);

                // Create the Request Object
                GrantAccessRequest grant = new GrantAccessRequest();

                // Set the Request Object's properties
                grant.PrincipalAccess = principalAccess;
                grant.Target = target;

                // Execute the Request
                GrantAccessResponse granted = (GrantAccessResponse)service.Execute(grant);
            }
            catch (SoapException ex)
            {
                throw ex;
            }
            catch (Exception e)
            {               
                throw e;
            }
        }
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: