Using system;
Using Microsoft. xrm. SDK;
Using Microsoft. CRM. SDK. messages;
Using Microsoft. xrm. SDK. messages;
/// <Summary>
/// Team
/// </Summary>
Public class teamhelper
{
Public static readonly string entityname = "team ";
Public guid teamid = guid. empty;
/// <Summary>
/// Create a team
/// </Summary>
/// <Param name = "service"> service </param>
Public void create (iorganizationservice Service)
{
Entity en = new entity () {logicalname = entityname };
En ["name"] = "Software Development Team ";
Teamid = service. Create (en );
}
/// <Summary>
/// Set the business department of the team
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "unitid"> business department ID </param>
Public void setparentunit (iorganizationservice service, guid unitid)
{
Setparentteamrequest request = new setparentteamrequest ();
// Business department ID
Request. businessid = unitid;
// Team ID
Request. teamid = teamid;
Service. Execute (request );
}
/// <Summary>
/// Modify the team
/// </Summary>
/// <Param name = "service"> service </param>
Public void Update (iorganizationservice Service)
{
Entity en = new entity () {logicalname = entityname, id = teamid };
En ["name"] = "Software Development Team ";
Service. Update (en );
}
/// <Summary>
/// Add members to the team
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "userarrays"> User ID array </param>
Public void adduser (iorganizationservice service, guid [] userarrays)
{
Addmembersteamrequest request = new addmembersteamrequest ();
// Team
Request. teamid = teamid;
// User ID Array
Request. memberids = userarrays;
Service. Execute (request );
}
/// <Summary>
/// Remove a member from the team
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "userarrays"> User ID array </param>
Public void removeuser (iorganizationservice service, guid [] userarrays)
{
Removemembersteamrequest request = new removemembersteamrequest ();
// Team
Request. teamid = teamid;
// User ID Array
Request. memberids = userarrays;
Service. Execute (request );
}
/// <Summary>
/// Query team Permissions
/// </Summary>
/// <Param name = "service"> service </param>
Public void searchteamrole (iorganizationservice Service)
{
Retrieveteamprivilegesrequest request = new retrieveteamprivilegesrequest ();
// Team
Request. teamid = teamid;
Retrieveteamprivilegesresponse response = (retrieveteamprivilegesresponse) service. Execute (request );
If (response. roleprivileges! = NULL)
{
Roleprivilege [] array = response. roleprivileges;
}
}
/// <Summary>
/// Add and remove security roles
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "roleid"> security role id </param>
Public void addandremoverole (iorganizationservice service, guid roleid)
{
// Add a security role
Addconnection (service, "teamroles_association", new entityreference () {logicalname = "role", id = roleid });
// Remove a security role
Removeconnection (service, "teamroles_association", new entityreference () {logicalname = "role", id = roleid });
}
/// <Summary>
/// Allocate a record to the team
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "target"> record object </param>
Public void assigntoteam (iorganizationservice service, entityreference target)
{
Assignrequest request = new assignrequest ();
Request. Target = target;
Request. assignee = new entityreference () {logicalname = entityname, id = teamid };
Service. Execute (request );
}
/// <Summary>
/// Delete the team
/// </Summary>
/// <Param name = "service"> service </param>
Public void Delete (iorganizationservice Service)
{
Service. Delete (entityname, teamid );
}
Public void addconnection (iorganizationservice service, string name, Params entityreference [] array)
{
Relationship ship = new relationship (name );
Associaterequest request = new associaterequest ();
Request. relationship = ship;
Request. Target = new entityreference () {logicalname = entityname, id = teamid };
Request. relatedentities = new entityreferencecollection ();
Request. relatedentities. addrange (array );
Service. Execute (request );
}
Public void removeconnection (iorganizationservice service, string name, Params entityreference [] array)
{
Relationship ship = new relationship (name );
Disassociaterequest request = new disassociaterequest ();
Request. relationship = ship;
Request. Target = new entityreference () {logicalname = entityname, id = teamid };
Request. relatedentities = new entityreferencecollection ();
Request. relatedentities. addrange (array );
Service. Execute (request );
}
}