Thursday 28 November 2013

How to get get and Set the Party list in Javascript:

To retrieve values by jscript: 

var partylistItem = new Array;
//get the items/values in a field named"resources"

partylistItem  = Xrm.Page.getAttribute("resources").getValue();

To set values by  jscript :
To set  values we also  need to  get an  array.
 var partlistset= new Array();
partlistset[0] = new Object();
partlistset[0].id = id; // provide a guid type value
partlistset[0].name = name; // provide a suitable name
partlistset[0].entityType = entityType; // provide the entity  name  of the item  ie account/ contact etc .
Xrm.Page.getAttribute("resource").setValue(partlistset);

How to get the Contact Guids from a PartyList in a Plugin?

EntityCollection Recipients;
Entity entity = (Entity) context.InputParameters["Target"];

IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
Content = entity.GetAttributeValue<String>("subject");
Recipients = entity.GetAttributeValue<EntityCollection>("to");
for (int i = 0; i < Recipients.Entities.Count; i++)
        {
            ActivityParty ap = Recipients[i].ToEntity<ActivityParty>();
            string contactid = ap.PartyId.Id.ToString(); 
            Contact c = (Contact) service.Retrieve(Contact.EntityLogicalName,ap.PartyId.Id,new ColumnSet(new string[]{"mobilephone"}));
            string mobilephone = c.MobilePhone;
              //Do something with the mobile phone nr

}