Sharing Record with a Team

using  System;

using System.Collections.Generic;

using System.Text;

using Microsoft.Crm.Sdk;

using Microsoft.Crm.SdkTypeProxy;

using Microsoft.Crm.Sdk.Query;

namespace CRM.Customization.Sharing

public class SharingRecord:IPlugin


string teamId = string.Empty;

#region IPlugin Members

public void Execute(IPluginExecutionContext context)
DynamicEntity entity=null ;

if (context.InputParameters.Properties.Contains(“Target” )
&& context.InputParameters.Properties[“Target”] is DynamicEntity )
entity = (DynamicEntity)context.InputParameters.Properties[“Target” ];
Guid recordId=(Guid)context.OutputParameters.Properties[“id” ];

ICrmService service = context.CreateCrmService(true );

// Query express to find out team id to share the rescord

QueryByAttribute query = new QueryByAttribute ();
query.EntityName =EntityName .team.ToString();

ColumnSet cols = new ColumnSet ();
cols.AddColumn(“teamid” );

query.ColumnSet = cols;
query.Attributes =new string[] { “name”

query.Values =new string[] { “MUL” };

BusinessEntityCollection retrieved = (BusinessEntityCollection) service.RetrieveMultiple(query);
if (retrieved.BusinessEntities.Count > 0 )
team newTeam = (team )retrieved.BusinessEntities[0];
Key teamGuid = (Key )newTeam.teamid;
teamId = teamGuid.Value.ToString();

// Create the SecurityPrincipal Object
SecurityPrincipal principal = new SecurityPrincipal ();
principal.Type =SecurityPrincipalType .Team;

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

// 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
TargetOwnedOpportunity target = new TargetOwnedOpportunity ();

// EntityId is the Guid of the account access is being granted to
target.EntityId = recordId;

// 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);





Author: Arvind Singh

Microsoft Dynamics CRM Consultant

One thought on “Sharing Record with a Team”

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your 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