关注本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复148或者20150813可方便获取本文,同时可以在第一时间得到我发布的最新的博文信息,follow me!
//Add the code you want to test here: // You must use the SoapLoggerOrganizationService 'slos' proxy rather than the IOrganizationService proxy you would normally use.就是在这里写你要执行的消息,我这里以分享一个客户记录的读取权限给一个CRM的用户为例,C#版本的代码是这么写的:
var request = new GrantAccessRequest() { PrincipalAccess = new PrincipalAccess() { AccessMask = AccessRights.ReadAccess, Principal = new EntityReference("systemuser", new Guid("CC06819E-9AD3-E411-80BC-00155D09F02E")) }, Target = new EntityReference("account", new Guid("FB9A903E-FC3F-E511-80DF-00155D1D7B07")) }; slos.Execute(request);确保编译能通过后调试起来,我这里按 F5 启动起来,弹出如下窗口,我输入如下,我这个CRM环境没有做IFD,但是是使用HTTS绑定的,所以第二个选项我输入了Y,大家根据自己的环境做选择啊。
HTTP REQUEST
--------------------------------------------------
POST https://crm2015.luoyong.com/SuGeGeDev/XRMServices/2011/Organization.svc/web
Content-Type: text/xml; charset=utf-8
SOAPAction: http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<Execute xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<request i:type="b:GrantAccessRequest" xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:b="http://schemas.microsoft.com/crm/2011/Contracts">
<a:Parameters xmlns:c="http://schemas.datacontract.org/2004/07/System.Collections.Generic">
<a:KeyValuePairOfstringanyType>
<c:key>Target</c:key>
<c:value i:type="a:EntityReference">
<a:Id>fb9a903e-fc3f-e511-80df-00155d1d7b07</a:Id>
<a:LogicalName>account</a:LogicalName>
<a:Name i:nil="true" />
</c:value>
</a:KeyValuePairOfstringanyType>
<a:KeyValuePairOfstringanyType>
<c:key>PrincipalAccess</c:key>
<c:value i:type="b:PrincipalAccess">
<b:AccessMask>ReadAccess</b:AccessMask>
<b:Principal>
<a:Id>cc06819e-9ad3-e411-80bc-00155d09f02e</a:Id>
<a:LogicalName>systemuser</a:LogicalName>
<a:Name i:nil="true" />
</b:Principal>
</c:value>
</a:KeyValuePairOfstringanyType>
</a:Parameters>
<a:RequestId i:nil="true" />
<a:RequestName>GrantAccess</a:RequestName>
</request>
</Execute>
</s:Body>
</s:Envelope>
--------------------------------------------------
HTTP RESPONSE
--------------------------------------------------
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body>
<ExecuteResponse xmlns="http://schemas.microsoft.com/xrm/2011/Contracts/Services" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ExecuteResult i:type="b:GrantAccessResponse" xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts" xmlns:b="http://schemas.microsoft.com/crm/2011/Contracts">
<a:ResponseName>GrantAccess</a:ResponseName>
<a:Results xmlns:c="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
</ExecuteResult>
</ExecuteResponse>
</s:Body>
</s:Envelope>
--------------------------------------------------
function ShareAccountReadRightRequest(AccountId,SystemUserId)
{
var requestMain = ""
requestMain += "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\">";
requestMain += " <s:Body>";
requestMain += " <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";
requestMain += " <request i:type=\"b:GrantAccessRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:b=\"http://schemas.microsoft.com/crm/2011/Contracts\">";
requestMain += " <a:Parameters xmlns:c=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <c:key>Target</c:key>";
requestMain += " <c:value i:type=\"a:EntityReference\">";
requestMain += " <a:Id>";
requestMain += AccountId;
requestMain += "</a:Id>";
requestMain += " <a:LogicalName>account</a:LogicalName>";
requestMain += " <a:Name i:nil=\"true\" />";
requestMain += " </c:value>";
requestMain += " </a:KeyValuePairOfstringanyType>";
requestMain += " <a:KeyValuePairOfstringanyType>";
requestMain += " <c:key>PrincipalAccess</c:key>";
requestMain += " <c:value i:type=\"b:PrincipalAccess\">";
requestMain += " <b:AccessMask>ReadAccess</b:AccessMask>";
requestMain += " <b:Principal>";
requestMain += " <a:Id>";
requestMain += SystemUserId;
requestMain += " </a:Id>";
requestMain += " <a:LogicalName>systemuser</a:LogicalName>";
requestMain += " <a:Name i:nil=\"true\" />";
requestMain += " </b:Principal>";
requestMain += " </c:value>";
requestMain += " </a:KeyValuePairOfstringanyType>";
requestMain += " </a:Parameters>";
requestMain += " <a:RequestId i:nil=\"true\" />";
requestMain += " <a:RequestName>GrantAccess</a:RequestName>";
requestMain += " </request>";
requestMain += " </Execute>";
requestMain += " </s:Body>";
requestMain += "</s:Envelope>";
var req = new XMLHttpRequest();
//req.open("POST", Xrm.Page.context.getClientUrl() + "/XRMServices/2011/Organization.svc/web", true);
req.open("POST", GetGlobalContext().getClientUrl() + "/XRMServices/2011/Organization.svc/web", true);
req.setRequestHeader("Accept", "application/xml, text/xml, */*");
req.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
req.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/xrm/2011/Contracts/Services/IOrganizationService/Execute");
req.onreadystatechange = function () { CallSOAPResponse(req); };
req.send(requestMain);
}
function CallSOAPResponse(req)
{
if (req.readyState == 4) {
req.onreadystatechange = null; //avoids memory leaks
if (req.status == 200) {
//成功我这里什么都不做
}
else {
Xrm.Utility.alertDialog("共享客户失败。请确保共享给的用户对客户实体至少具有最低级别的读取权限。");
}
}
}