CRM operation business department

Source: Internet
Author: User

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 );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.