如何在CRM Online 2013中使用Javascript查询记录注释中保存的图像

如何在CRM Online 2013中使用Javascript查询记录注释中保存的图像,javascript,dynamics-crm-2011,odata,crm,dynamics-crm-online,Javascript,Dynamics Crm 2011,Odata,Crm,Dynamics Crm Online,我的实体中的每个记录都附有一张图像。我想在web资源中的记录中显示这些图像,就像记录图片一样。我正在使用以下代码: function GetData(){ // var base64image = document.getElementById('image').src.substr(document.getElementById('image').src.indexOf('base64')+7); var recordId = window.parent.Xrm.Page.data

我的实体中的每个记录都附有一张图像。我想在web资源中的记录中显示这些图像,就像记录图片一样。我正在使用以下代码:

function GetData(){
//  var base64image = document.getElementById('image').src.substr(document.getElementById('image').src.indexOf('base64')+7);

    var recordId = window.parent.Xrm.Page.data.entity.getId();  
    var serverUrl = Xrm.Page.context.getServerUrl().toString();
    var ODATA_ENDPOINT = "XRMServices/2011/OrganizationData.svc";
    var objAnnotation = new Object();
    var ODATA_EntityCollection = "/AnnotationSet";
    var temp= "/AnnotationSet?$select=DocumentBody,FileName,MimeType,ObjectId&$filter=ObjectId/Id eq guid'" + recordId + "'";
    var result =serverUrl + ODATA_ENDPOINT + ODATA_EntityCollection + temp; 


    // Parse the entity object into JSON 
    var jsonEntity = window.JSON.stringify(objAnnotation);

    // Asynchronous AJAX function to Create a CRM record using OData 
    $.ajax({
        type: "GET",
        contentType: "application/json; charset=utf-8",
        datatype: "json",
        url: result ,
        //data: jsonEntity,
        async: false,
        beforeSend: function (XMLHttpRequest) {
            XMLHttpRequest.setRequestHeader("Accept", "application/json");
        },
        success: function(status){
            alert("success paa jee!!");
        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
            alert("Status: " + textStatus + "; ErrorThrown: " + errorThrown);
        }
    });
}
</script>
函数GetData(){ //var base64image=document.getElementById('image').src.substr(document.getElementById('image').src.indexOf('base64')+7); var recordId=window.parent.Xrm.Page.data.entity.getId(); var serverUrl=Xrm.Page.context.getServerUrl().toString(); var ODATA_ENDPOINT=“XRMServices/2011/OrganizationData.svc”; var objAnnotation=新对象(); var ODATA_EntityCollection=“/AnnotationSet”; var temp=“/AnnotationSet?$select=DocumentBody,FileName,MimeType,ObjectId&$filter=ObjectId/Id eq guid'+recordId+”; var结果=服务器URL+ODATA_端点+ODATA_EntityCollection+temp; //将实体对象解析为JSON var jsonEntity=window.JSON.stringify(objAnnotation); //使用OData创建CRM记录的异步AJAX函数 $.ajax({ 键入:“获取”, contentType:“应用程序/json;字符集=utf-8”, 数据类型:“json”, url:结果, //数据:JSONENITY, async:false, beforeSend:函数(XMLHttpRequest){ setRequestHeader(“接受”、“应用程序/json”); }, 成功:功能(状态){ 警惕(“成功帕吉!!”); }, 错误:函数(xmlHttpRequest、textStatus、errorshown){ 警报(“状态:+textStatus+”;错误抛出:+Error抛出); } }); } 但是当我到达Ajax部分时,我得到一个错误,
$未定义。基本上,每个记录的注释中都有一个图像附在实体的记录上,我想在web资源中将此图像显示为记录图片

如果有更好的/另一种方法,我愿意接受建议


编辑:我已经编辑了代码并更新了ODataURL

在CRM 2011中,我使用了两个
自定义
Aspx页面
来显示所附图像

第1页:AccountImage.aspx具有以下控件:

<asp:Image ID="IMG_Logo" runat="server" Height="50px" ImageUrl="AccountImageForm.aspx" Visible="false"  />
ShowImages()
函数包含以下代码:

function ShowImages()
{
  IMG_Logo.Visible = false;
  QueryExpression query = new QueryExpression("annotation");
  query.Criteria.AddCondition("objectid", ConditionOperator.Equal, Id);
  query.Criteria.AddCondition("mimetype", ConditionOperator.In, new string[] { "image/x-png", "image/pjpeg", "image/png", "image/jpeg" });
  query.Criteria.AddCondition("subject", ConditionOperator.NotEqual, "membershipcardthumbnail");
  query.Criteria.AddCondition("subject", ConditionOperator.NotEqual, "membershipcardimage");
  query.ColumnSet = new ColumnSet(true);
  EntityCollection AllLogoImageNotes = Common.Common.RetrieveMultiple(query);
  if (AllLogoImageNotes.Entities.Count > 0)
        {
         foreach (Entity Note in AllLogoImageNotes.Entities)
            {
            if (Note.Attributes.Contains("subject") && Note.Attributes.Contains("documentbody"))
                {
                    if (Note["subject"].ToString().ToLower() == "accountlogoimage")
                    {
                        HttpRuntime.Cache.Remove("AccountLogoImage");
                        HttpRuntime.Cache.Remove("AccountLogoImageType");
                        HttpRuntime.Cache.Add("AccountLogoImage", Convert.FromBase64String(Note["documentbody"].ToString()), null, DateTime.Now.AddMinutes(5), TimeSpan.Zero, CacheItemPriority.Normal, null);
                        HttpRuntime.Cache.Add("AccountLogoImageType", Note["mimetype"].ToString(), null, DateTime.Now.AddMinutes(5), TimeSpan.Zero, CacheItemPriority.Normal, null);
                        IMG_Logo.ImageUrl = "AccountImageForm.aspx" + "?time=" + DateTime.Now.ToString();
                        IMG_Logo.Visible = true;
                       }
                }
            }
        }
  }
protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();
        if (HttpRuntime.Cache["AccountLogoImage"] != null)
        {
            Response.ContentType = HttpRuntime.Cache["AccountLogoImageType"].ToString();
            byte[] data = (byte[])HttpRuntime.Cache["AccountLogoImage"];
            Response.BinaryWrite(data);

        }
    }
如您所见,下面的行:

IMG_Logo.ImageUrl=“AccountImageForm.aspx”+”?time=“+DateTime.Now.ToString()

AccountImageForm.aspx
中编写以下代码:

function ShowImages()
{
  IMG_Logo.Visible = false;
  QueryExpression query = new QueryExpression("annotation");
  query.Criteria.AddCondition("objectid", ConditionOperator.Equal, Id);
  query.Criteria.AddCondition("mimetype", ConditionOperator.In, new string[] { "image/x-png", "image/pjpeg", "image/png", "image/jpeg" });
  query.Criteria.AddCondition("subject", ConditionOperator.NotEqual, "membershipcardthumbnail");
  query.Criteria.AddCondition("subject", ConditionOperator.NotEqual, "membershipcardimage");
  query.ColumnSet = new ColumnSet(true);
  EntityCollection AllLogoImageNotes = Common.Common.RetrieveMultiple(query);
  if (AllLogoImageNotes.Entities.Count > 0)
        {
         foreach (Entity Note in AllLogoImageNotes.Entities)
            {
            if (Note.Attributes.Contains("subject") && Note.Attributes.Contains("documentbody"))
                {
                    if (Note["subject"].ToString().ToLower() == "accountlogoimage")
                    {
                        HttpRuntime.Cache.Remove("AccountLogoImage");
                        HttpRuntime.Cache.Remove("AccountLogoImageType");
                        HttpRuntime.Cache.Add("AccountLogoImage", Convert.FromBase64String(Note["documentbody"].ToString()), null, DateTime.Now.AddMinutes(5), TimeSpan.Zero, CacheItemPriority.Normal, null);
                        HttpRuntime.Cache.Add("AccountLogoImageType", Note["mimetype"].ToString(), null, DateTime.Now.AddMinutes(5), TimeSpan.Zero, CacheItemPriority.Normal, null);
                        IMG_Logo.ImageUrl = "AccountImageForm.aspx" + "?time=" + DateTime.Now.ToString();
                        IMG_Logo.Visible = true;
                       }
                }
            }
        }
  }
protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();
        if (HttpRuntime.Cache["AccountLogoImage"] != null)
        {
            Response.ContentType = HttpRuntime.Cache["AccountLogoImageType"].ToString();
            byte[] data = (byte[])HttpRuntime.Cache["AccountLogoImage"];
            Response.BinaryWrite(data);

        }
    }
在ODATA中,您可以执行以下操作:

  retrieveImages("/AnnotationSet?$select=DocumentBody,MimeType&$filter=ObjectId/Id eq guid'" + Xrm.Page.data.entity.getId()+ "'", function (JsonObject) {
    if (JsonObject != null) {
        //  debugger;
            var ByteString= JsonObject[0].DocumentBody;
            var MimeType =  JsonObject[0].MimeType
    }


 function retrieveImages(query, SuccessFunc) {
var retrieveRecordsReq = new XMLHttpRequest();
var ODataPath = Xrm.Page.context.getServerUrl() + "/xrmservices/2011/OrganizationData.svc";
retrieveRecordsReq.open('GET', ODataPath + query, false);
retrieveRecordsReq.setRequestHeader("Accept", "application/json");
retrieveRecordsReq.setRequestHeader("Content-Type", "application/json; charset=utf-8");
retrieveRecordsReq.onreadystatechange = function () {
    if (this.readyState == 4 /* complete */) {
        if (this.status == 200) {
            this.onreadystatechange = null; //avoids memory leaks
            var data = JSON.parse(this.responseText, SDK.REST._dateReviver);
            if (data && data.d && data.d.results)
                SuccessFunc(JSON.parse(this.responseText, SDK.REST._dateReviver).d.results);
        }
        else {
            alert(SDK.REST._errorHandler(this));
        }
    }
};
retrieveRecordsReq.send();
}

没有办法通过ODATA做到这一点。我似乎想不出所需的查询程序。您如何在crm中上载aspx页面?@hkhan您必须将aspx页面作为单独的网站或ISV文件夹上载到crm服务器。然后使用IFrame显示页面。我不认为你们可以在CRM Online中这样做。这就是为什么我要使用javascript和OData。由于缺少一些括号,我不得不对代码进行一些更改,但这确实起到了作用……现在下一步是显示检索到的图像。谢谢小多特。