Quantcast
Channel: Arvind's CRM Blog » CRM
Viewing all articles
Browse latest Browse all 11

Sharing Record with a Team

$
0
0

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

}

#endregion

}

}



Viewing all articles
Browse latest Browse all 11

Trending Articles