Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Unanswered

Share many contact (number of contact record is entered in a field on the form) to one user with c# plugin

Posted on by 5

Hi, i want to get the fetchxml result and with help of loop share contact record (the number of record is in a field) to just one user in a field with help of plugin.

I'm new to CRM and plugin any help is appreciated.

this is my code

for example number of contact to share field = 3

to whom want to share field = john  smith

namespace Saedi
{
public class DistributionNumber : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));


IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = factory.CreateOrganizationService(context.UserId);

if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
{

Entity contact = (Entity)context.InputParameters["Target"];

try
{
//if (contact.Attributes.Contains("new_sharecontactnumber"))
//{


string fetchXml =
@"<fetch version=""1.0"" output-format=""xml-platform"" mapping=""logical"" distinct=""false"">
<entity name=""contact"">
<attribute name=""fullname"" />
<attribute name=""telephone1"" />
<attribute name=""contactid"" />
<order attribute=""fullname"" descending=""false"" />
<filter type=""and"">
<condition attribute=""ownerid"" operator=""eq-userid"" />
<condition attribute=""modifiedon"" operator=""today"" />
</filter>
</entity>
</fetch>";

fetchXml = string.Format(fetchXml, contact.Id);
var qe = new FetchExpression(fetchXml);
var result = service.RetrieveMultiple(qe);

if (fetchXml != null && result.Entities.Count > 0)
{
string totalcount = result.Entities.Count.ToString();
int new_sharecontactnumber = Convert.ToInt32(contact.Attributes["new_sharecontactnumber"]);

//for (int i = 0; i == result.Entities.Count; i++)
//{
for (int j = 0; j <= new_sharecontactnumber; j++)
{
//throw new InvalidPluginExecutionException("the number of records is: " + totalcount)

//Entity item = result.Entities[i];

Guid userlookupid = ((EntityReference)contact.Attributes["new_useruser"]).Id;
var UserRef = new EntityReference(((EntityReference)contact.Attributes["new_useruser"]).LogicalName, userlookupid);
GrantAccessRequest request = new GrantAccessRequest
{
PrincipalAccess = new PrincipalAccess
{
AccessMask = AccessRights.ReadAccess | AccessRights.WriteAccess,
Principal = UserRef
},
Target = new EntityReference(result[j].LogicalName, result[j].Id)

};
service.Execute(request);
}
//}
}
else
{
throw new InvalidPluginExecutionException("no record has been found");
}

}
catch (Exception ex)
{
throw new InvalidPluginExecutionException(ex.Message);
}
}
}
}
}

  • Mehransaedi Profile Picture
    Mehransaedi 5 on at
    RE: Share number (number is entered in a field on the form) of contact to one user with c# plugin

    my problem is the FOR loop section of the code and don't know how to solve it.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans