how to insert resource especially System User into Resource Group in Microsoft CRM 2011 SDK
Hi Again,
First,we have to get deep technical information about Constraint definition file;
here is the definition in XML format;
<Constraints>
<Constraint>
<Expression>
<Body>
resource["Id"] == {ac8e6996-ef65-e211-bc9b-005056820011} ||
resource["Id"] == {4a65de5a-d763-e211-bc9b-005056820011} ||
resource["Id"] == {626E49EC-2011-E211-84B5-005056820011}
</Body>
<Parameters>
<Parameter name="resource"/>
</Parameters>
</Expression>
</Constraint>
</Constraints>
Body tag refers to any resources in the Resource Group.
if you add primary key as guid for the record to the constraint with 'or' logical word, you will see them in the selected resource group in CRM
if you add primary key as guid for the record to the constraint with 'or' logical word, you will see them in the selected resource group in CRM
You can get inspiration from AddMembersToTheConstraint methods as shown bolew,Method takes constraints string and id of member to be added , parses XML and inserts id's.
private string AddMembersToTheConstraint(string constraints, Guid[] ids)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(constraints);
XmlNodeList body = doc.GetElementsByTagName("Body");
String MemberLists = body[0].FirstChild.InnerText;
foreach (Guid id in ids)
{
if (MemberLists.ToLower().IndexOf("false") >= 0)
{
MemberLists = "";//Group has not got any member yet.
MemberLists += "resource[\"Id\"] == {" + id.ToString() + "}";
}
else
MemberLists += " || " + "resource[\"Id\"] == {" + id.ToString() + "}";
}
body[0].FirstChild.InnerText = MemberLists;
using (var stringWriter = new StringWriter())
using (var xmlTextWriter = XmlWriter.Create(stringWriter))
{
doc.WriteTo(xmlTextWriter);
xmlTextWriter.Flush();
return stringWriter.GetStringBuilder().ToString();
}
}
here is code that you may use while inserting resource(s) to the Resource Group
{
...
resourcegroupGuide.Constraints = AddMembersToTheConstraint(resourcegroupGuide.Constraints, new Guid[] { user.Id });
if (!context.IsAttached(resourcegroupGuide))
context.Attach(resourcegroupGuide);
context.UpdateObject(resourcegroupGuide);
context.SaveChanges();
if you need more information about 'context' and '_serviceProxy' objects in the sample code.Please follow :
http://muhammetatalay.blogspot.com/2012/11/retrieve-records-in-microsoft-crm-2011.html
good luck,
Comments
Post a Comment