Using system;
Using system. LINQ;
Using Microsoft. xrm. SDK;
Using Microsoft. CRM. SDK. messages;
Using system. Collections. Generic;
Using Microsoft. xrm. SDK. messages;
/// <Summary>
/// Operate the business department
/// </Summary>
Public class businessunithelper
{
Public static readonly string entityname = "businessunit ";
Public guid businessunitid = guid. empty;
/// <Summary>
/// Create a business department
/// </Summary>
Public void createunit (iorganizationservice Service)
{
Entity en = new entity () {logicalname = entityname };
En ["name"] = "Software Development Department ";
Businessunitid = service. Create (en );
}
/// <Summary>
/// Modify the business department
/// </Summary>
Public void updateunit (iorganizationservice Service)
{
Entity en = new entity () {logicalname = entityname, id = businessunitid };
En ["name"] = "Software Development Department ";
Service. Update (en );
}
/// <Summary>
/// Set the upper-level department of the business department
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "parentid"> superior business department ID </param>
Public void setparentbusinessunit (iorganizationservice service, guid parentid)
{
Setparentbusinessunitrequest request = new setparentbusinessunitrequest ();
// ID of the current business department
Request. businessunitid = businessunitid;
// Superior business department ID
Request. parentid = parentid;
Service. Execute (request );
}
/// <Summary>
/// Retrieve all business departments in the business department hierarchy
/// </Summary>
Public void searchbusinessunit (iorganizationservice Service)
{
Retrievebusinesshierarchybusinessunitrequest request = new retrievebusinesshierarchybusinessunitrequest ();
Request. entityid = businessunitid;
Request. columnset = new Microsoft. xrm. SDK. query. columnset ("name", "parentbusinessunitid ");
Retrievebusinesshierarchybusinessunitresponse response =
(Retrievebusinesshierarchybusinessunitresponse) service. Execute (request );
If (response! = NULL & response. entitycollection! = NULL)
{
List <businessunit> List = new list <businessunit> ();
Entitycollection ecresult = response. entitycollection;
Int Index = 1;
# Region obtain all Departments
Foreach (entity en in ecresult. Entities)
{
Businessunit unite = new businessunit ();
Unite. businessunitid = en. ID;
Unite. Name = en. Contains ("name") & en ["name"]! = NULL? En ["name"]. tostring (): String. empty;
If (EN. Contains ("parentbusinessunitid") & en ["parentbusinessunitid"]! = NULL)
{
Entityreference parer = en ["parentbusinessunitid"] As entityreference;
Unite. parentbusinessunitid = parer. ID;
}
Unite. Order = index;
List. Add (unite );
Index ++;
}
# Endregion
# Region resetting the order of business departments
Foreach (businessunit model in List)
{
VaR result = List. Where (A => A. parentbusinessunitid = A. businessunitid );
If (result! = NULL & result. Count ()> 0)
{
VaR parmodel = result. firstordefault ();
If (model. Order + 1! = Parmodel. Order ){
Parmodel. Order = model. Order + 1;
}
}
}
# Endregion
}
}
Public class businessunit
{
Public guid businessunitid {Get; set ;}
Public string name {Get; set ;}
Public guid parentbusinessunitid {Get; set ;}
Public int order {Get; set ;}
}
/// <Summary>
/// Add or remove a user
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "userid"> User </param>
Public void addandremoveuser (iorganizationservice service, guid userid)
{
// Add a user
Addconnection (service, "business_unit_system_users ",
New entityreference () {logicalname = "systemuser", id = userid });
// Remove a user
Removeconnection (service, "business_unit_system_users ",
New entityreference () {logicalname = "systemuser", id = userid });
}
/// <Summary>
/// Add or remove a team
/// </Summary>
/// <Param name = "service"> service </param>
/// <Param name = "teamid"> Team </param>
Public void addandremoveuser (iorganizationservice service, guid teamid)
{
// Add team
Addconnection (service, "business_unit_teams ",
New entityreference () {logicalname = "team", id = teamid });
// Remove a team
Removeconnection (service, "business_unit_teams ",
New entityreference () {logicalname = "team", id = teamid });
}
/// <Summary>
/// Disable or enable the business department
/// </Summary>
Public void setbusinessunitstate (iorganizationservice Service)
{
// Enable the business department
Updatestate (Service, businessunitid, 0,-1 );
// Disable the business department
Updatestate (Service, businessunitid,-1,-1 );
}
/// <Summary>
/// Delete the business department
/// </Summary>
Public void deleteunit (iorganizationservice Service)
{
Service. Delete (entityname, businessunitid );
}
Private void updatestate (iorganizationservice service, guid ID, int state, int status)
{
Setstaterequest setstate = new setstaterequest ()
{
Entitymoniker = new entityreference ()
{
Id = ID,
Logicalname = entityname
},
State = new optionsetvalue (state ),
Status = new optionsetvalue (Status)
};
Service. Execute (setstate );
}
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 = businessunitid };
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 = businessunitid };
Request. relatedentities = new entityreferencecollection ();
Request. relatedentities. addrange (array );
Service. Execute (request );
}
}