Posts Tagged ‘Sharing’


          Below is the sample code to share entity record with team in MS CRM 2011

            var teamReference = new EntityReference(“team”, teamId);
            var grantAccessRequest = new GrantAccessRequest
            {
                PrincipalAccess = new PrincipalAccess
                {
                    AccessMask=AccessRights.ReadAccess | AccessRights.WriteAccess,        
                    Principal=teamReference
                },

                Target=new EntityReference(“lead”,recordId)

            };
            service.Execute(grantAccessRequest);

           // Get teamId from below function

private Guid GetTeamId(string teamName,OrganizationService service)
        {
            Guid teamId=Guid.Empty;
            QueryByAttribute query = new QueryByAttribute();
            ColumnSet cols = new ColumnSet();
            cols.AddColumn(“teamid”); 

            query.ColumnSet = cols;
            query.AddAttributeValue(“name”, teamName);
            query.EntityName = “team”; 

            EntityCollection entityCollection = (EntityCollection)service.RetrieveMultiple(query);              
            if (entityCollection.Entities.Count > 0)
            {
                    Entity entity =entityCollection.Entities[0];
                    teamId = entity.Id;
            }
            return teamId;          

        }

I have already posted the same for CRM 4.0. Please find the reference link below.
https://arvindcsit.wordpress.com/2012/02/26/create-contact-and-share-the-record-which-contains-lookuppicklist-and-string-datatype/

 


private bool RevokeAccessToEntity(ICrmService service, Guid guidValue, Guid entityID, string EntityName)
{
bool isSuccess = false;
// Create the SecurityPrincipal object.
SecurityPrincipal principal = new SecurityPrincipal();

// PrincipalId is the GUID of the team whose access is being revoked.
principal.Type = SecurityPrincipalType.Team;

principal.PrincipalId = guidValue;

// Create the target for the request.
TargetOwnedDynamic target = new TargetOwnedDynamic();
// EntityId is the GUID of the Opportunity to which
// access is being revoked.
target.EntityId = entityID;
target.EntityName = EntityName;

// Create the request object.
RevokeAccessRequest revoke = new RevokeAccessRequest();
// Set the properties of the request object.
revoke.Revokee = principal;
revoke.Target = target;
// Execute the request.

try
{
RevokeAccessResponse revoked = (RevokeAccessResponse)service.Execute(revoke);
isSuccess = true;
}
catch (Exception ex)
{
isSuccess = false;
throw ex;
}
return isSuccess;

}


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

}

}